且构网

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

如何旋转UIImageView相对于任何点(除了它的中心)?

更新时间:2022-03-14 22:36:38

这样做的一种方法是更改​​anchorPoint的UIImageView的底层和旋转。您可以使用类似以下内容来更改旋转点:

One way of doing this is by changing the anchorPoint of the UIImageView's underlying layer and rotating about that. You can change the point of rotation using something like the following:

imageView.layer.anchorPoint = CGPointMake(0.25, 0.25);

anchorPoint是根据图层中的相对坐标定义的。也就是说,(0,0)是图层的左上角(在iPhone上,UIView图层已经翻转了Y坐标),(1,1)是右下角。

anchorPoint is defined in terms of relative coordinates within the layer. That is, (0,0) is the upper-left of the layer (on the iPhone, where UIView layers have flipped Y coordinates) and (1,1) is the lower-right.

移动anchorPoint可能会移动您的图片,因此您可能需要稍后调整其位置。

Moving the anchorPoint may move your image, so you might need to adjust its position afterwards.

要旋转图片的图层,你可以使用类似下面的方法来改变图层的CATransform3D结构:

To rotate the image's layer about that new anchor point, you can change the CATransform3D struct for the layer using something like the following:

CATransform3D rotatedTransform = imageView.layer.transform;
rotatedTransform = CATransform3DRotate(rotatedTransform, 60.0 * M_PI / 180.0, 0.0f, 0.0f, 1.0f);
imageView.layer.transform = rotatedTransform;

此示例将围绕定位点递增旋转60度。

This example does an incremental rotation of 60 degrees about the anchor point.

对图层的所有更改默认都是动画。您可以通过在以下CATransaction中封装对这些属性的更改来禁用此动画:

All changes to a layer are animated by default. You can disable this animation by enclosing your changes to these properties in a CATransaction like the following:

[CATransaction begin];
[CATransaction setValue:(id)kCFBooleanTrue forKey:kCATransactionDisableActions];    

// Code for changes here

[CATransaction commit];