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))
}