且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-02 14:54:52

由于@fs_tigre请求,我正在添加另一个答案。问题在于xib文件中的自动布局,遗憾的是,为什么会影响转换,这是未知的。

现在这里是我为解决问题所采取的步骤:

I am adding another answer due to @fs_tigre request. The problem is with the auto layouts in your xib file, unfortunately is it unknown why that affects the transform.
Now here is the steps I did to solve the issue:

1-首先你需要摆脱你的汽车布局(是的,你必须)

取消选中使用Autolayout

1- first you need to get rid off your auto layout (yes, you have to)
uncheck Use Autolayout

2-删除所有约束并自动调整要旋转的视图的遮罩,如截屏
(这里我有我的蓝盒子,看右边自动调整,没有选择)

2- remove all constraints and autoresizing masks for your view that will be rotated, as in the screenshot (Here I have my blue box, see on the right autoresizing, nothing is selected)

我对您的轮值代码进行了一些更改

I have made some changes for your rotation's code

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);
    }];

点击旋转,看看魔术:)