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开始动画。

posted @   兜兜有糖的博客  阅读(3166)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示