ios - 如何动画 CAShapeLayer 路径变化?

标签 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; }

获取带半径的圆:

- (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;}

最佳答案

尝试将“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"];

关于ios - 如何动画 CAShapeLayer 路径变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39744700/

相关文章:

ios - 多个具有 .round lineCap 的 CAShapeLayers 相互重叠

swift - 渲染显示在窗口外的 CALayer 内容

ios - textView :shouldChangeTextInRange:replacementText: returning NO, 但自动更正忽略?

iphone - 我可以完全删除 xib 文件吗?

ios - 如何从两个不同的 View 层次结构中 'link' 两个层?

ios - 覆盖类和在 Controller 中声明所有内容之间的区别?

iphone - UIImageView 边框颜色

IOS Swift 嵌套垂直 StackView

ios - Interface Builder 文件中的未知类 RAMAnimatedTabBarItem

ios - UIView 的背景颜色没有改变