如何旋转UIImageView相对于任何点(除了它的中心)? [英] How can I rotate a UIImageView with respect to any point (other than its center)?

查看:147
本文介绍了如何旋转UIImageView相对于任何点(除了它的中心)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,UIImageView只会绕其中心旋转。如何让它围绕图像中的任何其他点旋转?

By default, a UIImageView will rotate only about its center. How do I get it to rotate about any other point in the image?

推荐答案

这样做的一种方法是更改​​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];

这篇关于如何旋转UIImageView相对于任何点(除了它的中心)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆