一、编写一个简单的动画,使一个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. 使用SLT 工具从SAP导入数据到SAP HANA的监控

    使用SLT工具从SAP导入数据到SAP HANA主要有两种方式监控, 一是在SAP SLT服务器上使用以下T-Code: IUUC_SYNC_MON MWBMON 二是在SAP HANA Studio ...

  2. iOS中的布局

    1.UIView 有三个比较重要的布局属性: frame , bounds 和 center , CALayer 对应地叫做 frame , bounds 和 position .为了能清楚区分,图层 ...

  3. 敏捷开发(十一)- Scrum Sprint评审会议

    本文主要是为了检测你对SCRUM 评审会议的了解和使用程度, 通过本文你可以检测一下     1.你们的SCRUM 评审会议的过程和步骤    2.SCRUM 评审会议的输出结果一.会议目的     ...

  4. USACO 3.2 Contact

    ContactIOI'98 The cows have developed a new interest in scanning the universe outside their farm wit ...

  5. socket编程之TCP/UDP

    目标: 1.编写TCP服务端客户端,实现客户端发送数据,服务端接收打印 2.采用OOP方式编写TCP服务端客户端,实现客户端发送数据,服务端添加时间戳,返回给客户端 3.采用OOP方式编写UDP服务端 ...

  6. python字符串及正则表达式[转]

    原文链接:http://www.cnblogs.com/guojidong/archive/2012/12/20/2826388.html 字符串: 正则表达式 正则表达式元字符与语法图: 注意事项: ...

  7. 利用Rsync在windows和linux之间同步数据

    使用Rsync从windows同步文件到linux 1.windows服务端的安装与配置: 免费软件下载地址:http://linux.linuxidc.com/,用户名密码为:www.linuxid ...

  8. action解耦方式

    ServletAction方式,必须要有Servlet容器作支持 package com.hanqi.action; import javax.servlet.ServletContext; impo ...

  9. asp.net MVC Session锁的问题

    一直在用Session,对Session锁并没有太多的关注,可能是平时没有注意.前段时间突然发现,一个Ajax Get请求,直接访问地址,只需要几十ms,但是在页面中加载,却需要2s.最后定位到Ses ...

  10. Qt事件和事件循环

    在处理QT循环事件的时候遇到了问题,查了半天资料都没弄明白问题出在哪,后来找大牛同事问了一下,同事就给我写了QCoreApplication::processEvent()这个函数,好啦,终于搞定了, ...