(笔记)动画
来源:互联网 发布:北京亚信数据有限公司 编辑:程序博客网 时间:2024/04/28 22:50
(一)使用UIIamge创建动画
- 不需要手动释放内存
- animatedImageNamed方法,会自动去找文件名以ship-anim开头的图片,一一加载
- 局限性比较大。
UIImage*img = [UIImageanimatedImageNamed:@"ship-anim"duration:1];
self.flyImages.image = img;
(二)使用UIIamgeView创建动画
- 执行完动画需要手动释放内存。
- 释放图片
- [self.tomImgsperformSelector:@selector(setAnimationImages:)withObject:nilafterDelay:self.tomImgs.animationDuration];
(三)系统动画 UIView
- animation方法的option选项:执行动画的方式
- 1.常规动画属性设置(可以同时选择多个进行设置)
- UIViewAnimationOptionLayoutSubviews //动画过程中保证子视图跟随运动。
- UIViewAnimationOptionAllowUserInteraction //动画过程中允许用户交互。
- UIViewAnimationOptionBeginFromCurrentState //所有视图从当前状态开始运行。
- UIViewAnimationOptionRepeat//重复运行动画。
- UIViewAnimationOptionAutoreverse //动画运行到结束点后仍然以动画方式回到初始点。
- UIViewAnimationOptionOverrideInheritedDuration //忽略嵌套动画时间设置。
- UIViewAnimationOptionOverrideInheritedCurve //忽略嵌套动画速度设置。
- UIViewAnimationOptionAllowAnimatedContent //动画过程中重绘视图(注意仅仅适用于转场动画)。
- UIViewAnimationOptionShowHideTransitionViews //视图切换时直接隐藏旧视图、显示新视图,而不是将旧视图从父视图移除(仅仅适用于转场动画)
- UIViewAnimationOptionOverrideInheritedOptions //不继承父动画设置或动画类型。
- 2.动画速度控制(可从其中选择一个设置)
- UIViewAnimationOptionCurveEaseInOut //动画先缓慢,然后逐渐加速。
- UIViewAnimationOptionCurveEaseIn //动画逐渐变慢。
- UIViewAnimationOptionCurveEaseOut //动画逐渐加速。
- UIViewAnimationOptionCurveLinear //动画匀速执行,默认值。
- 3.转场类型(仅适用于转场动画设置,可以从中选择一个进行设置,基本动画、关键帧动画不需要设置)
- 对应的方法:
- [UIView transitionWithView:待变化的场景duration:动画时间options:转场动画枚举animations:block动画代码 completion:nil];
- UIViewAnimationOptionTransitionNone //没有转场动画效果。
- UIViewAnimationOptionTransitionFlipFromLeft //从左侧翻转效果。
- UIViewAnimationOptionTransitionFlipFromRight //从右侧翻转效果。
- UIViewAnimationOptionTransitionCurlUp //向后翻页的动画过渡效果。
- UIViewAnimationOptionTransitionCurlDown //向前翻页的动画过渡效果。
- UIViewAnimationOptionTransitionCrossDissolve //旧视图溶解消失显示下一个新视图的效果。
- UIViewAnimationOptionTransitionFlipFromTop //从上方翻转效果。
- UIViewAnimationOptionTransitionFlipFromBottom //从底部翻转效果。
(四)为程序添加音频
- step1:添加库文件 —— AVFoundation.framework。
- step2:将音频拖入工程,引入AVFoundation头文件
- step3:创建音频对象
NSString* path = [[NSBundlemainBundle]pathForResource:@"frxz"ofType:@"mp3"];
NSURL*url = [NSURLfileURLWithPath:path];
//为了延迟音频对象释放的时间,使用属性(强引用)牵制着它
self.avplay= [AVPlayerplayerWithURL:url];
//播放
[self.avplayplay];
(五)Core Animation层面的动画
1.CALayer层(显示的基础)
- UIView的显示功能是依赖底层CALayer实现的
- 每一个UIView包含一个CALayer对象,修改CALayer会影响表现出来的UIView的外观
- UIView可以响应用户事件,是因为继承了UIResponder,而CALayer不可以响应用户事件。
2.Layer的使用
- 重要属性:
- bounds 大小
- position 位置
- anchorPoint 锚点
- 创建新Layer,并将创建好的layer添加到其他layer中
- Layer的种类
- 图片 CALayer
- 给contents属性赋值
- 文字 CATextLayer
- 给string属性赋值
- 绘图 CAShapeLayer
- 给path属性赋值
- 图片 CALayer
3.获取UIView中的CALayer
- 通过【self.view.layer】属性,获取
//获取myView中的CALayer属性
CALayer *layer =self.myView.layer;
layer.backgroundColor = [UIColor greenColor].CGColor;
//设置圆角的半径
layer.cornerRadius = 20;
//设置边的颜色
layer.borderColor = [UIColor redColor].CGColor;
//设置边宽度
layer.borderWidth = 3;
//一定要设置 默认是0透明看不见
layer.shadowOpacity = 1;
//设置阴影边境
layer.shadowRadius = 10;
//设置阴影颜色
layer.shadowColor = [UIColor blackColor].CGColor;
//设置阴影的偏移量
CALayer *layer =self.myView.layer;
layer.backgroundColor = [UIColor greenColor].CGColor;
//设置圆角的半径
layer.cornerRadius = 20;
//设置边的颜色
layer.borderColor = [UIColor redColor].CGColor;
//设置边宽度
layer.borderWidth = 3;
//一定要设置 默认是0透明看不见
layer.shadowOpacity = 1;
//设置阴影边境
layer.shadowRadius = 10;
//设置阴影颜色
layer.shadowColor = [UIColor blackColor].CGColor;
//设置阴影的偏移量
layer.shadowOffset=CGSizeMake(10,10);
4.圆形头像效果
效果图:
//设置imageView中的layer
self.imageView.layer.borderWidth=3;
self.imageView.layer.borderWidth=3;
self.imageView.layer.borderColor= [UIColorredColor].CGColor;
//设置圆角的半径为图片长或宽的一半
self.imageView.layer.cornerRadius=self.imageView.bounds.size.width*0.5;
//削去圆角以外的部分。
self.imageView.layer.masksToBounds=YES;
(六)添加子layer
CALayer* subLayer = [CALayerlayer];
subLayer.backgroundColor = [UIColor redColor].CGColor;
subLayer.bounds = CGRectMake(0,0,40, 40);
subLayer.backgroundColor = [UIColor redColor].CGColor;
subLayer.bounds = CGRectMake(0,0,40, 40);
//设置锚点取值范围0~1 自身的一个点。
//默认是(0.5,0.5),也就是视图的中心点
subLayer.anchorPoint=CGPointMake(0,0);
//设置位置,自身在superLayer中的位置
//即修改【锚点】在父layer中的位置
subLayer.position=CGPointMake(100,100);
//将【子layer】添加到【父layer】中
//将【子layer】添加到【父layer】中
[self.myView.layeraddSublayer:subLayer];
(七)CADisplayLink定时器
- 严格意义上讲 CADisplayLink 并不是计时器控件, 它是与显示器刷新频率一致的。比如显示的刷新频率是60赫兹, 那么 CADisplayLink 就会每秒钟执行60次。正是因为这个原因所以有时也把 CADisplayLink 当做计时器控件来使用。
@property(strong,nonatomic)CADisplayLink* link;
-(CADisplayLink*)link{
if(!_link) {
//创建一个执行某方法的定制器
//rotation:为自定义的方法
_link= [CADisplayLinkdisplayLinkWithTarget:selfselector:@selector(rotation:)];
//把定时器加到主循环中
[_linkaddToRunLoop:[NSRunLoopcurrentRunLoop]forMode:NSDefaultRunLoopMode];
//一开始为暂停
_link.paused=YES;
}
return _link;
}
(八)CAAnimation动画
- CA的动画,只能施加到CALayer上
- CA动画与UIView的区别:
- CA动画是假的,视图看着好像位置改变,但其实数据没有改变
- UIView动画中,明确改变了视图的位置
//创建基础动画对象
CABasicAnimation* anim = [CABasicAnimationanimation];
//设置动画要修改的内容
//内容:
//位置 position(点)
//位移 translate
//大小 scale
//旋转rotation
//使用KVC为属性赋值,说明要改的属性名是什么
anim.keyPath = @"position";
//为属性赋值,到达的位置
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(250,520)];
//为属性赋值,起点的的位置
//anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(250, 520)];
//持续时间
anim.duration = 2;
//执行次数
anim.repeatCount = 2;
/** 固定动画结束时 视图的位置**/
//动画结束时,不要把动画从视图上移除
//保持结束时的状态
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
//旋转rotation
//使用KVC为属性赋值,说明要改的属性名是什么
anim.keyPath = @"position";
//为属性赋值,到达的位置
anim.toValue = [NSValue valueWithCGPoint:CGPointMake(250,520)];
//为属性赋值,起点的的位置
//anim.fromValue = [NSValue valueWithCGPoint:CGPointMake(250, 520)];
//持续时间
anim.duration = 2;
//执行次数
anim.repeatCount = 2;
/** 固定动画结束时 视图的位置**/
//动画结束时,不要把动画从视图上移除
//保持结束时的状态
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
/**
fillMode的取值:
kCAFillModeForwards =当动画结束时,layer保持动画最后的状态
kCAFillModeBackwards = 动画开始前,只需要将动画加入一个layer,layer会立即进入动画的初始状态并等待动画开始
kCAFillModeBoth = 以上两个动画效果的集合
kCAFillModeRemoved = 默认值。动画开始前后,动画对layer都没有影响。动画结束后,layer恢复到原来的状态。
*/
//将动画添加给layer
kCAFillModeBackwards = 动画开始前,只需要将动画加入一个layer,layer会立即进入动画的初始状态并等待动画开始
kCAFillModeBoth = 以上两个动画效果的集合
kCAFillModeRemoved = 默认值。动画开始前后,动画对layer都没有影响。动画结束后,layer恢复到原来的状态。
*/
//将动画添加给layer
[self.iamgeView.layeraddAnimation:animforKey:nil];
(九)关键帧动画
//创建关键帧动画的对象
CAKeyframeAnimation* anim = [CAKeyframeAnimationanimation];
anim.keyPath = @"position";
//1.关键帧,组
anim.values = @[
[NSValue valueWithCGPoint:CGPointMake(100,100)],
[NSValue valueWithCGPoint:CGPointMake(400,100)],
[NSValue valueWithCGPoint:CGPointMake(400,400)],
[NSValue valueWithCGPoint:CGPointMake(100,400)],
[NSValue valueWithCGPoint:CGPointMake(100,100)]
];
CAKeyframeAnimation* anim = [CAKeyframeAnimationanimation];
anim.keyPath = @"position";
//1.关键帧,组
anim.values = @[
[NSValue valueWithCGPoint:CGPointMake(100,100)],
[NSValue valueWithCGPoint:CGPointMake(400,100)],
[NSValue valueWithCGPoint:CGPointMake(400,400)],
[NSValue valueWithCGPoint:CGPointMake(100,400)],
[NSValue valueWithCGPoint:CGPointMake(100,100)]
];
//2.使用贝塞尔对象,绘制矩形,以矩形的四个点作为关键帧顶点运动
anim.path= [UIBezierPathbezierPathWithRoundedRect:CGRectMake(100,100,200, 200)cornerRadius:20].CGPath;
//持续时间
anim.duration = 1;
//执行次数
anim.repeatCount = 1000000000;
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
//持续时间
anim.duration = 1;
//执行次数
anim.repeatCount = 1000000000;
anim.removedOnCompletion = NO;
anim.fillMode = kCAFillModeForwards;
[self.iamgeView.layeraddAnimation:animforKey:nil];
(十)组动画
- 同时设置多个动画效果
//组动画
-(void)groupAnimation{
//创建位移的动画
CABasicAnimation* positionAnim = [CABasicAnimationanimation];
positionAnim.keyPath = @"position.y";
positionAnim.toValue = @500;
//动画运行时,速度函数EaseIn开始快
positionAnim.timingFunction= [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseIn];
-(void)groupAnimation{
//创建位移的动画
CABasicAnimation* positionAnim = [CABasicAnimationanimation];
positionAnim.keyPath = @"position.y";
positionAnim.toValue = @500;
//动画运行时,速度函数EaseIn开始快
positionAnim.timingFunction= [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseIn];
//创建一个变形的动画
CABasicAnimation* transformAnim = [CABasicAnimationanimationWithKeyPath:@"transform.rotation.y"];
transformAnim.toValue = @(M_PI);
transformAnim.timingFunction= [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseOut];
//创建动画组
CAAnimationGroup* animGroup = [CAAnimationGroupanimation];
animGroup.animations = @[positionAnim,transformAnim];
animGroup.duration = 2;
animGroup.repeatCount = 10;
animGroup.removedOnCompletion=NO;
animGroup.fillMode = kCAFillModeForwards;
[self.iamgeView.layeraddAnimation:animGroupforKey:nil];
CABasicAnimation* transformAnim = [CABasicAnimationanimationWithKeyPath:@"transform.rotation.y"];
transformAnim.toValue = @(M_PI);
transformAnim.timingFunction= [CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseOut];
//创建动画组
CAAnimationGroup* animGroup = [CAAnimationGroupanimation];
animGroup.animations = @[positionAnim,transformAnim];
animGroup.duration = 2;
animGroup.repeatCount = 10;
animGroup.removedOnCompletion=NO;
animGroup.fillMode = kCAFillModeForwards;
[self.iamgeView.layeraddAnimation:animGroupforKey:nil];
}
总结:CAAnimation的子类
- CABasicAnimation
- 位移、缩放、旋转
- CAKeyframeAnimation
- CAAnimationGroup
0 0
- (笔记)动画
- jQuery笔记(动画)
- UIView动画(学习笔记)
- Android学习笔记(动画)
- 动画笔记
- Android笔记 动画之tween(补间)动画demo
- Android开发笔记(九十六)集合动画与属性动画
- Android动画学习笔记(三)—基础动画小结
- Android学习笔记之动画篇(视图动画)
- Ogre学习笔记(8):骨骼动画
- XNA 学习笔记-动画(Animation)
- openGL学习笔记4(动画)
- Flex学习笔记(五) 动画效果
- android_动画学习笔记(一)
- Css3学习笔记(六):动画
- 菜单动画效果(viewWithTag) 笔记
- 绘图与动画学习笔记(一)
- 绘图与动画学习笔记(二)
- Struts2 result type(结果类型)
- 20160612
- Linux下基于socket多线程并发通信的实现
- 线程池
- CentOS 服务器配置完全记录
- (笔记)动画
- sqlite操作总结(一)
- iOS Facebook POP
- sql with as用法详解
- 【SSH系列】Hibernate映射 -- 一对一单向关联映射
- 单例模式以及通过反射和序列化破解单例模式
- java—数据类型
- Makefile 编写实例
- HBase 常见Shell命令总结