• 核心动画(转场动画和组动画)-转


    一.转场动画简单介绍 
    CAAnimation的子类,用于做转场动画,能够为层提供移出屏幕和移入屏幕的动画效果。ios比Max OS X的转场动画效果少一点。 
    UINavigationController就是通过CATransition实现了将控制器的视图推入屏幕的动画效果。 

    属性解析: 
    type:动画过渡方向 
    subtype:动画过渡方向 
    strartProgress:动画起点(在整体动画的百分比) 
    endProgress:动画终点(在整体动画的百分比)

    二.转场动画代码示例

    #import "ViewController.h"
    
    @interface ViewController (){
        NSMutableArray *colorArray;
    }
    @property(nonatomic,assign) int index;
    @property(nonatomic,strong) UIView *iconView;
    @property(nonatomic,strong) UIButton *previousButton;
    @property(nonatomic,strong) UIButton *nextButton;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        if (colorArray == nil) {
            colorArray = [[NSMutableArray alloc] init];
        }
        [colorArray addObject:[UIColor redColor]];
        [colorArray addObject:[UIColor blueColor]];
        [colorArray addObject:[UIColor greenColor]];
        [colorArray addObject:[UIColor yellowColor]];
        [colorArray addObject:[UIColor grayColor]];
    
        _iconView = [[UIView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 280/2,
                                                             100,
                                                             280,
                                                             200)];
        _iconView.backgroundColor = [UIColor brownColor];
        [self.view addSubview:_iconView];
    
        _previousButton = [[UIButton alloc] initWithFrame:CGRectMake(_iconView.frame.origin.x,
                                                                     350,
                                                                     60,
                                                                     40)];
        [_previousButton setTitle:@"上一张" forState:UIControlStateNormal];
        [_previousButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_previousButton addTarget:self
                            action:@selector(previousClick)
                  forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_previousButton];
    
        _nextButton = [[UIButton alloc] initWithFrame:CGRectMake(_iconView.frame.origin.x + _iconView.frame.size.width - 60,
                                                                 350,
                                                                 60,
                                                                 40)];
        [_nextButton setTitle:@"下一张" forState:UIControlStateNormal];
        [_nextButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_nextButton addTarget:self
                        action:@selector(nextButtonClick)
              forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_nextButton];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        _index = 0;
    }
    
    #pragma mark -按钮事件
    - (void)previousClick{
        _index--;
        if (_index < 0) {
            _index = 4;
        }
        _iconView.backgroundColor = [colorArray objectAtIndex:_index];
        //创建核心动画
        CATransition *animation = [CATransition animation];
        //告诉要执行什么动画
        //设置过度效果
        animation.type = @"cube";
        //设置动画的过度方向(向左)
        animation.subtype = kCATransitionFromLeft;
        //设置动画的时间
        animation.duration = 2.0;
        //添加动画
        [_iconView.layer addAnimation:animation forKey:nil];
    }
    
    - (void)nextButtonClick{
        _index++;
        if (_index > 4) {
            _index = 0;
        }
        _iconView.backgroundColor = [colorArray objectAtIndex:_index];
        //创建核心动画
        CATransition *animation = [CATransition animation];
        //告诉要执行什么动画
        //设置过度效果
        animation.type = @"cube";
        //设置动画的过度方向(向左)
        animation.subtype = kCATransitionFromRight;
        //设置动画的时间
        animation.duration = 2.0;
        //设置动画的起点
        //animation.startProgress = 0.5;
        //设置动画的终点
        //animation.endProgress = 0.5;
        //添加动画
        [_iconView.layer addAnimation:animation forKey:nil];
    }
    @end

    三.组动画简单说明 
    CAAnimation的子类,可以保存一组动画对象,将CAAnimationGroup对象加入层后,组中所有动画对象可以同时并发运行。 
    属性解析:animation:用来保存一组动画对象NSArray 默认情况下,一组动画对象是同时运行的,也可以通过设置动画对象的beginTime属性更改动画的开始时间。

    四.分组动画代码示例

    #import "ViewController.h"
    
    @interface ViewController ()
    @property(nonatomic,strong) UIView *iconView;
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        _iconView = [[UIView alloc] initWithFrame:CGRectMake(160,
                                                             40,
                                                             100,
                                                             100)];
        _iconView.backgroundColor = [UIColor redColor];
        [self.view addSubview:_iconView];
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        //平移动画
        CABasicAnimation *animation1 = [CABasicAnimation animation];
        animation1.keyPath = @"transform.translation.y";
        animation1.toValue = @(100);
        //缩放动画
        CABasicAnimation *animation2 = [CABasicAnimation animation];
        animation2.keyPath = @"transform.scale";
        animation2.toValue = @(0.0);
        //旋转动画
        CABasicAnimation *animation3 = [CABasicAnimation animation];
        animation3.keyPath = @"transform.rotation";
        animation3.toValue = @(M_PI_2);
    
        //组动画
        CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
        groupAnimation.animations = @[animation1,animation2,animation3];
        groupAnimation.duration = 2;
        groupAnimation.repeatCount = MAXFLOAT;
        groupAnimation.fillMode = kCAFillModeForwards;
        groupAnimation.removedOnCompletion = NO;
        [_iconView.layer addAnimation:groupAnimation forKey:nil];
    }
    
    @end
  • 相关阅读:
    分布式事务之最终一致性BASE理论
    CAP理论
    Comparator中返回0导致数据丢失的大坑
    电脑主板分类
    SimpleDateFormat线程不安全
    Redis面试题
    JS闭包
    ES6将两个数组合并成一个对象数组
    视频色彩空间RGB、YUV、YCbCr
    c#接口作用的深入理解
  • 原文地址:https://www.cnblogs.com/jiuyi/p/10108430.html
Copyright © 2020-2023  润新知