一. UIView 动画
动画的属性:
frame: 视图框架
center: 视图的位置
alpha: 视图的透明度
bounds: 视图的大小
transform: 视图的装换
backgroundColor: 背景色
1.UIView的设置方法
//开始动画设置 参数1:标识符(名字) 参数2:携带的参数
[UIView beginAnimations:@"UIView" context:nil];
// 设置动画持续时间
[UIView setAnimationDuration:3];
// 设置动画延迟, 延迟多少秒开始
[UIView setAnimationDelay:0];
// 设置动画的速度曲线
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn]
// 设置反转
[UIView setAnimationRepeatAutoreverses:YES];
// 设置动画循环次数
[UIView setAnimationRepeatCount:1];
// 设置动画的代理
[UIView setAnimationDelegate:self];
//设置动画开始的代理方法
[UIView setAnimationWillStartSelector:@selector(willStart)];
//设置动画结束的代理方法
[UIView setAnimationWillEndSelector:@selector(willEnd)];
// 动画从当前状态继续执行
[UIView setAnimationBeginsFromCurrentState:YES];
// 动画提交
[UIView commitAnimations];
2.UIView 动画的两种 block方法:
2.1 简单式的block
[UIView animateWithDuration:1 animations:^{
// 执行的动画
_imageView.center = CGPointMake(100, 200);
}];
2.2 该 block 动画结束后可以做需要的操作
// action: block 动画结束后该做的事
[UIView animateWithDuration:1 animations:^{
// 如果设置了反转属性,那么在动画结束后不用在另行添加动画效果了
[UIView setAnimationRepeatAutoreverses:YES];
// 这里写执行的动画
} completion:^(BOOL finished) {
// 上面动画结束触发(相当于 代理方法中的完成动画的方法)
// 还可以继续添加其他的动画
[UIView animateWithDuration:1 animations:^{
} completion:^(BOOL finished) {
}];
action; }];
CGAffineTransform2D仿射变换
1>平移(2D仿射变换方法)
[UIView animateWithDuration:1 animations:^{
// 如果设置了反转属性,那么在动画结束后不用在另行添加动画效果了
[UIView setAnimationRepeatAutoreverses:YES];
// 执行的动画
// 平移(2D仿射变换方法)
// transform 形变属性
// self.imageView:要形变的 View
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, 100, 0);
} completion:^(BOOL finished) {
// 上面动画结束触发(相当于 代理方法中的完成动画的方法)
// 复原位置
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, -100, 0);
}];
2> 缩放(2D仿射变换方法)
[UIView animateWithDuration:1 animations:^{
// 反转
[UIView setAnimationRepeatAutoreverses:YES];
// 缩放(2D仿射变换方法)
// 参数2/3 是缩放的比例
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, 2, 2);
} completion:^(BOOL finished) {
self.imageView.transform = _transform;
}];
3> 旋转(2D仿射变换方法)
// 需求:
// 点击按钮,开始旋转,再点停止转动
[UIView animateWithDuration:0.1 animations:^{
// 旋转
// 参数二:旋转角度
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI_2);
} completion:^(BOOL finished) {
// 结束时调用旋转方法
[self rotateAnimation];
}];
_isRuning = !_isRuning;
}
// 循环转
- (void)rotateAnimation
{
if (_isRuning == YES) {
[UIView animateWithDuration:0.1 animations:^{
// 旋转
// 参数二:旋转角度
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI_2);
// 给旋转的初值
self.isRuning = YES;
} completion:^(BOOL finished) {
// 结束时调用旋转方法
[self rotateAnimation];
}];
}
}
二. CALayer 动画
CALayer层的属性
// layer 是负责显示图层的
// 要更改看到的形状,需要更改 layer
// 设置圆角
// 变圆的先决条件必须是长宽相同
self.myView = [[UIView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
self.myView.layer.cornerRadius = self.myView.frame.size.width / 2;
// 设置阴影的颜色
// CGColorRef 是涂层绘制的颜色
self.myView.layer.shadowColor = [UIColor yellowColor].CGColor;
// 设置阴影的显示范围
self.myView.layer.shadowOffset = CGSizeMake(10, 10);
// 阴影透明度
self.myView.layer.shadowOpacity = 1;
// 模糊程度
self.myView.layer.shadowRadius = 50;
// 设置边框
self.myView.layer.borderWidth = 5;
// 设置边框的颜色
self.myView.layer.borderColor = [UIColor redColor].CGColor;
1>CABasicAnimation 基础动画
// 旋转
- (void)xyAnimation
{
// 创建一个基础动画
// 注意: KeyPath 一定不要拼错
// 要更改的是 transform.rotation.x
// 形变属性下 弧度的点 X 轴
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
// 设置属性变化 到什么值
// toValue 需要一个对象类型,即 NSNumber 或 NSValue
animation.toValue = [NSNumber numberWithFloat:M_PI];
// 设置动画时间
animation.duration = 1;
// 设置动画重复
animation.repeatCount = 2;
// 把设置好的动画添加到 layer上
// 参数2:添加的动画标识
[self.myView.layer addAnimation:animation forKey:@"transform.rotation.x"];
}
2>// 改变 size
- (void)sizeAnimation
{
// 更改大小的话,需要更改 bounds.size
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
// 设置改变的值
// fromValue 初始值
animation.fromValue = [NSValue valueWithCGSize:CGSizeMake(10, 10)];
// toValue 结束值
animation.toValue = [NSValue valueWithCGSize:CGSizeMake(100, 100)];
// 设置时间
animation.duration = 1;
// 添加到 layer 上
[self.myView.layer addAnimation:animation forKey:@"bounds.size"];
}
3> 3D旋转
- (void)transform3D
{
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
// 结束值
animation.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(self.myView.layer.transform, M_PI, 10, 20, 20)];
animation.duration = 1;
[self.myView.layer addAnimation:animation forKey:@"transform"];
}
CAKeyframeAnimation 类似按轨迹移动, 位置, 执行一组动画的组动画
1>// 改变颜色
- (void)changeBackGroundColor
{
// 可以执行一组动画
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
// 创建绘制颜色
CGColorRef color1 = [UIColor orangeColor].CGColor;
CGColorRef color2 = [UIColor lightGrayColor].CGColor;
CGColorRef color3 = [UIColor greenColor].CGColor;
CGColorRef color4 = [UIColor blueColor].CGColor;
// 改变一组颜色
animation.values = @[(id)color1, (id)color2, (id)color3, (id)color4];
// 设置时间
animation.duration = 2;
// 添加到 layer 上
[self.myView.layer addAnimation:animation forKey:@"backgroundColor"];
}
2> 轨迹移动
- (void)positionPoint
{
// NSLog(@"%@", NSStringFromCGPoint(self.myView.layer.position));
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
// 创建一堆点
NSValue *point1 = [NSValue valueWithCGPoint:CGPointMake(100, 200)];
NSValue *point2 = [NSValue valueWithCGPoint:CGPointMake(200, 200)];
NSValue *point3 = [NSValue valueWithCGPoint:CGPointMake(100, 300)];
NSValue *point4 = [NSValue valueWithCGPoint:CGPointMake(200, 300)];
NSValue *point5 = [NSValue valueWithCGPoint:CGPointMake(100, 400)];
// 添加点进数组
animation.values = @[point1, point2, point3, point4, point5];
// 设置时间
animation.duration = 1;
// 添加到 layer上
[self.myView.layer addAnimation:animation forKey:@"position"];
}
3> 左右晃动
- (void)positionX
{
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
CGFloat center =self.myView.layer.position.x;
CGFloat left = center -100;
CGFloat right = center +100;
NSNumber *r = [NSNumber numberWithFloat:right];
NSNumber *c = [NSNumber numberWithFloat:center];
NSNumber *l = [NSNumber numberWithFloat:left];
animation.values = @[r, c, l, c];
// 设置时间
animation.duration = 0.5;
// 设置重复次数
animation.repeatCount = 1000;
// 添加到 layer上
[self.myView.layer addAnimation:animation forKey:@"position.x"];
}
3>组动画
- (void)groupAnimation
{
// 创建组动画
CAAnimationGroup *group = [CAAnimationGroup animation];
/***********改变颜色************/
// 改变颜色
// 可以执行一组动画
CAKeyframeAnimation *animation1 = [CAKeyframeAnimation animationWithKeyPath:@"backgroundColor"];
// 创建绘制颜色
CGColorRef color1 = [UIColor orangeColor].CGColor;
CGColorRef color2 = [UIColor lightGrayColor].CGColor;
CGColorRef color3 = [UIColor greenColor].CGColor;
CGColorRef color4 = [UIColor blueColor].CGColor;
// 改变一组颜色
animation1.values = @[(id)color1, (id)color2, (id)color3, (id)color4];
/*********改变大小**********/
// 更改大小的话,需要更改 bounds.size
CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
// 设置改变的值
// fromValue 初始值
animation2.fromValue = [NSValue valueWithCGSize:CGSizeMake(10, 10)];
// toValue 结束值
animation2.toValue = [NSValue valueWithCGSize:CGSizeMake(100, 100)];
// 设置组动画时间
group.duration = 3;
// 设置组动画执行的动画数组
group.animations = @[animation1, animation2];
// 添加到 layer 上
[self.myView.layer addAnimation:group forKey:@"group"];
}