本文介绍了SWIFT 在 Main.storyboard 中定义的 UIImageview 中旋转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 SWIFT 的新手,我正在练习学习,但在某些方面遇到了一些困难.我在 Main.storyboard 定义的 UIImageview 中有一个图像,我需要旋转.

I'm new to SWIFT and I'm practicing to learn but I'm having some difficulty with something.I have an image in an UIImageview defined at the Main.storyboard that I need to rotate.

我有一个 IBOutlet 定义为:

I have an IBOutlet defined as:

@IBOutlet weak var imageToRotate: UIImageView!

我试图用最右侧和高度中间的旋转中心旋转它,根据苹果文档和这里的一些帖子,我使用了锚点:

I was trying to rotate it with the center of rotation on the rightmost side and middle of height and as per the apple documentation and some posts in here I used the anchorPoint:

imageToRotate.layer.anchorPoint = CGPointMake( 1.0, 0.5 )

然后我尝试使用变换来旋转它:

Then I try to rotate it using the transform:

imageToRotate.transform = CGAffineTransformMakeRotation(CGFloat( myRadians ) )

图像在 Main.storyboard 中定义,加载时看起来不错,但是当我发出 anchorPoint 时,图像向左移动,右边缘转到图像过去的中心位置和旋转发生在那个点附近.

The image is defined in the Main.storyboard and it looks good when it loads but when I issue the anchorPoint the image gets shifted to the left and the right edge goes to where the center of the image used to be and the rotation happens around that point.

我尝试使用:

imageToRotate.center = CGPointMake( 487, 160)

将图像移回它应该在的位置,但它没有移动,所以这也让我感到困惑.

to move the image back to the place where it's supposed to be but it doesn't move so that also has me confused.

我想我在这里遗漏了一些东西,但我无法弄清楚它是什么.我是否必须定义子层或其他东西?此外,仅仅发布锚点移动图像的事实告诉我,我遗漏了一些东西.

I think I'm missing something here but I fail to figure out what it is. Do I have to define a sublayer or something else?Also the fact that just issuing the anchorpoint moves the image tells me there's something I'm missing.

在类定义中,我使用了它并将其链接到故事板的 UIImage:

In the class definition I used this and got it linked to the storyboard's UIImage:

@IBOutlet weak var imageToRotate: UIImageView!

在 viewDidLoad() 的覆盖中我使用了这个:

inside the override of viewDidLoad() I used this:

imageToRotate.layer.anchorPoint = CGPointMake(1.0, 0.5)

imageToRotate.transform = CGAffineTransformMakeRotation(CGFloat( myRadians ) )

谢谢.

推荐答案

你真正需要做的就是围绕一个点评分,将平移和旋转结合起来.平移是以图像中心为原点的 x 和 y 坐标.旋转是以弧度为单位的 CGFloat.它可能看起来像这样:

All you really need to do to rate around a point is combine a translation and a rotation. The translation is an x and y coordinate with the origin at the center of the image. The rotation is a CGFloat in radians. It may look something like this:

let square2 = UIView(frame: CGRectMake(200, 200, 50, 50))
view.addSubview(square2)
// Desired point from the center to rotate around
let x = CGFloat(25)
let y = CGFloat(0)
var transform = CGAffineTransformMakeTranslation(x, y)
transform = CGAffineTransformRotate(transform, CGFloat(M_PI_4))
transform = CGAffineTransformTranslate(transform,-x,-y)
square2.backgroundColor = UIColor.magentaColor()
square2.transform = transform

这会围绕新的旋转轴创建以下旋转:

This creates the following rotation around the new axis of rotation:

这篇关于SWIFT 在 Main.storyboard 中定义的 UIImageview 中旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:25