简体   繁体   English

动画UIImageView旋转时的时髦行为

[英]Funky behavior when animating rotation of UIImageView

I'm trying to rotate an UIImageView with the following code: 我正在尝试使用以下代码旋转UIImageView:

var x = -((self.needleImage.frame.size.width/2) - 15) //x for rotation point

UIView.animateWithDuration(5.0, delay: 10.0, options: nil, animations: {
    var transform = CGAffineTransformMakeTranslation(x, 0)
    transform = CGAffineTransformRotate(transform, CGFloat(-M_PI_2))
    transform = CGAffineTransformTranslate(transform, -x, 0)
    self.needleImage.transform = transformm

The end position is right, but during the animation/rotation, the image is shifted a little to the left before settling at the right place. 结束位置是正确的,但是在动画/旋转过程中,图像会向左移动一点,然后再固定在正确的位置。

I tried the same code with a UIView from this and it doesn't do that. 我试图从一个UIView相同的代码 ,并没有做到这一点。

Then I tried wrapping the Imageview inside a View and rotating that, but that didn't help either. 然后我尝试将Imageview包裹在View内并旋转它,但这也无济于事。

I have drawn a cicle ontop of the rotation point to check if it isn't in the right place, but it seems alright. 我在旋转点的顶部绘制了一个cicle,以检查它是否在正确的位置,但是看起来还不错。

I've used this code snippet in the past to rotate a view perpetually, but by removing the last closure it should work to rotate an image 360 degrees. 我过去曾使用此代码段来永久旋转视图,但是通过删除最后一个闭合,它应该可以将图像旋转360度。 This can be adjusted, or course. 这可以调整,或者当然。 Right now it's set to 2PI. 现在将其设置为2PI。 You'll have to convert your rotation angle to radians. 您必须将旋转角度转换为弧度。

func animateRotator() {
        UIView.animateWithDuration(0.5, delay: 0, options: .CurveLinear, animations: { () -> Void in
            self.rotator.transform = CGAffineTransformRotate(self.rotator.transform, CGFloat(M_PI_2))
            }) { (finished) -> Void in
                self.animateRotator()
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM