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)。