如何使用多个子图层为CALayer设置动画

时间:2016-04-05 14:13:51

标签: objective-c caanimation

我有一个CALyer forgroundLayer,它有3个不同颜色的子图层。现在,我想要显示从0位到宽度的这些子图层,所以我尝试将动画添加到forgroundLayer,但它不起作用。

CALayer *_subLayer1 = [CALayer layer];
CALayer *_subLayer2 = [CALayer layer];
CALayer *_subLayer3 = [CALayer layer];
CALayer *_subLayer4 = [CALayer layer];

[_foregroundLayer addSublayer:_subLayer1];
[_foregroundLayer addSublayer:_subLayer2];
[_foregroundLayer addSublayer:_subLayer3];
[_foregroundLayer addSublayer:_subLayer4];

_subLayer1.position = NSMakePoint(0, 0);
_subLayer1.backgroundColor = [NSColor colorWithCalibratedRed:61/255.0 green:191/255.0 blue:227/255.0 alpha:1].CGColor;
_subLayer1.bounds = NSMakeRect(0, 0, 59.742357, self.frame.size.height);

_subLayer2.position = NSMakePoint(5, 0);
_subLayer2.backgroundColor = [NSColor colorWithCalibratedRed:143/255.0 green:213/255.0 blue:112/255.0 alpha:1].CGColor;
_subLayer2.bounds = NSMakeRect(0, 0, 3, self.frame.size.height);

_subLayer3.position = NSMakePoint(8, 0);
_subLayer3.backgroundColor = [NSColor colorWithCalibratedRed:255/255.0 green:0/255.0 blue:112/255.0 alpha:1].CGColor;
_subLayer3.bounds = NSMakeRect(0, 0, 2, self.frame.size.height);



CABasicAnimation * resize = [CABasicAnimation animationWithKeyPath:@"bounds"];
    resize.duration = 3.0f;
    resize.fromValue = [NSValue valueWithRect:NSRectFromCGRect(NSMakeRect(0, 0, 0, self.foregroundLayer.bounds.size.height))];
    resize.toValue = [NSValue valueWithRect:NSRectFromCGRect(self.foregroundLayer.bounds)];
    resize.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [self.foregroundLayer addAnimation:resize forKey:@"resize"];

0 个答案:

没有答案