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(, , , )];
_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 几种停止的回调的更多相关文章
- C++使用模板、函数指针、接口和lambda表达式这四种方法做回调函数的区别比较
在C++中,两个类之间存在一种关系,某个类需要另外一个类去完成某一个功能,完成了之后需要告知该类结果,这种最普通最常见的需求,往往使用回调函数来解决. 如题,我总结下来有这么四种方式可以完成这项功能, ...
- [转]C/C++实现回调机制的几种方式(回调、槽、代理)
转自:https://www.jianshu.com/p/4f907bba6d5f (1)Callback方式(回调) Callback的本质是设置一个函数指针进去,然后在需要需要触发某个事件时调用该 ...
- java四种引用与回调函数
JAVA四种引用 java对象的引用包括: 强引用 软引用 弱引用 虚引用 Java中提供这四种引用类型主要有两个目的: 第一是可以让程序员通过代码的方式决定某些对象的生命周期: 第二是有利于JVM进 ...
- 线程(Thread)的四种停止方式
1.正常的程序启动,停止 2.使用退出标记,一般程序在run()方法后,线程会正常结束.但是有一些伺服线程还在运行,他们运行时间较长,只有当外部条件满足时,他们才会停止.实现如下: public cl ...
- Python Twisted系列教程17:造”回调”的另一种方法
作者:dave@http://krondo.com/just-another-way-to-spell-callback/ 译者: Cheng Luo 你可以从”第一部分 Twist理论基础“开始阅 ...
- [教程]Delphi 中三种回调函数形式解析
Delphi 支持三种形式的回调函数 全局函数这种方式几乎是所有的语言都支持的,类的静态函数也可以归为此类,它保存的只是一个函数的代码起始地址指针( Pointer ).在 Delphi 中声明一般为 ...
- Springboot 优雅停止服务的几种方法
在使用Springboot的时候,都要涉及到服务的停止和启动,当我们停止服务的时候,很多时候大家都是kill -9 直接把程序进程杀掉,这样程序不会执行优雅的关闭.而且一些没有执行完的程序就会直接退出 ...
- [WCF编程]10.操作:回调操作
一.回调操作概述 WCF支持服务将调用返回给它的客户端.在回调期间,许多方面都将颠倒过来:服务将成为客户端,客户端将编程服务.回调操作可以用在各种场景和应用程序中,但在涉及事件或者服务发生时间需要通知 ...
- Hystrix微服务容错处理及回调方法源码分析
前言 在 SpringCloud 微服务项目中,我们有了 Eureka 做服务的注册中心,进行服务的注册于发现和服务治理.使得我们可以摒弃硬编码式的 ip:端口 + 映射路径 来发送请求.我们有了 F ...
随机推荐
- linux kernel 编译
lin
- Sublime Text3下如何快速搭建开发环境
安装好Sublime Text3之后,简单几步就可以搭建一个好用的开发环境. sublime的设置包括自定义设置以及插件系统. 打开菜单Preferences -> Settings,编辑自定义 ...
- mysql与oracle在groupby语句上的细节差异
前言 之所以去纠那么细节的问题,是因为之前有过一个这样的场景: 有个同学,给了一条数据库的语句给我,问,为啥这样子的语句在oracle语句下执行不了. select * from xx where x ...
- <hdu - 1232> 畅通工程 并查集问题 (注意中的细节)
本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 结题思路:因为题目是汉语的,那我就不解释题意了,要求的是最少建设的道路,我们可以用并查集来做这 ...
- Labeling Balls(变种拓扑)
Labeling Balls Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Tot ...
- DOM小解
现在来说说DOM 文档对象模型DOM(Document Object Model)定义访问和处理html文档的标准方法.DOM将html文档呈现为带有元素 ,属性和文本的树结构(节点树) 先来看看 ...
- [河南省ACM省赛-第三届] 网络的可靠性 (nyoj 170)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=170 根据题意,需要找到度数为1的结点个数,如下图: #include<iostre ...
- 一篇顺手的Ubuntu+caffe配置笔记
主要参考: https://github.com/lbzhang/dl-setup http://ouxinyu.github.io/Blogs/20151108001.html http://www ...
- VFL语言
VFL语言 VFL即Visual Format Language,可视化格式语言 NSDictionaryOfVariableBindings(testViewA, testViewB):此为一个宏, ...
- Unity属性(Attributes)
Unity3d中的属性(Attributes) Attributes属性属于U3D的RunTimeClass,所以加上以下的命名空间是必须的了. using UnityEngine; using Sy ...