码迷,mamicode.com
首页 > 移动开发 > 详细

iOS动画浅析

时间:2014-09-30 17:13:49      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   使用   ar   strong   

概述

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:timeInterval
target:self.animationCollections
selector:animationSelector
userInfo:nil
repeats: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;

代码实例


在实例代码中实现了一下几种动画效果:
  1. 渐入/渐出;
  2. 闪烁;
  3. 下拉/收缩菜单;
  4. 移动;
  5. 放大/缩小
  6. 匀速旋转;
  7. 3D旋转;
  8. 时钟;
  9. 快速旋转;

效果

bubuko.com,布布扣

iOS动画浅析

标签:style   blog   http   color   io   os   使用   ar   strong   

原文地址:http://blog.csdn.net/shutingchen/article/details/39695835

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!