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

查看:23
本文介绍了如何相对于任何点(除了它的中心)旋转 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?

推荐答案

这样做的一种方法是更改​​ UIImageView 底层的 anchorPoint 并围绕它旋转.您可以使用以下内容更改旋转点:

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.

移动锚点可能会移动您的图像,因此您可能需要在之后调整其位置.

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天全站免登陆