動畫組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 這個方法增加。 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM