Ios 快速旋转Main.storyboard中定义的UIImageview中的图像

Ios 快速旋转Main.storyboard中定义的UIImageview中的图像,ios,iphone,swift,uiimageview,image-rotation,Ios,Iphone,Swift,Uiimageview,Image Rotation,我是斯威夫特的新手,我正在练习学习,但我在某些方面遇到了一些困难。 我在Main.storyboard上定义的UIImageview中有一个图像需要旋转 我的IBOutlet定义为: @IBOutlet weak var imageToRotate: UIImageView! 我试着旋转它,旋转中心在最右边和中间高度,根据苹果的文档和这里的一些帖子,我使用了主播点: imageToRotate.layer.anchorPoint = CGPointMake( 1.0, 0.5 ) 然后我尝试

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

我的IBOutlet定义为:

@IBOutlet weak var imageToRotate: UIImageView!
我试着旋转它,旋转中心在最右边和中间高度,根据苹果的文档和这里的一些帖子,我使用了主播点:

imageToRotate.layer.anchorPoint = CGPointMake( 1.0, 0.5 )
然后我尝试使用变换旋转它:

imageToRotate.transform = CGAffineTransformMakeRotation(CGFloat( myRadians ) )
图像是在Main.storyboard中定义的,加载时看起来不错,但当我发布锚定点时,图像会向左移动,右边缘会移到图像的中心位置,并围绕该点旋转

我尝试使用:

imageToRotate.center = CGPointMake( 487, 160)
将图像移回它应该在的位置,但它不移动,这也让我感到困惑

我想我遗漏了一些东西,但我没能弄清楚是什么。我必须定义一个子层还是其他什么? 另外,仅仅发布主播点移动图像的事实告诉我,我遗漏了一些东西

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

@IBOutlet weak var imageToRotate: UIImageView!
在viewDidLoad()的重写中,我使用了以下内容:

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

imageToRotate.transform = CGAffineTransformMakeRotation(CGFloat( myRadians ) )

谢谢。

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

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
这将围绕新的旋转轴创建以下旋转:

谢谢你帮我做这件事。我使用了你的代码,得到了与使用其他代码相同的结果。子视图以相同的方式旋转,但也向右移动。我确信这个问题与视图定义有关。我使用以下方式打印边界:self.waitLabel.text=NSString(格式:“%.1f、%.1f、%.1f、%.1f”、self.imageToRotate.bounds.origin.x、self.imageToRotate.bounds.origin.y、self.imageToRotate.bounds.maxX、self.imageToRotate.bounds.maxY),我得到了所有的零:0、0.0、0、0.0