为什么在使用CGAffineTransformMakeRotation旋转视图时视图会改变位置
我有以下超级简单的animation,我基本上旋转一个视图从原来的angular度/中心2弧度,它旋转得很好我唯一的误解是为什么旋转发生时视图从原来的位置移动。
[UIView animateWithDuration:1.0 animations:^{ self.someView.transform = CGAffineTransformMakeRotation( 2 ); }];
为什么用上面的代码旋转视图时会移动?
我目前正在尝试辨别CGAffineTransform参考中的信息。
了解定位点 。
我发现这个线程,但它没有显示具体的答案。 为什么旋转imageView,它改变位置?
非常感谢
由于@fs_tigre请求,我添加了另一个答案。 问题是你的xib文件中的自动布局,不幸的是,它不知道为什么会影响转换。
下面是我解决这个问题的步骤:
1-首先你需要摆脱你的自动布局(是的,你必须)
取消选中使用Autolayout
2-删除所有将被旋转的视图的约束和自动调整掩码,如屏幕截图所示(这里有我的蓝色方块,请参阅右边的自动调整,没有被选中)
我已经对你的代码做了一些改变
self.someView.layer.anchorPoint = CGPointMake(0.5, 0.5); // one degree = pi/180. so... // rotate by 90 CGFloat radians = (M_PI/180) * 90; [UIView animateWithDuration:1.0 animations:^{ self.someView.transform = CGAffineTransformRotate(self.someView.transform, radians); }];
点击rotate
,看魔术:)
您需要设置视图的锚点来旋转。
self.somview.layer.anchorPoint = CGPointMake(0.5, 0.5);
然后开始旋转。
从苹果公司的文件
@property(nonatomic) CGAffineTransform transform
对这个属性的改变可以是animation的。 使用beginAnimations:context:类方法开始,使用commitAnimations类方法结束animation块。 默认值是无论中心值是什么(或锚点如果改变)链接: https : //developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CALayer_class/Introduction/Introduction.html#//apple_ref/occ / instp / CALayer的/ anchorPoint
来自这里的图片http://www.raywenderlich.com/9864/how-to-create-a-rotating-wheel-control-with-uikit
– 正如你看到的定位点是从0.0到1.0的值,当你旋转旋转的X和Y的点将在这些点
注意:您需要导入QuartzCore
CGAffineTransformMakeRotation
的“锚”是X,Y的观点。 你可以试试这个:
CGPoint center = self.someView.center; [UIView animateWithDuration:1.0 animations:^{ self.someView.transform = CGAffineTransformMakeRotation( 2 ); self.someView.center = center; }];