使用CGAFFINETRANSFORM量表调整Uiview的大小,而不是更新框架
我正在尝试使用CGAFFINETRANSFORM量表调整带有一些子视图的Uiview(parent)大小。 我是通过使用锅手势从一个角落拖动父母来调整父母的大小。 调整大小按预期工作,但是如果我尝试再次调整大小,它将跳回初始帧。就像它不知道它已经调整大小。
这些是我到目前为止所做的步骤:
1 .-仅当锅手势开始时,我就会获得初始框架和触摸位置:
if gesture.state == .began {
//We get all initial values from the first touch
initialFrame = self.frame;
touchStart = gesture.location(in: superview)
}
2.-然后我转到正在拖动的手柄(在这种情况下, ),设置锚点,计算三角洲(初始触摸 - 手势距离),计算新框架,尺度并应用变换。
case topRight:
if gesture.state == .began {
self.setAnchorPoint(anchorPoint: CGPoint(x: 0, y: 1))
}
let deltaX = -1 * (touchStart.x - gesture.location(in: superview).x)
let deltaY = 1 * (touchStart.y - gesture.location(in: superview).y)
let newWidth = initialFrame.width + deltaX;
let newHeight = initialFrame.height + deltaY;
let scaleX:CGFloat = newWidth / initialFrame.width;
let scaleY:CGFloat = newHeight / initialFrame.height;
self.transform = CGAffineTransform.identity.scaledBy(x: scaleX, y: scaleY)
3.-最终,我将锚点重置为Uiview的中间。
if gesture.state == .ended {
self.setAnchorPoint(anchorPoint: CGPoint(x: 0.5, y: 0.5))
}
我附上了一个GIF,您可以从右上方的手柄中看到Uiview大小。当我尝试再次调整大小时,它会跳回初始帧。 (看来视频重新启动了,但这是跳跃)
我缺少什么?我需要更新其他内容吗? 谢谢大家!
I'm trying to resize a UIView (Parent) with a few subviews in it using CGAffineTransform scale.
I'm resizing the parent by dragging it from one corner using pan gesture.
The resizing works as expected but if I try to resize it again, it jumps back to the initial frame. It's like it never knew it was resized.
These are the steps I am doing so far:
1.- Just when the pan gesture begins I get the initial frame and the touch location in superview:
if gesture.state == .began {
//We get all initial values from the first touch
initialFrame = self.frame;
touchStart = gesture.location(in: superview)
}
2.- Then I go to the handle I'm dragging (Top right in this case), set the anchor point, calculate deltas (Initial touch - gesture distance traveled), calculate new frame, scales, and apply transform.
case topRight:
if gesture.state == .began {
self.setAnchorPoint(anchorPoint: CGPoint(x: 0, y: 1))
}
let deltaX = -1 * (touchStart.x - gesture.location(in: superview).x)
let deltaY = 1 * (touchStart.y - gesture.location(in: superview).y)
let newWidth = initialFrame.width + deltaX;
let newHeight = initialFrame.height + deltaY;
let scaleX:CGFloat = newWidth / initialFrame.width;
let scaleY:CGFloat = newHeight / initialFrame.height;
self.transform = CGAffineTransform.identity.scaledBy(x: scaleX, y: scaleY)
3.- Finally I reset the anchor point to the middle of the UIView.
if gesture.state == .ended {
self.setAnchorPoint(anchorPoint: CGPoint(x: 0.5, y: 0.5))
}
I attached a gif where you can see the UIView is resized from the top right handle. When I try to resize it again, it jumps back to the initial frame. (It seems that the video is restarted, but this is the jump)
what am I missing? do I need to update something else?
Thank you all!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,答案实际上很简单,以下代码行不正确:
在这里,我将缩放率应用于始终是原始大小的身份变换。
仅通过将其更改为:
现在,我将缩放量应用于当前变换,该转换总是在手势开始时保存。
希望这对那些想缩放类似于photoshop的Uiview的人会有所帮助。如果子视图旋转,它甚至可以工作。
请记住,一旦视图转换,您 就无法再设置帧值了。这就是为什么通过在监督中拥有所有视图更方便的原因,因此它可以照顾所有调整大小的调整。
目的是复制 Photoshop 在调整大小时所做的相同行为。
So the answer is actually quite simple, this following line of code was incorrect:
Here I am applying the scaling to the identity transform which is always the original size.
Just by changing this to:
Now I am applying the scaling to the current transform which is always saved when the gesture begins.
Hopefully this can be helpful for those who would like to scale a UIView similar to like Photoshop does with it's layers. It even works if the subviews were rotated.
Keep in mind that once the view is transformed, you cannot set the frame value anymore. That's why by having all views inside a superView is more convenient so it takes care of all resizing.
The goal was to replicate the same behavior that Photoshop does when resizing layers.