如何动画CAShapleLayer的路径变化?

时间:2016-09-28 10:21:10

标签: ios objective-c calayer cashapelayer

我有一个CAShapeLayer,其中包含一个“洞”和一个透明的环绕(see screenshot)。我想动画那个圆的半径变化(我想让它更大或更小)。有任何想法吗 ?

这是我的图层:

CAShapeLayer *fillLayer = [self getCircleLayerWithRadius:self.view.frame.size.width/2];

getCirleWithRadius函数:

-(CAShapeLayer *)getCircleLayerWithRadius:(NSInteger)radius {
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.view.frame cornerRadius:0];
UIBezierPath *circlePath = [self getCirclePathWithRadius:radius];
[path appendPath:circlePath];
path.usesEvenOddFillRule = YES;
CAShapeLayer *fillLayer = [CAShapeLayer layer];
fillLayer.path = path.CGPath;
fillLayer.fillRule = kCAFillRuleEvenOdd;
fillLayer.fillColor = [UIColor grayColor].CGColor;
fillLayer.opacity = 0.5;
return fillLayer; }

getCircleWithRadius:

- (UIBezierPath *)getCirclePathWithRadius:(NSInteger)radius {
CGRect rect = self.view.frame;
UIBezierPath *circlePath;
circlePath = [UIBezierPath bezierPathWithArcCenter:(CGPoint){rect.size.width/2, rect.size.height/2} radius:radius startAngle:0 endAngle:M_PI*2 clockwise:YES];
return circlePath;}

1 个答案:

答案 0 :(得分:1)

尝试使用' CABasicAnimation'使用@"path"键。 以下是配置动画的示例:

    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
    animation.duration = [CATransaction animationDuration];
    animation.timingFunction = [CATransaction animationTimingFunction];
    animation.fromValue = (id)oldPath;
    animation.toValue = (id)path;
    [fillLayer addAnimation:animation forKey:@"pathAnimation"];