IOS中UIView动画的简单实现

简述:

iOS的UIView类为我们提供了一系列的类方法来实现动画效果,在UIView对象或者它对应的CALayer对象中支持动画效果的属性有如下:

frame 
bounds 
center 
transform 
alpha 
backgroundColor 

1、通过UIView(UIViewAnimation)分类实现动画,这种方式是最原始的方式,所有的动画块代码都定义在commitAnimations方法之前

[objc]  view plain copy
  1. #import "ViewController.h"  
  2.       
  3.     @interface ViewController ()  
  4.     @property (nonatomic,retain)UIView *anView;  
  5.     @end  
  6.       
  7.     @implementation ViewController  
  8.       
  9.     - (void)viewDidLoad  
  10.     {  
  11.         [super viewDidLoad];  
  12.         UIButton *animationButton = [UIButton buttonWithType:UIButtonTypeCustom];  
  13.         animationButton.frame = CGRectMake(06420050);  
  14.         [animationButton addTarget:self action:@selector(beginAnimation) forControlEvents:UIControlEventTouchUpInside];  
  15.         [self.view addSubview:animationButton];  
  16.         [animationButton setTitle:@"动画" forState:UIControlStateNormal];  
  17.         [animationButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  
  18.         [animationButton setBackgroundColor:[UIColor blueColor]];  
  19.           
  20.         _anView = [[UIView alloc] initWithFrame:CGRectMake(0115320500)];  
  21.         _anView.backgroundColor = [UIColor redColor];  
  22.         [self.view addSubview:_anView];  
  23.         // Do any additional setup after loading the view, typically from a nib.  
  24.     }  
  25.     - (void)beginAnimation{  
  26.           
  27.         /** 
  28.          1、所有动画属性的设置和实现动画效果的代码必须放在beginAnimations:context:和commitAnimations快中 
  29.          */  
  30.         [UIView beginAnimations:@"firstAnimation" context:nil];  
  31.         [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];  
  32.         [UIView setAnimationDidStopSelector:@selector(stop)];  
  33.         [UIView setAnimationDuration:1.0f];  
  34.         // --实现大小缩放一倍的动画效果  
  35.         self.anView.transform = CGAffineTransformMakeScale(0.5f0.5f);  
  36.         [UIView commitAnimations];  
  37.         // --或者用层也可以实现动画效果</span>  
  38.         [UIView beginAnimations:nil context:NULL];  
  39.         CGAffineTransform moveTransform = CGAffineTransformMakeTranslation(180200);  
  40.         self.anView.layer.affineTransform=moveTransform;  
  41.         [UIView commitAnimations];  
  42.     }  
[objc]  view plain copy
  1. <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">2、通过UIView(UIViewAnimationWithBlocks分类实现动画</span>  

iOS4及以后推荐这种方式,它通过块的形式使得动画的实现代码简洁明了。二者原理是一样的,下面看一段代码

3、关键帧动画

iOS7为UIView封装了一组API,让我们很容易的得到与Core Animation框架中的CAKeyframeAnimation一样的效果。新引入的animateKeyframesWithDuration与CAKeyframeAnimation的关系,可以比对animateWithDuration和CABasicAnimation,我们只需要将每一帧动画加入到block方法中,并传入此段动画在全过程中的相对开始时间和执行时间(duration具体是指此段动画的执行时间占全过程的百分比)。同时,你可以在一次动画中使用多个关键帧,只需使用addKeyframe依次将所有关键帧加入动画执行栈中。

[objc]  view plain copy
  1. [UIView animateKeyframesWithDuration:duration delay:delay   
  2. options:options animations:^{   
  3. [UIView addKeyframeWithRelativeStartTime:0.0   
  4. relativeDuration:0.5 animations:^{   
  5. //第一帧要执行的动画   
  6. }];   
  7. [UIView addKeyframeWithRelativeStartTime:0.5   
  8. relativeDuration:0.5 animations:^{   
  9. //第二帧要执行的动画   
  10. }];   
  11. } completion:^(BOOL finished) {   
  12. //动画结束后执行的代码块   
  13. }];   

4、弹簧动画

iOS7新引入的另一个block方法可以让你轻松将真实物理世界中的弹性效果集成进视图动画中。苹果公司一直建议开发者尽可能将动画效果做的跟真实物理世界一样——在视图滑动时,可以像弹簧一样,稍微拉伸一些,再弹回正确位置。使用新的弹簧动画API来实现此效果相较以往要简单很多。

[objc]  view plain copy
  1. [UIView animateWithDuration:duration delay:delay   
  2. usingSpringWithDamping:damping initialSpringVelocity:velocity   
  3. options:options animations:^{   
  4. //这里书写动画相关代码   
  5. } completion:^(BOOL finished) {   
  6. //动画结束后执行的代码块   
  7. }];   
这里用到了一些物理上的概念:damping参数代表弹性阻尼,随着阻尼值越来越接近0.0,动画的弹性效果会越来越明显,而如果设置阻尼值为1.0,则视图动画不会有弹性效果——视图滑动时会直接减速到0并立刻停止,不会有弹簧类的拉伸效果。
velocity参数代表弹性修正速度,它表示视图在弹跳时恢复原位的速度,例如,如果在动画中视图被拉伸的最大距离是200像素,你想让视图以100像素每秒的速度恢复原位,那么就设置velocity的值为0.5。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值