【问题标题】:Adding CAShapeLayer as sublayer not visible将 CAShapeLayer 添加为不可见的子层
【发布时间】:2014-02-06 16:23:10
【问题描述】:

我第一次体验CALayers,毫无疑问我在做一些非常简单、非常错误的事情。

以下代码位于从UIView 扩展的类中。我正在访问视图的 CALayer,这很好,并尝试根据用户触摸绘制的线附加子层。 (CGPath)

通过将背景设置为亮橙色,我可以看到我将正确的 CALayer 传递到我的函数中,但是如果我尝试将 CAShapeLayer *line 设置为橙色 - 我似乎无法在屏幕上看到它。

我在想我把这两者联系在一起是错误的,或者错过了定义一些大小或其他东西,但我没有什么东西可以尝试。谁能直截了当?

(void)makeLineLayer:(CALayer *)layer{
    CAShapeLayer *line = [CAShapeLayer layer];
    //This line sets the view to orange - so I know 'layer' is pointing to the right thing
    //layer.backgroundColor = [UIColor orangeColor].CGColor;
    UIBezierPath *linePath=[UIBezierPath bezierPath];
    [linePath moveToPoint:self.lineStart];
    [linePath addLineToPoint:self.lineEnd];
    [linePath setLineWidth:60.0];

    line.path=linePath.CGPath;
    //This below line is meant to show me where my sublayer is - I currently can't see it.
    line.fillColor = [UIColor orangeColor].CGColor;
    line.opacity = 1.0;
    [layer addSublayer:line];
}

【问题讨论】:

    标签: ios objective-c cocoa-touch calayer cashapelayer


    【解决方案1】:

    那里的代码没有任何问题。

    但是有一个问题很突出——没有什么可以“填补”的,因为你只有一个点可以移动。

    在此处设置 lineWidth 无济于事,因为线的 path 仅将其视为一个点到另一个点形成的线,而不考虑 UIBezierPathwidth

    如果您希望看到您的代码确实有效,请添加此代码并查看它是否有效:

    UIBezierPath *linePath=[UIBezierPath bezierPath];
    [linePath moveToPoint:self.lineStart];
    [linePath addLineToPoint:self.lineEnd];
    
    [linePath addLineToPoint:(CGPoint){250.0f, 350.0f}]; <<=== random points I chose.
    
    [linePath setLineWidth:60.0];
    

    玩得开心。希望这会有所帮助。

    附录,如果您只想显示一行并且只显示一行,请添加以下内容:

    line.strokeColor = [UIColor <YOUR_COLOR>].CGColor;
    

    然后你可以删除我提到的这个:

    [linePath addLineToPoint:(CGPoint){250.0f, 350.0f}];
    

    【讨论】:

    • 好地方,谢谢。知道它必须是愚蠢的,它没有复杂到复杂的代码!不,图像最终会沿着这条路径移动,所以我需要额外的点,但我可以计算出复制 60 的宽度。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多