• CABasicAnimation 几种停止的回调


    一、编写一个简单的动画,使一个UIview从屏幕的左上角移动到左下角,间隔时间3S

    //
    //  ViewController.m
    //  CAAnimationTest
    //
    //  Created by on 15-10-27.
    //  Copyright (c) 2015年 va. All rights reserved.
    //
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    
    @property (nonatomic, strong) UIView *aniView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        
        _aniView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
        _aniView.backgroundColor = [UIColor yellowColor];
    //    [self.view addSubview:_aniView];
        
        UIView *backView = [[UIView alloc] initWithFrame:self.view.bounds];
        backView.backgroundColor = [UIColor greenColor];
        [self.view addSubview:backView];
        [backView addSubview:_aniView];
        
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
        animation.fromValue = [NSValue valueWithCGPoint:CGPointMake(50, 50)];
        animation.toValue = [NSValue valueWithCGPoint:CGPointMake(CGRectGetWidth(self.view.bounds) - 50, CGRectGetHeight(self.view.bounds) - 50)];
        animation.duration = 3;
        animation.removedOnCompletion = NO;
        animation.fillMode = kCAFillModeForwards;
        animation.delegate = self;
        
        [_aniView.layer addAnimation:animation forKey:@"testAni"];
    
    }
    
    - (void)animationDidStart:(CAAnimation *)anim
    {
        NSLog(@"animation start , aniView = %@", _aniView);
    }
    
    - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
    {
        NSLog(@"animation stop : flag = %d , aniView = %@", flag, _aniView);
    
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    @end

    效果:

    二、动画中断的几种情况

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [_aniView removeFromSuperview];
            
        });
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{        [backView removeFromSuperview];
        });
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{        [_aniView.layer removeAllAnimations];
        });

    以上三种情况都会回调下面的方法

    - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag

    输出:

    2015-10-28 01:08:19.330 CAAnimationTest[3615:6005077] animation start , aniView = <UIView: 0x7fe47b5308b0; frame = (0 0; 100 100); animations = { testAni=<CABasicAnimation: 0x7fe47b534020>; }; layer = <CALayer: 0x7fe47b530980>>

    2015-10-28 01:08:37.300 CAAnimationTest[3615:6005077] animation stop : flag = 1 , aniView = <UIView: 0x7fe47b5308b0; frame = (0 0; 100 100); animations = { testAni=<CABasicAnimation: 0x7fe47b534020>; }; layer = <CALayer: 0x7fe47b530980>>

    三、动画暂停

     

        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

            CFTimeInterval pausedTime = [_aniView.layer convertTime:CACurrentMediaTime() fromLayer:nil];

            _aniView.layer.speed = 0.0;

            _aniView.layer.timeOffset = pausedTime;

            

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(15 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                CFTimeInterval pausedTime = [_aniView.layer timeOffset];

                _aniView.layer.speed = 1.0;

                _aniView.layer.timeOffset = 0.0;

                _aniView.layer.beginTime = 0.0;

                CFTimeInterval timeSincePause = [_aniView.layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;

                _aniView.layer.beginTime = timeSincePause;

            });

        });

    这里的pauseTime是动画已经运动的时间间隔,暂停一段时间之后想回复,那么就相当于从当前时间点的 -pauseTime开始动画。

  • 相关阅读:
    数据库作业
    5-5 数据库笔记整理
    5月4日数据库笔记整理
    数据库
    4.27内容整理
    python基础4
    python基础4
    python基础 3
    完善版二级联动(可作为函数)需要导入jquery
    jquery $this 指向子元素
  • 原文地址:https://www.cnblogs.com/doudouyoutang/p/4916154.html
Copyright © 2020-2023  润新知