Swift
// AMRK: 开始旋转
private func startRotation(sender : UIButton,back: Bool) {
// 动画时间内 关闭用户交互
sender.userInteractionEnabled = false;
// 创建 并 设置 修改的 参数
let rotationAnimation = CABasicAnimation(keyPath: "transform.rotation.z");
if back {
// 旋转角度
rotationAnimation.toValue = M_PI * -0.005;
}else {
// 旋转角度
rotationAnimation.toValue = M_PI * 0.25;
}
// 旋转持续时间
rotationAnimation.duration = 0.25;
// 执行 次数
//rotationAnimation.repeatCount = 100;
// 结束动画不回原处
rotationAnimation.fillMode = kCAFillModeForwards;
// 动画完成后 移除
rotationAnimation.removedOnCompletion = false;
//对谁做动画 就添加动画
sender.layer.addAnimation(rotationAnimation, forKey: nil);
// 动画结束 打开用户交互
let delay = dispatch_time(DISPATCH_TIME_NOW,
Int64(0.25 * Double(NSEC_PER_SEC)))
dispatch_after(delay, dispatch_get_main_queue()) {
sender.userInteractionEnabled = true;
}
}
OC
// 创建 并 设置 修改的 参数
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
// 旋转角度
rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 0.25];
// 旋转持续时间
rotationAnimation.duration = 0.25;
// 执行 次数
rotationAnimation.repeatCount = 100;
// 结束动画不回原处
rotationAnimation.fillMode = kCAFillModeForwards;
// 动画完成后 移除
rotationAnimation.removedOnCompletion = false;
//对谁做动画 就添加动画
[sender.layer addAnimation:rotationAnimation forKey:nil];
// 移除动画
[sender.layer removeAllAnimations];