2015-06-20 64 views
1

我想旋轉imageview永遠。我試過下面的代碼:Spining imageview動畫

UIView.animateWithDuration(3, animations: { 
     self.loginLogo.transform = CGAffineTransformMakeRotation((360 * CGFloat(M_PI))/360) 
     }){ (finished) -> Void in 
      self.rotateImage() 
    } 

但是這個工作一次。我的imageview並沒有永久地變化。我該如何解決它?

+0

你可以看看這個例子的代碼,可以幫助:https://github.com/rebello95/SizedSpinningImage – rebello95

+0

的可能重複[旋轉的UIImageView不斷](HTTP: //sackoverflow.com/.com/questions/7269076/spin-uiimageview-continuously) –

+0

它不適用於swift。 –

回答

1

如果你想永遠圖像旋轉,你可以這樣來做:

func rotateViewLayer() { 
    let rotateView = CABasicAnimation() 

    rotateView.fromValue = 0.degreesToRadian 
    rotateView.toValue = 360.degreesToRadian 
    rotateView.duration = 1 
    rotateView.repeatCount = Float.infinity 
    rotateView.removedOnCompletion = false 
    rotateView.fillMode = kCAFillModeForwards 
    rotateView.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionLinear) 
    imageView.layer.addAnimation(rotateView, forKey: "transform.rotation.z") 
} 

這裏是你的幫手擴展:

extension Int { 
    var degreesToRadian : CGFloat { 
     return CGFloat(self) * CGFloat(M_PI)/180.0 
    } 
} 

您可以參考THIS示例項目獲取更多信息。

0

使用這種在您的視圖層

var myImageView:UIImageView? 
    var rotation = CABasicAnimation(keyPath: "transform.rotation") 
    rotation.fromValue = 0.0 
    rotation.toValue = 2*M_PI 
    rotation.duration = 1.1 
    rotation.repeatCount = Float.infinity 
    myImageView?.layer.addAnimation(rotation, forKey: "Spin") 
+0

如何實現它到imageview? –

+0

編輯答案 –

+0

我試過了,但。圖像不移動。 –

0

這應該工作

func startAnimationWithDuration(duration:CGFloat) 
    { 
     var rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z") 
     rotationAnimation.fromValue = 0 
     rotationAnimation.toValue = 2 * M_PI 
     rotationAnimation.duration = NSTimeInterval(duration) 
     rotationAnimation.repeatCount = 1000.0 //some large value 
     imageView.layer.addAnimation(rotationAnimation, forKey: "spin") 
    }