How can I rotate a UIImageView with respect to any point (other than its center)?(如何相对于任何点(除了它的中心)旋转 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何相对于任何点(除了它的中心)旋转 UIImageView?
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01