我在 UIView
中添加了一个 UIImageView
作为 subview ,然后在 UITouchesBegan
中使用以下代码移动该 subview 。
CGPoint newTouch = [[touches anyObject] locationInView:self];
CGPoint lastTouch = [[touches anyObject] previousLocationInView:self];
float xDif = newTouch.x - lastTouch.x;
float yDif = newTouch.y - lastTouch.y;
translate = CGAffineTransformMakeTranslation(xDif, yDif);
[self setTransform: CGAffineTransformConcat([self transform], translate)];
然后我使用以下代码旋转该 subview 。
self.transform=CGAffineTransformRotate(self.transform, RADIANS(180));
现阶段,一切都很好.. 但是当我再次尝试移动 subview 时,它会向相反的方向移动,即当我想向上移动它时,它会向下移动。
有什么想法吗?有什么建议吗?
最佳答案
为什么要使用平移变换来移动 subview ?只需更新 center
属性即可。使用平移变换的问题是,如果它被旋转,它确实会沿相对于当前旋转的方向平移(正如您所发现的,这可能不是您所期望的)。如果您想坚持使用平移变换(为什么?),那么您需要确保旋转回 0(中性),进行位置平移,然后旋转回所需的旋转。
变换的顺序很重要。
关于ios - 旋转 subview 后移动 subview ,无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11077075/