給imageView 添加分類,然后直接調(diào)用
//開始旋轉(zhuǎn)
imageView. rotate360DegreeWithImageView()
//停止旋轉(zhuǎn)
imageView.stopRotate()
/// imageView 旋轉(zhuǎn)
///
/// - Parameters:
/// - duration: 周期 (控制速度:2--10)
/// - repeatCount: 次數(shù)
func rotate360DegreeWithImageView(duration:CFTimeInterval , repeatCount :Float ) {
//讓其在z軸旋轉(zhuǎn)
let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
//旋轉(zhuǎn)角度
rotationAnimation.toValue = NSNumber(value: Double.pi * 2.0 )
//旋轉(zhuǎn)周期
rotationAnimation.duration = duration;
//旋轉(zhuǎn)累加角度
rotationAnimation.isCumulative = true;
//旋轉(zhuǎn)次數(shù)
rotationAnimation.repeatCount = repeatCount;
self.layer .add(rotationAnimation, forKey: "rotationAnimation")
}
//停止旋轉(zhuǎn)
func stopRotate() {
self.layer.removeAllAnimations()
}