在Swift中无限期地旋转360度视图?

我想无限期地旋转360度的图像视图。

UIView.animateWithDuration(1.2, delay: 0.0, options: UIViewAnimationOptions.Repeat, animations: { // Rotate the image here self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, 6.28318530717959) // As the options are set to .Repeat, there is no completion }, completion: nil)} 

我该怎么做?

Swift 2.x方式来无限期地旋转UIView,从早期的答案编译:

 // Rotate <targetView> indefinitely private func rotateView(targetView: UIView, duration: Double = 1.0) { UIView.animateWithDuration(duration, delay: 0.0, options: .CurveLinear, animations: { targetView.transform = CGAffineTransformRotate(targetView.transform, CGFloat(M_PI)) }) { finished in self.rotateView(targetView, duration: duration) } } 

更新Swift 3.x

 // Rotate <targetView> indefinitely private func rotateView(targetView: UIView, duration: Double = 1.0) { UIView.animate(withDuration: duration, delay: 0.0, options: .curveLinear, animations: { targetView.transform = targetView.transform.rotated(by: CGFloat(M_PI)) }) { finished in self.rotateView(targetView: targetView, duration: duration) } } 

我会把它放在像rotateImage()这样的函数中,在完成代码中再次调用rotateImage() 。 不过,我认为你应该使用M_PI(或相当于swift)作为旋转量。

使用此扩展名将UIImageView旋转360度。

 extension UIView { func rotate360Degrees(duration: CFTimeInterval = 1.0, completionDelegate: AnyObject? = nil) { let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation") rotateAnimation.fromValue = 0.0 rotateAnimation.toValue = CGFloat(M_PI) rotateAnimation.duration = duration if let delegate: CAAnimationDelegate = completionDelegate as! CAAnimationDelegate? { rotateAnimation.delegate = delegate } self.layer.addAnimation(rotateAnimation, forKey: nil) } } 

比旋转UIImageView简单地使用这个方法

 self.YOUR_SUBVIEW.rotate360Degrees() 

这个在Swift 2.2中为我工作:

 let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z") rotationAnimation.fromValue = 0.0 rotationAnimation.toValue = 360 * CGFloat(M_PI/180) let innerAnimationDuration : CGFloat = 1.0 rotationAnimation.duration = Double(innerAnimationDuration) rotationAnimation.repeatCount = HUGE self.imageView.addAnimation(rotationAnimation, forKey: "rotateInner") 

这是Swift 3.0代码

  let imgViewRing = UIImageView(image: UIImage(named: "apple")) imgViewRing.frame = CGRect(x: 0, y: 0, width: UIImage(named: "apple")!.size.width, height: UIImage(named: "apple")!.size.height) imgViewRing.center = CGPoint(x: self.view.frame.size.width/2.0, y: self.view.frame.size.height/2.0) rotateAnimation(imageView: imgViewRing) self.view.addSubview(imgViewRing) 

这是animation逻辑

 func rotateAnimation(imageView:UIImageView,duration: CFTimeInterval = 2.0) { let rotateAnimation = CABasicAnimation(keyPath: "transform.rotation") rotateAnimation.fromValue = 0.0 rotateAnimation.toValue = CGFloat(.pi * 2.0) rotateAnimation.duration = duration rotateAnimation.repeatCount = Float.greatestFiniteMagnitude; imageView.layer.add(rotateAnimation, forKey: nil) } 

更新了Swift 3:我做了UIView的扩展,并在其中包含以下function:

 func rotate(fromValue: CGFloat, toValue: CGFloat, duration: CFTimeInterval = 1.0, completionDelegate: Any? = nil) { let rotateAnimation = CABasicAnimation(keyPath: >"transform.rotation") rotateAnimation.fromValue = fromValue rotateAnimation.toValue = toValue rotateAnimation.duration = duration if let delegate: Any = completionDelegate { rotateAnimation.delegate = delegate as? CAAnimationDelegate } self.layer.add(rotateAnimation, forKey: nil) } 

然后你可以通过调用函数(在UIView中,例如我做成一个button):

 monitorButton.rotate(fromValue: 0.0, toValue: CGFloat(M_PI * 2), completionDelegate: self) 

希望这可以帮助!

尝试这一个它为我工作,我旋转一次图像

  UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveLinear, animations: { self.imgViewReload.transform = CGAffineTransformRotate(self.imgViewReload.transform, CGFloat(M_PI)) }, completion: { (value: Bool) in UIView.animateWithDuration(0.5, delay: 0.0, options: .CurveLinear, animations: { self.imgViewReload.transform = CGAffineTransformIdentity }, completion: nil) })