平移视图和旋转视图问题
我有一个自定义的 UIImageView,我可以通过进行翻译来将其拖动到屏幕上(xDif 和 yDif 是手指移动的量):
CGAffineTransform translate = CGAffineTransformMakeTranslation(xDif, yDif);
[self setTransform: CGAffineTransformConcat([self transform], translate)];
假设我在 x 和 y 方向上将 ImageView 移动了 50px。然后,我尝试旋转 ImageView (通过手势识别器):
CGAffineTransform transform = CGAffineTransformMakeRotation([recognizer rotation]);
myImageView.transform = transform;
发生的情况是 ImageView 突然移动到 ImageView 最初所在的位置(在平移之前 - 不是从移动的位置 + 50px 在两个方向上)。
(似乎无论我如何翻译视图,ImageView 子类的 self.center 保持不变 - 它最初放置在 IB 中的位置)。
另一个问题是,如果我将 ImageView 旋转 30 度,然后尝试再旋转一点,它会再次从原始位置(角度 = 0)开始并从那里开始,为什么它不从角度开始30 度而不是 0 度。
I have a custom UIImageView, I can drag it around screen by making a translation with (xDif and yDif is the amount fingers moved):
CGAffineTransform translate = CGAffineTransformMakeTranslation(xDif, yDif);
[self setTransform: CGAffineTransformConcat([self transform], translate)];
Let's say I moved the ImageView for 50px in both x and y directions. I then try to rotate the ImageView (via gesture recognizer) with:
CGAffineTransform transform = CGAffineTransformMakeRotation([recognizer rotation]);
myImageView.transform = transform;
What happens is the ImageView suddenly moves to where the ImageView was originally located (before the translation - not from the moved position + 50px in both directions).
(It seems that no matter how I translate the view, the self.center of the ImageView subclass stays the same - where it was originally laid in IB).
Another problem is, if I rotate the ImageView by 30 deg, and then try to rotate it a bit more, it will again start from the original position (angle = 0) and go from there, why wouldn't it start from the angle 30 deg and not 0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在覆盖之前的转换。要添加到当前变换,您应该这样做 -
由于您要按顺序更改
transform
属性,因此您应该使用CGAffineTransformRotate
、CGAffineTransformTranslate 和
CGAffineTransformScale
来代替,以便您添加到原始变换而不是创建新变换。You are overwriting the earlier transform. To add to the current transform, you should do this –
Since you're changing the
transform
property in a serial order, you should useCGAffineTransformRotate
,CGAffineTransformTranslate
andCGAffineTransformScale
instead so that you add to the original transform and not create a new one.