UIView、Layer、Animation
来源:互联网 发布:php面试让现场写代码 编辑:程序博客网 时间:2024/05/16 16:08
a.第一种方式:(UIViewAnimation)通过Category中的API1.告诉系统动画要 begin2.在begin之后设置改变量 setChange3.最后执行动画 commitAnimation一套动画就完成了。[UIView beginAnimations:@"animation" context:nil];
[UIView setAnimationDuration:5];
[self.animationView setOrigin:CGPointMake(200, 200)];
[UIView commitAnimations];
b.第二种方式:(UIViewAnimationWithBlocks)通过Category中的API
+ (void)animateWithDuration:(NSTimeInterval)duration animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
第二种方式相对于第一种方式更加的简单,使用block的方式进行了相关代码的回调。
c.第三种方式:(特殊的UIView子类)通过特定的API方法实现动画
UIImageView *testimage = [[UIImageView alloc] init];
[testimage setAnimationDuration:2];
[testimage setAnimationImages:imageArray];
[testimage startAnimating];
特殊的UIView子类——UIImageView具有自己的动画实现机制。按照帧的概念填充到动画数组中,自定义播放完成动画展示。
a.第一种方式:CATransaction 事务型动画设置[CATransaction begin];[CATransaction setAnimationDuration:2];
sublayer2.position=CGPointMake(150.0,150.0); //x,y轴最后位置
sublayer2.opacity=0.5; //最后透明度
[CATransaction commit];
类似于UIView层动画的第一种方式,但是由于是处理layer的动画,所以需要用layer专用的方法进行设置。
(在UIView的beginAnimation中也是可以去控制Layer去进行动画,不过会有不可靠的设置,比如:无法确定动画的播放时间)
b.第二种方式:CABasicAnimation 核心基础动画(通过修改相关的属性进行动画的设置)
CABasicAnimation* fadeAnimation = [CABasicAnimation animationWithKeyPath:@”opacity”; fadeAnimation.fromValue = [NSNumber numberWithFloat:1.0]; fadeAnimation.toValue = [NSNumber numberWithFloat:0.0]; fadeAnimation.duration = 1.0; [theLayer addAnimation:fadeAnimation forKey:nil]; //改变图层实际的最后数据值 theLayer.opacity = 0.0; // 记得更新图层树
动画类型区分:c.第三种方式:CAKeyframeAnimation 关键帧动画(是基础动画的延伸,解决比如抖动类型的动画(无法仅仅给出两个状态就可以完成的动画))*************************************************//关键帧动画——path赋值方式实现CGMutablePathRef path = CGPathCreateMutable();
//将路径的起点定位到 (50 120)
CGPathMoveToPoint(path,NULL,50.0,120.0);
//下面5行添加5条直线的路径到path中
CGPathAddLineToPoint(path, NULL, 60, 130);
CGPathAddLineToPoint(path, NULL, 70, 140);
CGPathAddLineToPoint(path, NULL, 80, 150);
CGPathAddLineToPoint(path, NULL, 90, 160);
CGPathAddLineToPoint(path, NULL, 100, 170);
//下面四行添加四条曲线路径到path
CGPathAddCurveToPoint(path,NULL,50.0,275.0,150.0,275.0,70.0,120.0);
CGPathAddCurveToPoint(path,NULL,150.0,275.0,250.0,275.0,90.0,120.0);
CGPathAddCurveToPoint(path,NULL,250.0,275.0,350.0,275.0,110.0,120.0);
CGPathAddCurveToPoint(path,NULL,350.0,275.0,450.0,275.0,130.0,120.0);
//以“position”为关键字创建 实例
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
//设置path属性
[animation setPath:path];
[animation setDuration:3.0];
//这句代码 表示 是否动画回到原位
// [animation setAutoreverses:YES];
CFRelease(path);
[sublayer2 addAnimation:animation forKey:NULL];
*****************************************//关键帧动画——values赋值方式实现
//1.创建核心动画
CAKeyframeAnimation *keyAnima=[CAKeyframeAnimation animation];
keyAnima.keyPath=@"transform.rotation";
//设置动画时间
keyAnima.duration=5;
//设置图标抖动弧度
//把度数转换为弧度 度数/180*M_PI
keyAnima.values=@[@(M_PI/64),@(-M_PI/64),@(M_PI/64),@(-M_PI/64),@(M_PI/64),@(-M_PI/64)];
//设置动画的重复次数(设置为最大值)
keyAnima.repeatCount=1;
[keyAnima setCalculationMode:kCAAnimationLinear];
keyAnima.keyTimes = [[NSArray alloc] initWithObjects:[NSNumber numberWithFloat:0.0],[NSNumber numberWithFloat:0.45],[NSNumber numberWithFloat:0.5],[NSNumbernumberWithFloat:0.55],[NSNumber numberWithFloat:1], nil];
//2.添加动画
[sublayer2 addAnimation:keyAnima forKey:nil];
关键帧的动画模式也是类似于属性修改型动画模式。相对basicAnimation具有更多的空间(可以设置多个动画状态,以及时间)
keytimes:参数的设置并不是动画的时间,而是百分比的概念从0-1,设置间隔百分比
对于ios的动画而言,本质的表现是view属性的变化,“位置的变化”-2D(相对superView位置的变化),“自身的变化”-2D&Color(大小变化,透明度变化),“摄像机视角变化”-3D&2D(x轴旋转,y轴旋转,z轴旋转)
通过动画与自身属性的变化与否区分:
隐式动画、显式动画
1.隐式动画特点是,根据动画持有者的属性,衍生出动画。当动画结束之后,保留属性的修改。
2.显式动画特点是,同样是通过动画持有者的属性,衍生出动画。但当动画结束之后,不保留属性的修改。(就算是animation.removedOnCompletion = NO;)对于其本身的属性还是保持初始状态。
隐式动画的实现:创建一个Layer,将这个Layer添加到需要动画的View之上。对Layer中的需要动画的属性,进行设置。系统会根据属性自动添加,“过渡动画”。(系统给予过渡动画的动画时间默认是0.25秒,如果希望设置动画时间,只需要重置时间“[CATransactionsetAnimationDuration:1(希望动画所需时间)];”)
显式动画的实现:显式动画首先就需要一个动画的对象Animation!。实现这个Animation(具体是basic或是keyframe的类型可以自定义)。完成动画的配置之后,添加到需要动画的View上。则显式动画的实现就完成了。
隐式动画-更加是一种“过渡动画”的概念,为属性变化添加过渡。
显式动画-更加宽泛的动画。不是属性变化的附属品。而是独立于View动画的展现。
- UIView、Layer、Animation
- Animation-Layer
- uiview animation
- UIView(animation)
- UIView Animation
- UIView move animation
- UIView, CALayer,Core Animation
- uiview的animation动画
- UIView下 Animation
- UIView Animation(二)
- UIView animation常用用法
- UIView animateWithDuration:animation:completion
- 使用UIView的animation
- iOS UIView Animation
- UIView动画animation
- UIView的动画 Animation
- iOS之UIView Animation
- UIView layer 的对应关系
- EaselJs 测试小结
- 成都青羊考场科目二考试分享
- android 发送短信和接受短信模块
- hdu 4947 A simple dynamic programming problem(2014 Multi-University Training Contest 10)
- hdu 4970 Killing Monster
- UIView、Layer、Animation
- 二分图的最大匹配————匈牙利算法(hungary)基础详解。
- 国际化与本地化(i18n)支持函数库(多语言)
- net spy memcached 使用demo
- 机房收费系统(1)之结账
- Redis客户端之Jedis
- 网络报文转发逻辑
- Oracle 循环学习
- [IOS]UIViewController的生命周期及iOS程序执行顺序