Why does a view changes position when rotating it using CGAffineTransformMakeRotation

前端 未结 3 1356
执念已碎
执念已碎 2021-02-12 21:35

I have the following super simple animation, I\'m basically rotating a view 2 radians from its original angle/center, it rotates fine my only misunderstanding is why does the vi

3条回答
  •  心在旅途
    2021-02-12 22:30

    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- first you need to get rid off your auto layout (yes, you have to)
    uncheck Use Autolayout
    uncheck Use Autolayout

    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)
    enter image description here

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

    Click rotate and see the magic :)

提交回复
热议问题