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

Ces*_*are 31 ios swift

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

UIView.animate(withDuration: 2, delay: 0, options: [.repeat], animations: {
    self.view.transform = CGAffineTransform(rotationAngle: 6.28318530717959)
}, completion: nil)
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

Cra*_*lot 41

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

更新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)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这不是一个流畅的动画.每次动画重复时都会有轻微的打嗝. (3认同)

Har*_*kar 27

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)
Run Code Online (Sandbox Code Playgroud)

这是动画逻辑

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 = .greatestFiniteMagnitude

        imageView.layer.add(rotateAnimation, forKey: nil)
    }
Run Code Online (Sandbox Code Playgroud)

您可以在此链接中查看输出

  • 而不是**M_PI**,你可以使用**CGFloat.pi**,我认为它看起来更多***很好*** (3认同)

Saq*_*mer 7

使用此扩展名将旋转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)
}
}
Run Code Online (Sandbox Code Playgroud)

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

self.YOUR_SUBVIEW.rotate360Degrees()
Run Code Online (Sandbox Code Playgroud)

  • 在swift 3中:有一个问题:无法指定'AnyObject'类型的值来键入'CAAnimationDelegate?' 修复它:切换这个`如果让委托:AnyObject = completionDelegate`由这个`如果让委托:CAAnimationDelegate = completionDelegate as!CAAnimationDelegate?` (3认同)

sar*_*aut 6

这个在 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")
Run Code Online (Sandbox Code Playgroud)

  • `0 * CGFloat(M_PI/180)` 天才 0 次任何事情总是会导致 0。 (4认同)

nan*_*d59 5

我会把它粘在一个函数中,rotateImage()并在完成代码中rotateImage()再次调用.不过,我认为你应该使用M_PI(或快速等效物)作为旋转量.

  • 如果要调用完成例程,则不需要重复选项.用`UIViewAnimationOptions.CurveLinear替换`UIViewAnimationOptions.Repeat` (4认同)