设为首页 加入收藏

TOP

CAMediaTiming`协议(9.1 图层时间)(一)
2017-10-11 13:57:49 】 浏览:1629
Tags:CAMediaTiming 协议 9.1 时间

#CAMediaTiming`协议

CAMediaTiming协议定义了在一段动画内用来控制逝去时间的属性的集合,CALayerCAAnimation都实现了这个协议,所以时间可以被任意基于一个图层或者一段动画的类控制。

持续和重复

我们在第八章“显式动画”中简单提到过durationCAMediaTiming的属性之一),duration是一个CFTimeInterval的类型(类似于NSTimeInterval的一种双精度浮点类型),对将要进行的动画的一次迭代指定了时间。

这里的一次迭代是什么意思呢?CAMediaTiming另外还有一个属性叫做repeatCount,代表动画重复的迭代次数。如果duration是2,repeatCount设为3.5(三个半迭代),那么完整的动画时长将是7秒。

durationrepeatCount默认都是0。但这不意味着动画时长为0秒,或者0次,这里的0仅仅代表了“默认”,也就是0.25秒和1次,你可以用一个简单的测试来尝试为这两个属性赋多个值,如清单9.1,图9.1展示了程序的结果。

清单9.1 测试durationrepeatCount

 1 @interface ViewController ()
 2 
 3 @property (nonatomic, weak) IBOutlet UIView *containerView;
 4 @property (nonatomic, weak) IBOutlet UITextField *durationField;
 5 @property (nonatomic, weak) IBOutlet UITextField *repeatField;
 6 @property (nonatomic, weak) IBOutlet UIButton *startButton;
 7 @property (nonatomic, strong) CALayer *shipLayer;
 8 
 9 @end
10 
11 @implementation ViewController
12 
13 - (void)viewDidLoad
14 {
15     [super viewDidLoad];
16     //add the ship
17     self.shipLayer = [CALayer layer];
18     self.shipLayer.frame = CGRectMake(0, 0, 128, 128);
19     self.shipLayer.position = CGPointMake(150, 150);
20     self.shipLayer.contents = (__bridge id)[UIImage imageNamed: @"Ship.png"].CGImage;
21     [self.containerView.layer addSublayer:self.shipLayer];
22 }
23 
24 - (void)setControlsEnabled:(BOOL)enabled
25 {
26     for (UIControl *control in @[self.durationField, self.repeatField, self.startButton]) {
27         control.enabled = enabled;
28         control.alpha = enabled? 1.0f: 0.25f;
29     }
30 }
31 
32 - (IBAction)hideKeyboard
33 {
34     ?[self.durationField resignFirstResponder];
35     [self.repeatField resignFirstResponder];
36 }
37 
38 - (IBAction)start
39 {
40     CFTimeInterval duration = [self.durationField.text doubleva lue];
41     float repeatCount = [self.repeatField.text floatValue];
42     //animate the ship rotation
43     CABasicAnimation *animation = [CABasicAnimation animation];
44     animation.keyPath = @"transform.rotation";
45     animation.duration = duration;
46     animation.repeatCount = repeatCount;
47     animation.byValue = @(M_PI * 2);
48     animation.delegate = self;
49     [self.shipLayer addAnimation:animation forKey:@"rotateAnimation"];
50     //disable controls
51     [self setControlsEnabled:NO];
52 }
53 
54 - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
55 {
56     //reenable controls
57     [self setControlsEnabled:YES];
58 }
59 
60 @end
View Code

图9.1 演示durationrepeatCount的测试程序

创建重复动画的另一种方式是使用repeatDuration属性,它让动画重复一个指定的时间,而不是指定次数。你甚至设置一个叫做autoreverses的属性(BOOL类型)在每次间隔交替循环过程中自动回放。这对于播放一段连续非循环的动画很有用,例如打开一扇门,然后关上它(图9.2)。

 

图9.2 摆动门的动画

对门进行摆动的代码见清单9.2。我们用了autoreverses来使门在打开后自动关闭,在这里我们把repeatDuration设置为INFINITY,于是动画无限循环播放,设置repeatCountINFINITY也有同样的效果。注意repeatCountrepeatDuration可能会相互冲突,所以你只要对其中一个指定非零值。对两个属性都设置非0值的行为没有被定义。

清单9.2 使用autoreverses属性实现门的摇摆

 

@interface ViewController ()

@property (nonatomic, weak) UIView *containerView;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //add the door
    CALayer *doorLayer = [CALayer layer];
    doorLayer.frame = CGRectMake(0, 0, 128, 256);
    doorLayer.position = CGPointMake(150 - 64, 150);
    doorLayer.anchorPoint = CGPointMake(0, 0.5);
    doorLayer.contents = (__bridge id)[
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇iOS9 App Thinning(应用瘦身)功能.. 下一篇iOS9 App Thinning(应用瘦身)功能..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目