为什么视图在使用CGAffineTransformMakeRotation旋转时会改变位置

fs_*_*gre 18 objective-c rotation ios

我有以下超级简单的动画,我基本上从它的原始角度/中心旋转视图2弧度,它旋转很好我唯一的误解是为什么视图在旋转发生时从其原始位置移动.

[UIView animateWithDuration:1.0 animations:^{
        self.someView.transform = CGAffineTransformMakeRotation( 2 );
    }];
Run Code Online (Sandbox Code Playgroud)

使用上面的代码旋转时为什么视图会移动?

在此输入图像描述

我目前正在尝试辨别CGAffineTransform参考中的信息.

了解锚固.

我找到了这个帖子,但它没有显示出具体的答案. 为什么旋转imageView,它改变位置?

非常感谢

Bas*_*CAD 10

您需要将视图的锚点设置为围绕旋转.

self.somview.layer.anchorPoint = CGPointMake(0.5, 0.5);
Run Code Online (Sandbox Code Playgroud)

然后开始旋转.
来自Apple文档

@property(nonatomic) CGAffineTransform transform
可以对此属性的更改进行动画处理.使用beginAnimations:context:class方法开始,使用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
- 如您所见,锚点是值为0.0 - 1.0的点旋转旋转时的X和Y将围绕这些点
注意:您需要导入QuartzCore


Bas*_*CAD 7

由于@fs_tigre请求,我正在添加另一个答案.问题在于xib文件中的自动布局,遗憾的是,为什么会影响转换,这是未知的.
现在,这是我为解决问题所采取的步骤:

1-首先你需要摆脱你的自动布局(是的,你必须)
取消选中使用Autolayout
取消选中使用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);
    }];
Run Code Online (Sandbox Code Playgroud)

点击rotate查看魔术:)