我第一次体验 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 仅将其视为由一个点到另一个点形成的线,而不考虑 width
的UIBezierPath
.
如果您想看到您的代码确实有效,请添加此代码并查看它是否有效:
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/