ios - 将 CAShapeLayer 添加为不可见的子层

标签 ios objective-c cocoa-touch calayer cashapelayer

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

下面的代码是从 UIView 扩展的类中的.我正在访问 View 的 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];
}

最佳答案

您的代码没有任何问题。

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

设置lineWidth在这里没有帮助,因为 path of line 仅将其视为由一个点到另一个点形成的线,而不考虑 widthUIBezierPath .

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

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

关于ios - 将 CAShapeLayer 添加为不可见的子层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21608376/

相关文章:

ios - 如何使用括号内的字符串

ios - 使用 OCMock 模拟类方法不起作用

iphone - 带有 NSAttributedString 的 UITextView - 不应用属性

iphone - 处理 IB 中横向/纵向差异的最佳方法是什么?

ios - 如何在 iOS 中使用拖动项目

ios - Swift 3 - 在 View 数组上设置属性

iphone - NSPredicate 与 NSCompoundPredicate?

ios - UIImageView 不显示来自 imagePickerController 的图片

objective-c - 多线程崩溃中的 sizeWithFont!

iphone - 除非我手动将其设置为零,否则属性不会被释放。或者在 Instruments 中运行它