我从斯坦福 ios7 类(class)中复制了以下代码,用于向 View 添加贝塞尔曲线路径。然而,当我将此代码包含在 ViewDidLoad 中时,它没有在屏幕上显示任何内容
UIBezierPath *path = [[UIBezierPath alloc] init];
[path moveToPoint:CGPointMake(75, 10)];
[path addLineToPoint: CGPointMake(160, 150)];
[path addLineToPoint:CGPointMake(10, 150)];
[[UIColor whiteColor] setStroke];
[[UIColor whiteColor] setFill];
[path stroke];
[path fill];
之后,我添加了这个日志语句 NSLog(@"path: %@", path);
打印了这个不祥的错误消息。
This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
你能解释一下我做错了什么吗?
最佳答案
我通过创建一个 CAShapeLayer 并将其添加为 View 层的子层来完成此操作。巧妙的是,您可以使用 UIBezierPath 的 CGPath 属性来设置图层。
CAShapeLayer *layer = [CAShapeLayer layer];
layer.path = path.CGPath;
layer.strokeColor = [UIColor whiteColor].CGColor;
[self.view.layer addSublayer:layer];
关于ios - 贝塞尔路径在 View 中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23065217/