• iOS-动画效果(首尾式动画,代码快动画,核心动画,序列帧动画)


    一.各个动画的优缺点

    1.首尾动画:如果只是修改空间的属性,使用首尾动画比较方便,如果在动画结束后做后续处理,就不是那么方面了。

    2.核心动画:有点在于对后续的处理方便。

    3.块动画:

    (1)在实际的开发中更常用的时block代码块来处理动画操作。

    (2)块动画相对来说比较灵活,尤为重要的是能够将动画相关的代码编写在一起,便于代码的阅读和理解.

    4.使用序列帧动画:对UIImageview和button按钮进行连线。

      1 #import "ViewController.h"
      2 
      3 @interface ViewController ()
      4 {
      5     UIView             *_view;
      6     UIImageView        *_imageView;
      7 }
      8 @end
      9 
     10 @implementation ViewController
     11 
     12 - (void)viewDidLoad {
     13     [super viewDidLoad];
     14 
     15     //用于block代码快动画,核心动画,UIView封装的首尾式动画
     16     _view = [[UIView alloc] init];
     17     _view.frame = CGRectMake(0, 20, 40, 40);
     18     _view.backgroundColor = [UIColor redColor];
     19     [self.view addSubview:_view];
     20     
     21     //用于序列帧动画
     22     _imageView = [[UIImageView alloc] init];
     23     _imageView.frame = CGRectMake(60, 20, 40, 40);
     24     _imageView.backgroundColor = [UIColor orangeColor];
     25     [self.view addSubview:_imageView];
     26 }
     27 
     28 #pragma mark 序列帧动画
     29 //- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
     30 //{
     31 //    NSMutableArray * arrayM = [NSMutableArray array];
     32 //    
     33 //    
     34 //    for (int i = 0; i < 4; i ++) {
     35 //        [arrayM addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png",i]]];
     36 //    }
     37 //    //设置动画数组
     38 //    [_imageView setAnimationImages:arrayM];
     39 //    
     40 //    //设置动画播放次数 MAXFLOAT:无穷大数,表示一直循环下去
     41 //    [_imageView setAnimationRepeatCount:MAXFLOAT];
     42 //    
     43 //    //设置动画播放时间(图片个数 * 计划的每张照片动画时长)
     44 //    [_imageView setAnimationDuration:4 * 0.75];
     45 //    
     46 //    //开始动画
     47 //    [_imageView startAnimating];
     48 //}
     49 
     50 
     51 #pragma mark block代码快动画
     52 //- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
     53 //{
     54 //    
     55 //    [UIView animateWithDuration:2.0 animations:^{
     56 //         NSLog(@"动画开始执行前的位置: %@",NSStringFromCGPoint(_view.center));
     57 //        _view.center = CGPointMake(self.view.bounds.size.width - 20, self.view.bounds.size.height - 20);
     58 //        
     59 //        
     60 //    } completion:^(BOOL finished) {
     61 //        NSLog(@"动画执行完毕后的位置: %@",NSStringFromCGPoint(_view.center));
     62 //    }];
     63 //}
     64 
     65 #pragma mark 核心动画
     66 //- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
     67 //{
     68 //    //创建核心动画
     69 //    CABasicAnimation * animation = [CABasicAnimation animation];
     70 //    
     71 //    //平移
     72 //    animation.keyPath = @"position";
     73 //
     74 //    //设置执行的动画
     75 //    animation.toValue = [NSValue valueWithCGPoint:CGPointMake(self.view.bounds.size.width - 20, self.view.bounds.size.height - 20)];
     76 //    
     77 //    //设置动画的时长
     78 //    animation.duration = 2.0f;
     79 //    
     80 //    //设置动画执行完毕之后不删除动画
     81 //    animation.removedOnCompletion = NO;
     82 //    
     83 //    //设置保存动画的最新状态
     84 //    animation.fillMode = kCAFillModeForwards;
     85 //    
     86 //    //设置动画的代理
     87 //    animation.delegate = self;
     88 //    
     89 //    //给控件添加核心动画
     90 //    [_view.layer addAnimation:animation forKey:nil];
     91 //}
     92 //#pragma mark 核心动画开始时调用的方法
     93 //- (void)animationDidStart:(CAAnimation *)anim
     94 //{
     95 //    NSLog(@"动画开始执行前的位置: %@",NSStringFromCGPoint(_view.center));
     96 //}
     97 //#pragma mark 核心动画结束时调用的方法
     98 //- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
     99 //{
    100 //    NSLog(@"动画执行完毕后的位置: %@",NSStringFromCGPoint(_view.center));
    101 //}
    102 
    103 
    104 
    105 #pragma mark UIView封装的首尾式动画
    106 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    107 {
    108     NSLog(@"动画执行之前的位置: %@",NSStringFromCGPoint(_view.center));
    109     
    110     //首尾式动画
    111     //执行动画
    112     [UIView beginAnimations:nil context:nil];
    113     
    114     //动画时间
    115     [UIView setAnimationDuration:2.0f];
    116     
    117     //设置动画的代理
    118     [UIView setAnimationDelegate:self];
    119     
    120     //设置动画执行完毕调用的事件
    121     [UIView setAnimationDidStopSelector:@selector(didStopAnimation)];
    122     
    123     _view.center = CGPointMake(self.view.bounds.size.width - 20, self.view.bounds.size.height - 20);
    124     
    125     //动画结束
    126     [UIView commitAnimations];
    127 }
    128 
    129 #pragma mark 动画执行完毕调用的方法
    130 - (void)didStopAnimation
    131 {
    132     NSLog(@"动画执行完毕");
    133     
    134     NSLog(@"动画执行之后的位置: %@",NSStringFromCGPoint(_view.center));
    135 }
    136 
    137 @end
    你的一次推荐就是对我莫大的支持。感觉不错,给个推荐或者评论吧。
  • 相关阅读:
    Jzoj3899 逻辑的连通性
    第三十九天 how can I 坚持
    第三十八天 how can I 坚持
    第三十七天 how can I 坚持
    第三十六天 how can I 坚持
    第三十五天 how can I 坚持
    第三十四天 how can I 坚持
    第三十三天 how can I 坚持
    第三十二天 how can I 坚持
    逆向iOS SDK -- _UIImageAtPath 的实现(SDK 5.1)
  • 原文地址:https://www.cnblogs.com/mancong/p/5131038.html
Copyright © 2020-2023  润新知