网上搜索旋转动画的时候看到一篇OC的博客的实现,因为我是Swift项目,用Swift改写下为后面用做准备吧。
代码如下:
import Foundation
import UIKit
//设置逆时针和顺时针旋转的枚举
enum Direction {
case DirectionReight
case DirectionLeft
}
class Animation: NSObject {
func setAnimation(control: UIView, direction: Direction) {
// y是根据y轴旋转,x是根据x轴旋转,z是根据z轴旋转
// 如果是y, 则transform.rotation.y,如果是x,则transform.rotation.x, 如果是z,则transform.rotation.z
let rotationAnimation: CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
// 这里 M_PI * 2.0 为顺时针旋转,如果想要逆时针旋转可以写成 M_PI * -2.0
if(direction == .DirectionReight) {
rotationAnimation.toValue = NSNumber(value: Double.pi * 2.0)
} else {
rotationAnimation.toValue = NSNumber(value: Double.pi * -2.0)
}
rotationAnimation.duration = 2.0
rotationAnimation.isRemovedOnCompletion = false
rotationAnimation.repeatCount = MAXFLOAT
rotationAnimation.fillMode = .forwards
control.layer.add(rotationAnimation, forKey: "rotationAnimation")
}
}
使用如下:
Animation().setAnimation(control: roundView, direction: .DirectionReight)
移除动画:
roundView.layer.removeAllAnimations()
网友评论