iOS动画浅析
来源:互联网 发布:淘宝火锅底料 编辑:程序博客网 时间:2024/05/17 07:19
概述
iOS动画主要有三种调用方式:1. UIView的动画代码块
2. UIView [begin, commit]模式
3. CABasicAnimation方法
UIView Animation
代码块调用
[UIView animateWithDuration:timeInterval animations:^{weakTableView.frame = CGRectMake(0, -height, weakSelf.frame.size.width, height);[weakTableView setAlpha:0];}];
注意:在代码块中对对象的引用要用申明成__weak,否则会引起内存泄露。
[begin, commit]模式
[UIView beginAnimations:nil context:nil];[UIView setAnimationDuration:timeInterval];[view setHidden:hide];[UIView commitAnimations];
开始/结束动画消息定义形式:
//开始动画-(void)animationDidStart:(NSString *)animationID context:(void *)context//结束动画- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
两种方式对比
- 通过代码块实现动画,使用 UIViewAnimationOptionCurveEaseInOut 和UIViewAnimationOptionTransitionNone animation选项。在动画的播放过程中,用户的交互暂时不可用。
- 通过[begin,commit]模式进行动画的设置,较之代码块的方式,此种方式更加灵活,可以配置更多的选项。另外,通过animationID和context参数可以配置动画开始/结束时调用的方法。
CABasicAnimation
常用键值
注:键值对应于CALayer的相关属性,通过改变这些属性的值实现动画效果。- transform.scale.x宽的比例转换
- transform.scale.y 高的比例转换
- transform.rotation.z 平面圆的旋转
- opacity 透明度
- margin 在视图中展示内容的大小
- zPosition 屏幕上层次的前后顺序
- backgroundColor 背景颜色
- cornerRadius 圆角
- borderWidth 边的宽度
- bounds layer的边界矩阵, 与frame类似
- contents 内容, 例如:图片的变化
- contentsRect 内容可使用layer的大小
- frame 视图的frame矩阵
- hidden 隐藏状态
- mask 遮挡背景和内容显示的透明度
- masksToBounds 决定子layer是否从layer的边界中剪除
- position layer在其父layer中对应的位置
- shadowColor 阴影颜色
- shadowOffset 阴影偏移量
- shadowOpacity 阴影透明度
- shadowRadius 阴影圆角
调用方法
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
时间函数
设置方法
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
常用函数
- kCAMediaTimingFunctionLinear 线性递增
- kCAMediaTimingFunctionEaseIn 慢进快出,即动画开始时缓慢,然后逐渐加速
- kCAMediaTimingFunctionEaseOut 快进慢出,即动画开始时速度较快,然后逐渐缓慢
- kCAMediaTimingFunctionEaseInEaseOut 慢进-加速-慢出
- kCAMediaTimingFunctionDefault 使用控制点[(0.0,0.0), (0.25,0.1), (0.25,0.1), (1.0,1.0)],需要保证动画使用默认的计时
循环播放动画的两种方式
定时器
NSTimer
创建/启动NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:timeIntervaltarget:self.animationCollectionsselector:animationSelectoruserInfo:nilrepeats:YES];
停止
[timer invalidate];timer = nil;
CADiplayLink
创建/启动CADisplayLink *display = [CADisplayLink displayLinkWithTarget:self.animationCollections selector:animationSelector];[display addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
停止
[player invalidate];timer = nil;
循环播放
CABasicAnimation
animation.repeatCount = FLT_MAX;animaiton.removedOnCompletion = NO;
代码实例
- AnimationDemo
在实例代码中实现了一下几种动画效果:
- 渐入/渐出;
- 闪烁;
- 下拉/收缩菜单;
- 移动;
- 放大/缩小
- 匀速旋转;
- 3D旋转;
- 时钟;
- 快速旋转;
效果
0 0
- iOS动画浅析
- iOS动画浅析-CATransition
- iOS动画浅析
- ios 动画浅析
- iOS动画浅析
- iOS 动画分类浅析
- iOS动画浅析
- iOS动画浅析
- iOS动画浅析
- iOS动画浅析
- iOS动画浅析-CATransition
- iOS 动画浅析
- iOS核心动画--浅析三个动画的实现
- Android动画浅析
- Android动画浅析
- UIimageView动画浅析
- Android 动画浅析
- view动画浅析
- 【Oracle篇】存储过程
- APNs消息推送完整讲解
- JS中的prototype
- HQL的in的用法
- 选择排序
- iOS动画浅析
- C++文件读取 写入 操作
- 【Oracle篇】触发器的使用 [上]
- 王立平--ProgressDialog
- oto餐饮app实时推送订单给店家程序详细实现(二)
- java虚拟机
- 有域名有空间怎样建企业网站 - 企业建站十万个为什么
- IOS开发 UITableView中Cell重用机制导致内容重复解决方法
- 服务器遭受攻击后的一般处理过程