我有以下超级简单动画,我基本上是从原始角度/中心旋转视图2弧度,它旋转很好我唯一的误解是为什么视图在旋转发生时从其原始位置移动。
[UIView animateWithDuration:1.0 animations:^{
self.someView.transform = CGAffineTransformMakeRotation( 2 );
}];
为什么使用上面的代码旋转时视图会移动?
我目前正试图辨别CGAffineTransform Reference中的信息。
了解锚point。
我找到了这个帖子,但没有显示出具体的答案。 Why rotating imageView, it changes position?
非常感谢
答案 0 :(得分:10)
您需要将视图的定位点设置为旋转。
self.somview.layer.anchorPoint = CGPointMake(0.5, 0.5);
然后开始旋转。
来自Apple文档
@property(nonatomic) CGAffineTransform transform
对此进行了更改 属性可以动画。使用beginAnimations:context:类 方法开始和commitAnimations类方法结束 动画块。默认值是中心值(或 锚点如果改变了) 链接: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
- 如您所见,锚点是X和Y的值为0.0 - 1.0的点
旋转时旋转将围绕这些点
注意:您需要导入QuartzCore
答案 1 :(得分:7)
由于@fs_tigre请求,我正在添加另一个答案。问题在于xib文件中的自动布局,遗憾的是,为什么会影响转换,这是未知的。
现在,我是解决问题的步骤:
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
,看看魔术:)
答案 2 :(得分:3)
CGAffineTransformMakeRotation
的“锚点”是视图的X,Y。你可以试试这个:
CGPoint center = self.someView.center;
[UIView animateWithDuration:1.0 animations:^{
self.someView.transform = CGAffineTransformMakeRotation( 2 );
self.someView.center = center;
}];