一、编写一个简单的动画,使一个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(, , , )];
_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(, )];
animation.toValue = [NSValue valueWithCGPoint:CGPointMake(CGRectGetWidth(self.view.bounds) - , CGRectGetHeight(self.view.bounds) - )];
animation.duration = ;
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开始动画。

CABasicAnimation 几种停止的回调的更多相关文章

  1. C++使用模板、函数指针、接口和lambda表达式这四种方法做回调函数的区别比较

    在C++中,两个类之间存在一种关系,某个类需要另外一个类去完成某一个功能,完成了之后需要告知该类结果,这种最普通最常见的需求,往往使用回调函数来解决. 如题,我总结下来有这么四种方式可以完成这项功能, ...

  2. [转]C/C++实现回调机制的几种方式(回调、槽、代理)

    转自:https://www.jianshu.com/p/4f907bba6d5f (1)Callback方式(回调) Callback的本质是设置一个函数指针进去,然后在需要需要触发某个事件时调用该 ...

  3. java四种引用与回调函数

    JAVA四种引用 java对象的引用包括: 强引用 软引用 弱引用 虚引用 Java中提供这四种引用类型主要有两个目的: 第一是可以让程序员通过代码的方式决定某些对象的生命周期: 第二是有利于JVM进 ...

  4. 线程(Thread)的四种停止方式

    1.正常的程序启动,停止 2.使用退出标记,一般程序在run()方法后,线程会正常结束.但是有一些伺服线程还在运行,他们运行时间较长,只有当外部条件满足时,他们才会停止.实现如下: public cl ...

  5. Python Twisted系列教程17:造”回调”的另一种方法

    作者:dave@http://krondo.com/just-another-way-to-spell-callback/  译者: Cheng Luo 你可以从”第一部分 Twist理论基础“开始阅 ...

  6. [教程]Delphi 中三种回调函数形式解析

    Delphi 支持三种形式的回调函数 全局函数这种方式几乎是所有的语言都支持的,类的静态函数也可以归为此类,它保存的只是一个函数的代码起始地址指针( Pointer ).在 Delphi 中声明一般为 ...

  7. Springboot 优雅停止服务的几种方法

    在使用Springboot的时候,都要涉及到服务的停止和启动,当我们停止服务的时候,很多时候大家都是kill -9 直接把程序进程杀掉,这样程序不会执行优雅的关闭.而且一些没有执行完的程序就会直接退出 ...

  8. [WCF编程]10.操作:回调操作

    一.回调操作概述 WCF支持服务将调用返回给它的客户端.在回调期间,许多方面都将颠倒过来:服务将成为客户端,客户端将编程服务.回调操作可以用在各种场景和应用程序中,但在涉及事件或者服务发生时间需要通知 ...

  9. Hystrix微服务容错处理及回调方法源码分析

    前言 在 SpringCloud 微服务项目中,我们有了 Eureka 做服务的注册中心,进行服务的注册于发现和服务治理.使得我们可以摒弃硬编码式的 ip:端口 + 映射路径 来发送请求.我们有了 F ...

随机推荐

  1. Logstash使用grok过滤nginx日志(二)

    在生产环境中,nginx日志格式往往使用的是自定义的格式,我们需要把logstash中的message结构化后再存储,方便kibana的搜索和统计,因此需要对message进行解析. 本文采用grok ...

  2. mybatis分页控件

    https://my.oschina.net/miemiedev/blog/135516

  3. THINKCMF-NGINX伪静态

    location / { index index.php index.html index.htm; if (!-e $request_filename) { rewrite ^/index.php( ...

  4. RockMongo 安装

    1. yum install php-pecl-http php 2. yum install httpd 3. yum install php-devel 4. pecl install mongo ...

  5. 遍历json创建树状表(首先的前提条件是要引入jquery的jquery treeTable插件)

    "root":{ "children":[ { "name":"AA", "children":[ ...

  6. top batch output

    echo 3 > sudo tee /proc/sys/vm/drop_caches top -d30 -bn20 > a

  7. Sipdroid实现SIP(三): 消息接收处理

    I. 注册回调 RegisterAgent类 在TransactionClient Fail/Success的回调中, 调用RegisterAgentListener的Register Fail/Su ...

  8. nodejs 包引用的终极结论

    通常我们用exports 或module.exports 来导出一个文件中的接口和字段,用require来引用导出的对象.那么这个exports 和 module.exports到底有啥关联呢? 1. ...

  9. cocos2d-x中的init,onEnter,onExit......

    CCNode的init()方法和onEnter()方法: 一.调用顺序显示FirstScene时: FirstLayer::initFirstLayer::onEnterFirstLayer::onE ...

  10. java实现的简单词法分析器

    一个简单的词法分析器 词法分析(Lexical Analysis) 是编译的第一阶段.词法分析器的主要任务是读入源程序的输入字符.将他们组成词素,生成并输出一个词法单元序列,每个词法单元对应一个词素. ...