图片旋转动画
// 创建动画
let anim = CABasicAnimation(keyPath: "transform.rotation")
// 设置动画属性
anim.toValue = 2 * M_PI
anim.repeatCount = MAXFLOAT
anim.duration = 10
// 这个属性很重要 如果不设置当页面运行到后台再次进入该页面的时候 动画会停止
anim.isRemovedOnCompletion = false
要转动的控件名称.layer.add(anim, forKey: nil)
Swift3.0为视图添加旋转动画_CABasicAnimation
Swift2.3:
//创建旋转动画
let anim = CABasicAnimation(keyPath: "transform.rotation")
//旋转角度
anim.toValue = 1 * M_PI
//旋转指定角度需要的时间
anim.duration = 1
//旋转重复次数
anim.repeatCount = MAXFLOAT
//动画执行完后不移除
anim.removedOnCompletion = true
//将动画添加到视图的laye上
view.layer.addAnimation(anim, forKey: nil)
//取消动画
view.layer.removeAllAnimations()
//这个是旋转方向的动画
UIView.animateWithDuration(0.2) { () -> Void in
//指定旋转角度是180°
view.transform = CGAffineTransformRotate(view.transform, CGFloat(M_PI))
}
Swift3.0:
let anim = CABasicAnimation(keyPath: "transform.rotation")
anim.toValue = 1 * M_PI
anim.duration = 1
anim.repeatCount = MAXFLOAT
anim.isRemovedOnCompletion = true
view1.layer.add(anim, forKey: nil)
view1.layer.removeAllAnimations()
UIView.animate(withDuration: 0.2) {
view1.transform = view1.transform.rotated(by: CGFloat(M_PI))
}