【问题标题】:How to plot a graph with CAShapeLayer?如何用 CAShapeLayer 绘制图形?
【发布时间】:2012-07-30 10:07:22
【问题描述】:

是否可以使用 CAShapeLayer 仅绘制具有给定线宽的曲线?它似乎只能绘制填充形状。我想绘制一个动画绘图图。只是一条线,不是一个形状。

【问题讨论】:

    标签: iphone ios ipad core-animation cashapelayer


    【解决方案1】:

    看来您实际上可以使用 CAShapeLayers 绘制线条。试试这样的:

    UIBezierPath *graphPath = [UIBezierPath bezierPath];
    [graphPath moveToPoint:CGPointMake(x, y)];
    [graphPath addLineToPoint:CGPointMake(x, y + h)];
    [graphPath closePath];
    

    然后放到一个CAShapeLayer中:

    CAShapeLayer *shapeLayer = [[CAShapeLayer alloc] init];
    [shapeLayer setFrame: aFrame];
    [shapeLayer setPath: [graphPath CGPath]];
    [shapeLayer setStrokeColor:[[UIColor blackColor] CGColor]];
    [shapeLayer setMasksToBounds:YES];
    

    您可以更改graphPath 以绘制您想要绘制的任何曲线/线。

    希望这会有所帮助!

    【讨论】:

    • 非常感谢。点赞!为了只得到这条线,我必须将 fillColor 设置为 nil。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多