CABasicAnimation 几种停止的回调
2015-10-28 01:18
295 查看
一、编写一个简单的动画,使一个UIview从屏幕的左上角移动到左下角,间隔时间3S
效果:
二、动画中断的几种情况
以上三种情况都会回调下面的方法
- (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是动画已经运动的时间间隔,暂停一段时间之后想回复,那么就相当于从当前时间点的 -[b]pauseTime开始动画。[/b]
// // 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是动画已经运动的时间间隔,暂停一段时间之后想回复,那么就相当于从当前时间点的 -[b]pauseTime开始动画。[/b]
相关文章推荐
- iOS 打包静态库.a文件
- 硬盘分区 1
- fragement(二)Fragment回退栈以及与Activity的交互方式
- 慎用not in 和 <>all 表达式
- iOS 9 新特性之实现 3D Touch
- 真机测试的常出现问题的总结
- fragment(一)加载方式
- 验证歌德巴赫猜想
- Android笔记(四十三) Android中的数据存储——SQLite(五)delete
- 关于windows 2003 + tomcat7.0响应速度慢的问题
- 10.28,python,django中templates的奇偶性,python,swift多语言文件转android多语言文件
- 模拟器SDK路径
- 利用SynchronizationContext.Current在线程间同步上下文
- background-size背景缩放
- ListView的使用和BaseAdapter
- iOS 即时视频和聊天(基于环信)
- vlc 播放器的点播和广播服务
- linux 修改ip
- Some cool FireMonkey multi-device components
- 数据结构2---算法概念