根据路径构建绘制CGPath

4

是否可以在每个线段被添加时绘制CGPath,而不是一次性绘制所有点的路径?我希望能够看到路径在添加线段时逐步绘制出来。

另外,是否可以在较大的路径中一次只绘制一条线段,而不是每次都用一条额外的线段重复绘制路径?

2个回答

4

如果您使用CAShapeLayer,并将路径设置为其路径属性,则可以动画化strokeEnd值。

我不知道如何动态添加到此,但我以前用过它来沿途动画线条的时间点(但这些线条的整个路径都是预先已知的)。


3

@wattson12 绝对正确。这是我过去如何做的一个例子:

- (void)animateBezier
{
    CAShapeLayer *bezier = nil;
    UIBezierPath *bezierPath = [self bezierPath]; // clearly, generate your path anyway you want

    bezier = [[CAShapeLayer alloc] init];

    bezier.path = bezierPath.CGPath;
    bezier.strokeColor = [UIColor redColor].CGColor;
    bezier.fillColor   = [UIColor clearColor].CGColor;
    bezier.lineWidth   = 5.0;
    bezier.strokeStart = 0.0;
    bezier.strokeEnd   = 1.0;
    [self.view.layer addSublayer:bezier];

    CABasicAnimation *animateStrokeEnd = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animateStrokeEnd.duration  = 1.0;
    animateStrokeEnd.fromValue = [NSNumber numberWithFloat:0.0f];
    animateStrokeEnd.toValue   = [NSNumber numberWithFloat:1.0f];
    [bezier addAnimation:animateStrokeEnd forKey:@"strokeEndAnimation"];
}

我不知道这是否符合您的要求,或者您是否还希望稍后动画添加路径中的其他点。如果这是您想要的,您可能可以调整fromValue,或者将另一个段绘制为单独的路径进行动画处理。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接