2017-08-06 80 views
0

使用此代码:连续旋转不集中

UIView.animate(withDuration: 0.5, delay: 0, options: [.repeat], animations: { 
     self.star.transform = self.star.transform.rotated(by: CGFloat(M_PI_2)) 
    }) 

我的看法是这样做的: enter image description here

使用此代码:

extension UIView { 
    func rotate360Degrees(duration: CFTimeInterval = 3) { 
     let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation") 
     rotateAnimation.fromValue = 0.0 
     rotateAnimation.toValue = CGFloat(M_PI * 2) 
     rotateAnimation.isRemovedOnCompletion = false 
     rotateAnimation.duration = duration 
     rotateAnimation.repeatCount=Float.infinity 
     self.layer.add(rotateAnimation, forKey: nil) 
    } 
} 

我的看法是这样做的: enter image description here

Wel我都没有做我想做的事。正在旋转的视图是具有要填充的比例的UIImageView。我希望图像保持在中间。我怎么能做到这一点?这些函数在viewDidAppear中执行。最后的GIF看起来更好,但注意到这颗星并不是完美的中心... This is the image

回答

3

问题

图片的中心不是你明星的中心。

解决方案

有两种可能的解决方案

  • 编辑图像,从而使恒星的该中心是在图像的中心。
  • 将旋转图层的锚点设置为星号的中心(x:1004px,y:761px)。