1. 程式人生 > >動畫組(顯示動畫)

動畫組(顯示動畫)

cab rec red mage point creat __bridge pat 關鍵幀動畫

動畫組

CABasicAnimationCAKeyframeAnimation僅僅作用於單獨的屬性,而CAAnimationGroup可以把這些動畫組合在一起。CAAnimationGroup是另一個繼承於CAAnimation的子類,它添加了一個animations數組的屬性,用來組合別的動畫。我們把清單8.6那種關鍵幀動畫和調整圖層背景色的基礎動畫組合起來(清單8.10),結果如圖8.3所示。

清單8.10 組合關鍵幀動畫和基礎動畫

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     //create a path
 5
UIBezierPath *bezierPath = [[UIBezierPath alloc] init]; 6 [bezierPath moveToPoint:CGPointMake(0, 150)]; 7 [bezierPath addCurveToPoint:CGPointMake(300, 150) controlPoint1:CGPointMake(75, 0) controlPoint2:CGPointMake(225, 300)]; 8 //draw the path using a CAShapeLayer 9 CAShapeLayer *pathLayer = [CAShapeLayer layer];
10 pathLayer.path = bezierPath.CGPath; 11 pathLayer.fillColor = [UIColor clearColor].CGColor; 12 pathLayer.strokeColor = [UIColor redColor].CGColor; 13 pathLayer.lineWidth = 3.0f; 14 [self.containerView.layer addSublayer:pathLayer]; 15 //add a colored layer 16 CALayer *colorLayer = [CALayer layer];
17 colorLayer.frame = CGRectMake(0, 0, 64, 64); 18 colorLayer.position = CGPointMake(0, 150); 19 colorLayer.backgroundColor = [UIColor greenColor].CGColor; 20 [self.containerView.layer addSublayer:colorLayer]; 21 //create the position animation 22 CAKeyframeAnimation *animation1 = [CAKeyframeAnimation animation]; 23 animation1.keyPath = @"position"; 24 animation1.path = bezierPath.CGPath; 25 animation1.rotationMode = kCAAnimationRotateAuto; 26 //create the color animation 27 CABasicAnimation *animation2 = [CABasicAnimation animation]; 28 animation2.keyPath = @"backgroundColor"; 29 animation2.toValue = (__bridge id)[UIColor redColor].CGColor; 30 //create group animation 31 CAAnimationGroup *groupAnimation = [CAAnimationGroup animation]; 32 groupAnimation.animations = @[animation1, animation2]; 33 groupAnimation.duration = 4.0; 34 //add the animation to the color layer 35 [colorLayer addAnimation:groupAnimation forKey:nil]; 36 }

技術分享

動畫組(顯示動畫)