please set CG_CONTEXT_SHOW_BACKTRACE environmental variable

此生再无相见时 提交于 2019-12-03 12:59:16

Invalid context errors happen, because you are trying to use drawing operations outside of drawRect: method, so no current graphic context is set.

Like on OS X, you should perform drawing in drawRect: method, and use setNeedsDisplay to update resulting image. And both setNeedsDisplay and setNeedsDisplayInRect: methods are available for UIView on iOS.

So, result should look like this:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches]anyObject];
CGPoint touchLocation = [touch locationInView:self];
myPath = [UIBezierPath bezierPath];
[myPath moveToPoint:touchLocation];
}

- (void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches]anyObject];
CGPoint touchLocation = [touch locationInView:self];
[myPath addLineToPoint:touchLocation];
[self setNeedsDisplay];
}

- (void) drawRect:(CGRect)rect {
[[UIColor blackColor]setStroke];
[[UIColor greenColor]setFill];
[myPath stroke];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!