一、编写一个简单的动画,使一个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. python爬虫框架scrapy初试(二点一)

    功能:爬取某网站部分新闻列表和对应的详细内容. 列表页面http://www.zaobao.com/special/report/politic/fincrisis 实现代码: import scra ...

  2. Word 2016插入公式快捷键

    实用的插入公式快捷键"Alt+=", 与君共享

  3. hdu1116回溯N皇后问题

    题目连接 经过思考,不难发现:恰好N个皇后放在不同行不同列,那么是不是可以转换成N个皇后所在行分别确定(一人一行)的情况下对她们的所在列的枚举. 也就是列的全排列生成问题,我们用c[x]表示x行皇后的 ...

  4. NGINX----源码阅读---config配置脚本

    config文件为nginx的配置入口文件. 1. #!/bin/sh # Copyright (C) Igor Sysoev # Copyright (C) Nginx, Inc. LC_ALL=C ...

  5. Web中的无状态含义

    REST架构设计是目前非常火热的概念,已经成为构建web服务时应该遵循的事实标准.REST约束中有一条很重要的规则是“无状态”,但“无状态”是个很抽象的概念,对刚刚接触的人来讲,很难深刻形象的理解.今 ...

  6. Centos7 安装高版本php

    1. 配置yum源 查看yum源的链接是不是有效的.可以参考此链接  https://webtatic.com/projects/yum-repository/ # rpm -Uvh http://f ...

  7. Oracle表锁住处理

    select object_id,session_id,locked_mode from v$locked_object; select b.owner,b.object_name,l.session ...

  8. Java面试题及答案(基础122道,编码19道)

    JAVA相关基础知识1.面向对象的特征有哪些方面 1.抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分,暂时 ...

  9. CSS绑定

    css绑定会对元素的CSS类进行操作.在某些情况下这将非常有用,例如:当数值是负的时将其高亮显示. (注:如果如果不想直接更改CSS类,而是只要改其中一个样式,则需要使用style绑定) 示例:使用静 ...

  10. laravel sum 多个字段

    laravel中怎么实现下面的SQL select sum('profit'),sum('order_count') from pro where......; 参考 self::where('id' ...