oc基本动画实现

一、使用UIView类实现动画

基本写法,代码必须放在Begin和Commit之间: 

1. [UIView beginAnimations:nil context:nil]; // 开始动画
2. // Code。。。
3. [UIView commitAnimations]; // 提交动画

简单例子:

01. [UIView beginAnimations:nil context:nil]; // 开始动画
02. [UIView setAnimationDuration:10.0]; // 动画时长
03.  
04. /**
05. *  图像向下移动
06. */
07. CGPoint point = _imageView.center;
08. point.y += 150;
09. [_imageView setCenter:point];
10.  
11. [UIView commitAnimations]; // 提交动画

同时运行多个动画效果: 

01. [UIView beginAnimations:nil context:nil];
02. [UIView setAnimationDuration:3.0];
03. [_imageView setAlpha:0.0];
04. [UIView commitAnimations];
05.  
06. [UIView beginAnimations:nil context:nil];
07. [UIView setAnimationDuration:3.0];
08. CGPoint point = _imageView.center;
09. point.y += 150;
10. [_imageView setCenter:point];
11. [UIView commitAnimations];

以上代码实现的动画效果为(同时执行):

1、图像向下平移150像像

2、设置图像透明度为0。

指定上下文:

1. CGContextRef context = UIGraphicsGetCurrentContext();
2. [UIView beginAnimations:nil context:context];
3. [UIView setAnimationDuration:2.0];
4. [_imageView setAlpha:0];
5. [UIView commitAnimations];

UIGraphicsGetCurrentContext():获取当前视图的上下文

其它方法及属性:

以下方法及属性不为全部,只例举部分(其它没提及到的方法及属性请自行尝试,谢谢):

01. // 开始动画
02. + (void)beginAnimations:(NSString *)animationID context:(void *)context;
03.  
04. // 提交动画
05. + (void)commitAnimations; 
06.  
07. // 设置动画曲线,默认是匀速进行:
08. + (void)setAnimationCurve:(UIViewAnimationCurve)curve;
09.  
10. // 设置动画时长:
11. + (void)setAnimationDuration:(NSTimeInterval)duration;
12.  
13. // 默认为YES。为NO时跳过动画效果,直接跳到执行后的状态。
14. + (void)setAnimationsEnabled:(BOOL)enabled;
15.  
16. // 设置动画延迟执行(delay:秒为单位):
17. + (void)setAnimationDelay:(NSTimeInterval)delay;
18.  
19. // 动画的重复播放次数
20. + (void)setAnimationRepeatCount:(float)repeatCount;
21.  
22. // 如果为YES,逆向(相反)动画效果,结束后返回动画逆向前的状态; 默认为NO:
23. + (void)setAnimationRepeatAutoreverses:(BOOL)repeatAutoreverses;
24.  
25. // 设置动画代理:
26. + (void)setAnimationDelegate:(id)delegate;
27.  
28. // 动画将要开始时执行方法××(必须要先设置动画代理):
29. + (void)setAnimationWillStartSelector:(SEL)selector;
30.  
31. // 动画已结束时执行方法××(必须要先设置动画代理):
32. + (void)setAnimationDidStopSelector:(SEL)selector;
33.  
34. /**
35. *  设置动画过渡效果
36. *
37. *  @param transition 动画的过渡效果
38. *  @param view 过渡效果作用视图
39. *  @param cache 如果为YES,开始和结束视图分别渲染一次并在动画中创建帧;否则,视图将会渲染每一帧。例如,你不需要在视图转变中不停的更新,你只需要等到转换完成再去更新视图。
40. */
41. + (void)setAnimationTransition:(UIViewAnimationTransition)transition forView:(UIView *)view cache:(BOOL)cache;
42.  
43. // 删除所有动画层
44. // 注意:层指的是layout,例:[_imageView.layer removeAllAnimations];
45. - (void)removeAllAnimations;

二、使用UIView的动画块代码:

方法一:

1. [UIView animateWithDuration:4.0 // 动画时长
2. animations:^{
3. // code
4. }];

方法二:

1. [UIView animateWithDuration:4.0 // 动画时长
2. animations:^{
3. // code...
4. }
5. completion:^(BOOL finished) {
6. // 动画完成后执行
7. // code...
8. }];

方法三:

01. [UIView animateWithDuration:4.0 // 动画时长
02. delay:2.0 // 动画延迟
03. options:UIViewAnimationOptionCurveEaseIn // 动画过渡效果
04. animations:^{
05. // code...
06. }
07. completion:^(BOOL finished) {
08. // 动画完成后执行
09. // code...
10. }];

方法四,Spring Animationring Animation):

在IOS7开始,系统动画效果广泛应用Spring Animation:

01. [UIView animateWithDuration:4.0 // 动画时长
02. delay:0.0 // 动画延迟
03. usingSpringWithDamping:1.0 // 类似弹簧振动效果 0~1
04. initialSpringVelocity:5.0 // 初始速度
05. options:UIViewAnimationOptionCurveEaseInOut // 动画过渡效果
06. animations:^{
07. // code...
08. CGPoint point = _imageView.center;
09. point.y += 150;
10. [_imageView setCenter:point];
11. } completion:^(BOOL finished) {
12. // 动画完成后执行
13. // code...
14. [_imageView setAlpha:1];
15. }];

usingSpringWithDamping:它的范围为 0.0f 到 1.0f ,数值越小「弹簧」的振动效果越明显。

initialSpringVelocity:初始的速度,数值越大一开始移动越快。值得注意的是,初始速度取值较高而时间较短时,也会出现反弹情况。

1. 转:Spring Animation 是线性动画或 ease-out 动画的理想替代品。由于 iOS 本身大量使用的就是 Spring Animation,用户已经习惯了这种动画效果,因此使用它能使 App 让人感觉更加自然,用 Apple 的话说就是「instantly familiar」。此外,Spring Animation 不只能对位置使用,它适用于所有可被添加动画效果的属性。

方法五,关键帧动画:

1. UIView动画已经具备高级的方法来创建动画,而且可以更好地理解和构建动画。IOS7以后苹果新加了一个animateKeyframesWithDuration的方法,我们可以使用它来创建更多更复杂更酷炫的动画效果,而不需要去使用到核心动画(CoreAnimatino)。

创建关键帧方法:

01. /**
02. *  添加关键帧方法
03. *
04. *  @param duration   动画时长
05. *  @param delay      动画延迟
06. *  @param options    动画效果选项
07. *  @param animations 动画执行代码
08. *  @param completion 动画结束执行代码
09. */
10. + (void)animateKeyframesWithDuration:(NSTimeInterval)duration
11. delay:(NSTimeInterval)delay
12. options:(UIViewKeyframeAnimationOptions)options
13. animations:(void (^)(void))animations
14. completion:(void (^)(BOOL finished))completion;

添加关键帧方法:

01. /**
02. *  添加关键帧
03. *
04. *  @param frameStartTime 动画相对开始时间
05. *  @param frameDuration  动画相对持续时间
06. *  @param animations     动画执行代码
07. */
08. + (void)addKeyframeWithRelativeStartTime:(double)frameStartTime
09. relativeDuration:(double)frameDuration
10. animations:(void (^)(void))animations;

以上说的相对时间,也就是说:“它们自身会根据动画总持续时长自动匹配其运行时长”。

下面用一个简单的示例作解答,彩虹变化视图:

01. void (^keyFrameBlock)() = ^(){
02. // 创建颜色数组
03. NSArray *arrayColors = @[[UIColor orangeColor],
04. [UIColor yellowColor],
05. [UIColor greenColor],
06. [UIColor blueColor],
07. [UIColor purpleColor],
08. [UIColor redColor]];
09. NSUInteger colorCount = [arrayColors count];
10. // 循环添加关键帧
11. for (NSUInteger i = 0; i < colorCount; i++) {
12. [UIView addKeyframeWithRelativeStartTime:i / (CGFloat)colorCount
13. relativeDuration:1 / (CGFloat)colorCount
14. animations:^{
15. [_graduallyView setBackgroundColor:arrayColors[i]];
16. }];
17. }
18. };
19. [UIView animateKeyframesWithDuration:4.0
20. delay:0.0
21. options:UIViewKeyframeAnimationOptionCalculationModeCubic | UIViewAnimationOptionCurveLinear
22. animations:keyFrameBlock
23. completion:^(BOOL finished) {
24. // 动画完成后执行
25. // code...
26. }];

动画过渡效果(Options),新增了以下几个:

1. UIViewKeyframeAnimationOptionCalculationModeLinear     = 0 << 10// default
2. UIViewKeyframeAnimationOptionCalculationModeDiscrete   = 1 << 10,
3. UIViewKeyframeAnimationOptionCalculationModePaced      = 2 << 10,
4. UIViewKeyframeAnimationOptionCalculationModeCubic      = 3 << 10,
5. UIViewKeyframeAnimationOptionCalculationModeCubicPaced = 4 << 10

下面我们看一张图,让我们更容易理解:

小结:

UIView实现动画的方法有很多种。简单的动画效果你可以随意丢,比较复杂的动画效果你可以选用关键帧KeyFrame方法。

至于选用哪种,就需要根据产品需求去进行判断。

本文参考文章:

http://www.tuicool.com/articles/FjiQJbF 

http://www.tuicool.com/articles/ZR7nYv


博文作者:GarveyCalvin

博文出处:http://www.cnblogs.com/GarveyCalvin/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值