如何在不改变中心X位置的同时旋转图像视图

时间:2017-04-20 08:29:11

标签: ios objective-c

我想使用以下内容旋转图像视图:

CGFloat degrees = -20.0f; //the value in degrees
    CGFloat radians = degrees * M_PI/180;
    _arrowImgView.transform = CGAffineTransformMakeRotation(radians);

它会旋转,但不幸的是它会旋转整个图像向上移动。请看一下屏幕截图:

enter image description here

enter image description here

在第二个屏幕截图中,您可以看到图像向上移动并更改了X坐标。如何像时钟箭一样弯曲它?

更新: 我将代码更改为:

self.arrowImgView.layer.anchorPoint = CGPointMake(0.0f, 0.0f);
    CGFloat degrees = -20.0f; //the value in degrees
    CGFloat radians = degrees * M_PI/180;
    self.arrowImgView.transform = CGAffineTransformMakeRotation(radians);

enter image description here

另外我想要注意到我确实设置了这样的约束:

 [_arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self);
        make.centerX.equalTo(self.mas_centerX);
    }];

1 个答案:

答案 0 :(得分:2)

试试此代码

imageView.layer.anchorPoint = CGPointMake(0.0f, 0.0f);
imageView.transform = CGAffineTransformMakeRotation(angle);

参考: https://developer.apple.com/reference/quartzcore/calayer/1410817-anchorpoint

  

修改

我在下面的代码中尝试了以下代码

self.viewRotate.layer.anchorPoint = CGPoint(x: 0, y: 0)
    let degree : CGFloat = -20.0
    let rad : CGFloat = degree*CGFloat(M_PI/180)
    viewRotate.transform = CGAffineTransform(rotationAngle: rad)

<强>输出

轮换前

enter image description here

轮换后

enter image description here