IOS-动画Animation

iOS Animation详解

本篇只要讲解iOS中动画的使用. Animtion主要分为两类:UIView动画和CoreAnimation动画。 UIView动画有UIView属性动画,UIViewBlock动画,UIViewTransition动画。 而CoreAnimation动画主要通过CAAnimation和CALayer,常用的有CABasicAnimation,CAKeyframeAnimation,CATransition,CAAnimationGroup.

UIView动画

UIView属性动画

CGRect viewRect = CGRectMake(10,10,200,200);self.myView= [[UIView alloc] initWithFrame:viewRect];= [UIColor whiteColor];[];//1 准备动画//参数1: 动画的作用, 任意字符串,用来区分多个动画, 参数二: 传递参数用 nil:OC中使用[UIView beginAnimations:@”changeSizeAndColor” context:nil];//在准备动画的时候可以设置动画的属性[UIView setAnimationDuration:0.7];[UIView setAnimationDelegate:self];[UIView setAnimationWillStartSelector:@selector(startAnimation)];[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];//动画的曲线[UIView setAnimationRepeatCount:2];[= CGRectMake(110, 100, 100, 100);= [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:0.5];//3 提交并执行动画[UIView commitAnimations];Block动画

Block动画的实质是对UIView动画的封装

第一种

[UIView animateWithDuration:2 animations:^{= [UIColor orangeColor];}];

第二种

[UIView animateWithDuration:2 animations:^{= [UIColor orangeColor];} completion:^(BOOL finished) {//finished判断动画是否完成if (finished) {NSLog(@”finished”);}}];

第三种

[UIView animateWithDuration:2 delay:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{= [UIColor orangeColor];} completion:^(BOOL finished) {//finished判断动画是否完成if (finished) {NSLog(@”finished”);}}];

下面是AnimationOptionCurve参数:

UIViewAnimationOptionTransitionNone= 0 << 20, // defaultUIViewAnimationOptionTransitionFlipFromLeft = 1 << 20,UIViewAnimationOptionTransitionFlipFromRight = 2 << 20,UIViewAnimationOptionTransitionCurlUp= 3 << 20,UIViewAnimationOptionTransitionCurlDown= 4 << 20,UIViewAnimationOptionTransitionCrossDissolve = 5 << 20,UIViewAnimationOptionTransitionFlipFromTop= 6 << 20,UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20,CoreAnimation动画

Core Animation是一组非常强大的动画处理API,使用它能做出非常绚丽的动画效果,而且往往是事半功倍,使用它需要添加QuartzCore .framework和引入对应的框架

CABasicAnimation基本动画

CABasicAnimation基本动画没有真正的修改属性值

初始化UIView的layer

CGRect viewRect = CGRectMake(50,100,200,200);self.myView= [[UIView alloc] initWithFrame:viewRect];self.myView.backgroundColor = [UIColor whiteColor];self= 100;//设置圆角, 参数是内切圆的半径, 若想画一个圆, 前提是view必须是正方形, 参数应该是view边长的一半self= 5;//设置描边的宽度self= [UIColor orangeColor].CGColor;//设置描边的颜色(UIView上的颜色使用的是UIColor, CALayer上使用的颜色是CGColor)self= CGSizeMake(50, 100);//设置阴影的偏移量 width影响水平偏移(正右负左), height影响垂直偏移(正下负上)self= [UIColor redColor].CGColor;//阴影的偏移的颜色self= 0.5;//阴影的不透明度, 取值范围(0 ~ 1), 默认是0, 就是透明的[self.view addSubview:self.myView];

动画方法:

//1 创建并指定要修改的属性// KeyPath:CAlayer的属性名, 不是所有的属性都可以, 只有在头文件中出现animatable的属性才可以, 可以修改属性的属性, 例如:bounds.sizeCABasicAnimation *basic = [CABasicAnimation animationWithKeyPath:@”bounds”];[basic setDuration:0.7];//2 修改属性值basic, self)];basic.toValue = [NSValue valueWithCGRect:CGRectMake(0, 0, 300, 300)];//3 添加动画//key做区分动画用[self.myView.layer addAnimation:basic forKey:@”changColor”];

CAKeyframeAnimation关键帧动画

没有伞的孩子必须努力奔跑!

IOS-动画Animation

相关文章:

你感兴趣的文章:

标签云: