将CAShapeLayer添加为子图层不可见 [英] Adding CAShapeLayer as sublayer not visible

查看:77
本文介绍了将CAShapeLayer添加为子图层不可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次体验 CALayers ,并且毫无疑问地做了非常简单,非常错误的事情。

I'm having my first experience with CALayers, and am no doubt doing something very simple, very wrong.

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

The below code is in a class extended from a UIView. I am accessing the CALayer of the view, which is fine, and the attempting to attach a sublayer, based on a line drawn by the users touch. (The CGPath)

通过将背景设置为亮橙色,我可以看到我通过了右边 CALayer 放到我的函数中,但是如果我尝试将 CAShapeLayer * line 设置为橙色-我似乎没有可以在屏幕上看到它。

By setting the background to bright orange, I can see that I'm passing the right CALayer into my function, yet if I try and set the CAShapeLayer *line to orange - I don't seem to be able to see it on the screen.

我在想我将两个连接错了,或者错过了定义一些尺寸或东西的感觉,但是我很新鲜尝试的事情。谁能把我弄直?

I'm imagining I've connected the two together wrong, or missed defining some size or something, but am fresh out of things to try. Can anyone set me straight?

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


推荐答案

代码在那里。

但是一个问题很突出-鉴于您只有一点要搬到那里,所以没有什么可以填补的。

But one problem stands out - there is nothing to "fill" given that you only have a point to move to.

设置 lineWidth 在这里无济于事,因为行的 path 仅将其视为已形成的行一个点指向另一个点,而不考虑 UIBezierPath 宽度

Setting lineWidth would not help here because the path of line only sees it as a line that is formed one a point to another, not taking account the width of the UIBezierPath.

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

If you would like to see that your code does in fact works, add this and see that it does:

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];

玩得开心。希望对您有所帮助。

Have fun. Hope this helps.

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

Addendum, if all you want is to show the line and a line only, please add the following:

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

然后您可以删除我提到的内容:

And you can then remove this I mentioned:

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

这篇关于将CAShapeLayer添加为子图层不可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆