Swift3.0写--2.左右晃动
func shakeAnimation(){
//移除self.mineTopView.layer上的所有动画,可以避免多次重复添加
self.mineTopView.layer.removeAllAnimations()
let momAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
momAnimation.fromValue = NSNumber(value: -0.1) //左幅度
momAnimation.toValue = NSNumber(value: 0.1) //右幅度
momAnimation.duration = 0.1
momAnimation.repeatCount = HUGE //无限重复
momAnimation.autoreverses = true //动画结束时执行逆动画
self.momAnimation.isRemovedOnCompletion = false //切出此界面再回来动画不会停止
self.momAnimation.delegate = self//CAAnimationDelegate 代理中有动画的开始和结束
self.mineTopView.layer.add(momAnimation, forKey: "centerLayer")
}
//旋转动画
func rorateAnimation(holdView: UIView){
let momAnimation = CABasicAnimation(keyPath: "transform.rotation.z")
momAnimation.fromValue = NSNumber(value: 0) //左幅度
momAnimation.toValue = NSNumber(value: M_PI*2) //右幅度
momAnimation.duration = 1
momAnimation.repeatCount = HUGE //无限重复
holdView.layer.add(momAnimation, forKey: "centerLayer")
}
1.缩放
- (void)Animation
{
// 设定为缩放
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
// 动画选项设定
animation.duration = 2.0; // 动画持续时间
animation.repeatCount = HUGE_VALF; // 重复次数(无限)
animation.autoreverses = YES; // 动画结束时执行逆动画
// 缩放倍数
animation.fromValue = [NSNumber numberWithFloat:0.9]; // 开始时的倍率
animation.toValue = [NSNumber numberWithFloat:1.1]; // 结束时的倍率
// 添加动画
[self.moveView.layer addAnimation:animation forKey:@"scale-layer"];
}
2.左右晃动
- (void)animation
{
//z轴
CABasicAnimation *momAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
momAnimation.fromValue = [NSNumber numberWithFloat:-0.1];//左幅度
momAnimation.toValue = [NSNumber numberWithFloat:0.1];//右幅度
momAnimation.duration = 1.5;
momAnimation.repeatCount = CGFLOAT_MAX;//无限重复
momAnimation.autoreverses = YES;//动画结束时执行逆动画
momAnimation.delegate = self;
[self.moveView.layer addAnimation:momAnimation forKey:@"centerLayer"];
}
3.翻转动画
- (void)transitionAnimation
{
CGContextRef context=UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//可选
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.moveView cache:YES];//UIViewAnimationTransitionCurlUp五种翻转效果可选
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
4.循环动画
[UIView animateWithDuration:timer animations:^{
//执行
} completion:^(BOOL finished) {
//结束后再执行(就是不知无限循环有多大危害)
}];