给imageView 添加分类,然后直接调用
//开始旋转
imageView. rotate360DegreeWithImageView()
//停止旋转
imageView.stopRotate()
/// imageView 旋转
///
/// - Parameters:
/// - duration: 周期 (控制速度:2--10)
/// - repeatCount: 次数
func rotate360DegreeWithImageView(duration:CFTimeInterval , repeatCount :Float ) {
//让其在z轴旋转
let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
//旋转角度
rotationAnimation.toValue = NSNumber(value: Double.pi * 2.0 )
//旋转周期
rotationAnimation.duration = duration;
//旋转累加角度
rotationAnimation.isCumulative = true;
//旋转次数
rotationAnimation.repeatCount = repeatCount;
self.layer .add(rotationAnimation, forKey: "rotationAnimation")
}
//停止旋转
func stopRotate() {
self.layer.removeAllAnimations()
}
网友评论