动画组CAAnimationGroup

我们建立了一列基础动画,和简单的增加他们到层上面。如果你想要所有的动

画开始在同样的时间,并且他们中每个动画都有同样的执行时间,这个方法是足够了 

- (IBAction)animate:(id)sender;
{
NSRect oldRect = NSMakeRect(0.0, 0.0, 100.0, 100.0);
NSRect newRect = NSMakeRect(0.0, 0.0, 300.0, 300.0);
CABasicAnimation *boundsAnimation = [CABasicAnimation animationWithKeyPath:@”bounds”];
[boundsAnimation setFromValue:[NSValue valueWithRect:oldRect]];
[boundsAnimation setToValue:[NSValue valueWithRect:newRect]];
[boundsAnimation setDuration:5.0f];

CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@”position”];
[positionAnimation setFromValue:[NSValue valueWithPoint:NSPointFromCGPoint([layer position])]];
[positionAnimation setToValue:[NSValue valueWithPoint:NSMakePoint(0.0, 0.0)]];
[positionAnimation setDuration:5.0f];

CABasicAnimation *borderWidthAnimation = [CABasicAnimation animationWithKeyPath:@”borderWidth”];
[borderWidthAnimation setFromValue:[NSNumber numberWithFloat:5.0f]];
[borderWidthAnimation setToValue:[NSNumber numberWithFloat:30.0f]];
[borderWidthAnimation setDuration:5.0f];

[layer addAnimation:boundsAnimation forKey:@”bounds”]; [layer addAnimation:positionAnimation forKey:@”position”]; [layer addAnimation:borderWidthAnimation forKey:@”borderWidth”];
}

 

每个动画都有 5 秒的执行时间,并且它们在下个循环里一起播放,最后同时结束。层的位置到左下角,层 的边框宽度增加 30 个像素,和层的尺寸增加从 100x100 像素到了 300x300 像素。

让我们说下我们要做的情况,并不是使所有的动画同时播放,我们想要它们按顺序播放之前定义好的顺序。 我们可以完成这些,通过使用动画组合设定 beginTime 这个属性的区域。

我们必须显示的指定我们组动画的执行时间,以便于能为每个动画分离一部分时间。例如,我们设定我们 的动画时间为 15 秒钟,然后给每个动画 5 秒钟的播放时间。

- (IBAction)animate:(id)sender;
{
NSRect oldRect = NSMakeRect(0.0, 0.0, 100.0, 100.0);
NSRect newRect = NSMakeRect(0.0, 0.0, 300.0, 300.0);
CABasicAnimation *boundsAnimation = [CABasicAnimation animationWithKeyPath:@”bounds”];
[boundsAnimation setFromValue:[NSValue valueWithRect:oldRect]]; [boundsAnimation setToValue:[NSValue valueWithRect:newRect]];
[boundsAnimation setDuration:15.0f]; [boundsAnimation setBeginTime:0.0f];

CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@”position”]; [positionAnimation setFromValue: [NSValue valueWithPoint:NSPointFromCGPoint([layer position])]]; [positionAnimation setToValue:[NSValue valueWithPoint:NSMakePoint(0.0, 0.0)]]; [positionAnimation setDuration:15.0f]; [positionAnimation setBeginTime:5.0f];

CABasicAnimation *borderWidthAnimation = [CABasicAnimation animationWithKeyPath:@”borderWidth”];
[borderWidthAnimation setFromValue:[NSNumber numberWithFloat:5.0f]];
[borderWidthAnimation setToValue:[NSNumber numberWithFloat:30.0f]];
[borderWidthAnimation setDuration:15.0f]; [borderWidthAnimation setBeginTime:10.0f];

CAAnimationGroup *group = [CAAnimationGroup animation]; [group setDuration:15];

[group setAnimations:
[NSArray arrayWithObjects:boundsAnimation, positionAnimation,
borderWidthAnimation, nil]];
[layer addAnimation:group forKey:nil];

 

我们为每个分割的动画都设定了 15 秒的执行时间,但是每个动画的开始时间分别为 0.0,5.0,和 10.0.

你也注意到了我们仅仅增加组动画给层。组动画对象通过调用-setAnimations 这个方法增加。 

posted @ 2012-12-20 17:29  不曾拥有  阅读(5783)  评论(0编辑  收藏  举报