iOS 基础动画UIView动画CALayer动画

一. 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"];  


}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,640评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,254评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,011评论 0 355
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,755评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,774评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,610评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,352评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,257评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,717评论 1 315
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,894评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,021评论 1 350
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,735评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,354评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,936评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,054评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,224评论 3 371
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,974评论 2 355

推荐阅读更多精彩内容