动画组CAAnimationGroup

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

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

?
1
2
3
4
5
6
7
8
9
10
- (IBAction)animate:(id)sender;
{
NSRect oldRect = NSMakeRect(0.0, 0.0, 100.0, 100.0); <br>NSRect newRect = NSMakeRect(0.0, 0.0, 300.0, 300.0);<br> CABasicAnimation *boundsAnimation =
[CABasicAnimation animationWithKeyPath:@”bounds”]; <br>[boundsAnimation setFromValue:[NSValue valueWithRect:oldRect]]; <br>[boundsAnimation setToValue:[NSValue valueWithRect:newRect]]; <br>[boundsAnimation setDuration:5.0f];<br><br>
CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@”position”]; <br>[positionAnimation setFromValue:[NSValue valueWithPoint:NSPointFromCGPoint([layer position])]];<br>[positionAnimation setToValue:[NSValue valueWithPoint:NSMakePoint(0.0, 0.0)]]; <br>[positionAnimation setDuration:5.0f];<br><br>
CABasicAnimation *borderWidthAnimation =
[CABasicAnimation animationWithKeyPath:@”borderWidth”]; <br>[borderWidthAnimation setFromValue:[NSNumber numberWithFloat:5.0f]]; <br>[borderWidthAnimation setToValue:[NSNumber numberWithFloat:30.0f]]; <br>[borderWidthAnimation setDuration:5.0f];<br><br>
[layer addAnimation:boundsAnimation forKey:@”bounds”];
[layer addAnimation:positionAnimation forKey:@”position”];
[layer addAnimation:borderWidthAnimation forKey:@”borderWidth”]; }

 

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- (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); <br>CABasicAnimation *boundsAnimation =
[CABasicAnimation animationWithKeyPath:@”bounds”]; <br>[boundsAnimation setFromValue:[NSValue valueWithRect:oldRect]];
[boundsAnimation setToValue:[NSValue valueWithRect:newRect]]; <br>[boundsAnimation setDuration:15.0f];
[boundsAnimation setBeginTime:0.0f];<br><br>
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];<br><br>
CABasicAnimation *borderWidthAnimation =
[CABasicAnimation animationWithKeyPath:@”borderWidth”]; <br>[borderWidthAnimation setFromValue:[NSNumber numberWithFloat:5.0f]]; <br>[borderWidthAnimation setToValue:[NSNumber numberWithFloat:30.0f]]; <br>[borderWidthAnimation setDuration:15.0f];
[borderWidthAnimation setBeginTime:10.0f];<br><br>
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 这个方法增加。 

转自:http://www.cnblogs.com/bucengyongyou/archive/2012/12/20/2826766.html

你可能感兴趣的:(动画组CAAnimationGroup)