今天主要写的是动画类的操作,CAAnimation的两个子类的运用,一些动画的产生。
1.CABasicAnimation的属性,方法的使用
2.CAKeyframeAnimation的属性,方法的使用
3.CAAnimationGroup组的使用
代码部分:
属性
@property (weak, nonatomic) IBOutlet UIImageView *myImageView;
1.翻转属性
- (void)overturn{
//创建动画对象,并且设置动画的类型 --这里是rotation翻转.z是通过Z轴进行翻转。
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
//持续时间
animation.duration = 3;
//这里从开始位置进行动画,在结束时回到开始位置。
//开始位置
animation.fromValue = @0;
//结束位置:转换成对象
animation.toValue = @(M_PI*2/3);
//重复次数 最大数:(MAXFLOAT)一直旋转
animation.repeatCount = MAXFLOAT;
//重复时间(优先级高于重复次数)(单位:s)
animation.repeatDuration = 3;
//是否移除结果(不复原)
animation.removedOnCompletion = NO;
/*
kCAFillModeRemoved 这个是默认值,动画结束后,会恢复到之前的状态
kCAFillModeForwards 当动画结束后,会一直保持着动画最后的状态
kCAFillModeBackwards 在动画开始前,便立即进入动画的初始状态并等待动画开始。
kCAFillModeBoth 这个其实就是上面两个的合成.保持动画最后的状态
*/
animation.fillMode = kCAFillModeForwards;
//给imageView添加动画,forkey是一个标识
[self.myImageView.layer addAnimation:animation forKey:@"rotationAnimation"];
}
2.缩放属性
- (void)scaleAnimation{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.duration = 3;
//缩放倍数
animation.fromValue = @1;
animation.toValue = @0.5;
animation.repeatDuration = 3;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
//forkey是一个标识
[self.myImageView.layer addAnimation:animation forKey:@"scaleAnimation"];
}
3.路径绘制
拿贝塞尔曲线举例:
//绘制一个贝塞尔曲线的路径
-(void)bezierCurve{
//通过关键帧的动画来创建
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//设置时间
animation.duration = 3;
//创建一个路径
CGMutablePathRef path = CGPathCreateMutable();
//1.创建起点
CGPoint startPoint = self.view.center;
CGPathMoveToPoint(path, nil, startPoint.x,startPoint.y);
//2.创建各个切线
CGPoint point1 = CGPointMake(20,300);
CGPoint point2 = CGPointMake(50, 400);
CGPoint point3 = CGPointMake(100, 600);
CGPathAddCurveToPoint(path, nil, point1.x, point1.y, point2.x, point2.y, point3.x, point3.y);
//3.将路径添加进去
animation.path = path;
//不还原
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
//
[self.myImageView.layer addAnimation:animation forKey:@"bezierCurve"];
//手动释放路径
CGPathRelease(path);
}
]
//使劲的晃(其实就是翻转--)
-(void)ratationWaggle{
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
//持续时间
animation.duration = 0.1;
//无限晃动
animation.repeatCount = MAXFLOAT;
//设置一个数组,元素:旋转的角度
CGFloat angle = M_PI/20;
//0 - 左摇-0-右摇-0
animation.values = @[@0,@(-angle),@0,@(angle),@0];
[self.myImageView.layer addAnimation:animation forKey:@"waggle"];
}
5.组的运用---合成版
//在点击事件中进行操作
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//1.翻转动画
// [self overturn];
//2.缩放动画
// [self scaleAnimation];
//3.贝塞尔曲线路径动画
// [self bezierCurve];
//4.左右晃动
// [self ratationWaggle];
CAAnimationGroup *group = [CAAnimationGroup animation];
CAAnimation *animation1 = [self scaleAnimation];
CAAnimation *animation2 = [self bezierCurve];
CAAnimation *animation3 = [self ratationWaggle];
group.animations = @[animation1,animation2,animation3];
group.duration = 3;
group.repeatCount = MAXFLOAT;
[self.myImageView.layer addAnimation:group forKey:@"group"];
}
- (CAAnimation *)overturn{
//创建动画对象,并且设置动画的类型 --这里是rotation翻转.z是通过Z轴进行翻转。
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
//持续时间
animation.duration = 3;
//这里从开始位置进行动画,在结束时回到开始位置。
//开始位置
animation.fromValue = @0;
//结束位置:转换成对象
animation.toValue = @(M_PI*2/3);
//重复次数 最大数:(MAXFLOAT)一直旋转
animation.repeatCount = MAXFLOAT;
//重复时间(优先级高于重复次数)(单位:s)
animation.repeatDuration = 3;
//是否移除结果(不复原)
animation.removedOnCompletion = NO;
/*
kCAFillModeRemoved 这个是默认值,动画结束后,会恢复到之前的状态
kCAFillModeForwards 当动画结束后,会一直保持着动画最后的状态
kCAFillModeBackwards 在动画开始前,便立即进入动画的初始状态并等待动画开始。
kCAFillModeBoth 这个其实就是上面两个的合成.保持动画最后的状态
*/
animation.fillMode = kCAFillModeForwards;
//给imageView添加动画,forkey是一个标识
[self.myImageView.layer addAnimation:animation forKey:@"rotationAnimation"];
return animation;
}
- (CAAnimation*)scaleAnimation{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
animation.duration = 3;
//缩放倍数
animation.fromValue = @1;
animation.toValue = @0.5;
animation.repeatDuration = 3;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
//forkey是一个标识
// [self.myImageView.layer addAnimation:animation forKey:@"scaleAnimation"];
return animation;
}
//绘制一个贝塞尔曲线的路径
- (CAAnimation *)bezierCurve{
//通过关键帧的动画来创建
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//设置时间
animation.duration = 3;
//创建一个路径
CGMutablePathRef path = CGPathCreateMutable();
//1.创建起点
CGPoint startPoint = self.view.center;
CGPathMoveToPoint(path, nil, startPoint.x,startPoint.y);
//2.创建各个切线
CGPoint point1 = CGPointMake(20,300);
CGPoint point2 = CGPointMake(50, 400);
CGPoint point3 = CGPointMake(100, 600);
CGPathAddCurveToPoint(path, nil, point1.x, point1.y, point2.x, point2.y, point3.x, point3.y);
//3.将路径添加进去
animation.path = path;
//不还原
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
//
[self.myImageView.layer addAnimation:animation forKey:@"bezierCurve"];
//手动释放路径
CGPathRelease(path);
return animation;
}
-(CAAnimation *)ratationWaggle{
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];
//持续时间
animation.duration = 0.1;
//无限晃动
animation.repeatCount = MAXFLOAT;
//设置一个数组,元素:旋转的角度
CGFloat angle = M_PI/20;
//0 - 左摇-0-右摇-0
animation.values = @[@0,@(-angle),@0,@(angle),@0];
// [self.myImageView.layer addAnimation:animation forKey:@"waggle"];
return animation;
}