iOS动画浅析

时间:2014-09-30 16:24:40   收藏:0   阅读:360

概述

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


两种方式对比


CABasicAnimation

常用键值

注:键值对应于CALayer的相关属性,通过改变这些属性的值实现动画效果。

调用方法

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];

时间函数

设置方法

animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

常用函数


循环播放动画的两种方式

定时器

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,布布扣

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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!