绘图GUI有时会被切掉
Drawing GUI will sometimes cut out
所以我正在开发一个 iOS 应用程序,它有一个 GUI,您可以在其中绘制图像、添加过滤器等。绘图功能有效,但有时会在开始时随机剪切绘图。基本上,如果您开始在屏幕上绘图并且它工作了半秒以上,您的设置将一直工作,直到您再次尝试;但是有时候当你开始画的时候,它会在一瞬间后无缘无故地中断。
// Draws a line from point1 to point2
- (void) drawOnImage:(CGPoint *)point1 :(CGPoint *)point2 {
UIGraphicsBeginImageContext(self.view.frame.size);
[self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), point1->x, point1->y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), point2->x, point2->y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
// Beginning of the drawing, only happens when you click but don't move around on the screen
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
if (currentAction == DRAW) {
[self backupImage];
//disable the renderImageView so that the gestures dont interfere
[self drawOnImage :&lastPoint :&lastPoint];
}
}
// When lines are being drawn on the image (when your finger is moving)
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (currentAction == DRAW) {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
[self drawOnImage :¤tPoint :&lastPoint];
lastPoint = currentPoint;
}
}
这是我的故事板布局;如您所见,代码会在用户绘制时创建线条的 CGRect,然后使用 CGRect 作为图像的一部分重新生成主图像。
这是一个问题的例子,看到立即结束的两个黑色绘图标记了吗?最上面的绘制工作并继续进行,其他两个立即被剪掉然后没有渲染任何其他线。
知道为什么会这样吗?起初我们以为是模拟器,但不是。
在解决另一个问题的过程中解决了这个问题。我想我有手势识别器混淆(这真的不应该发生,因为当用一根手指移动触摸时应该只有一个手势),尝试将手势识别器委托设置为正确的视图(通常是自我),如果你有
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
函数已实现,告诉它在某些情况下仅 return 为真。不过,我怀疑任何人都会遇到这个确切的问题,所以发布这个解决方案可能有点没用。
所以我正在开发一个 iOS 应用程序,它有一个 GUI,您可以在其中绘制图像、添加过滤器等。绘图功能有效,但有时会在开始时随机剪切绘图。基本上,如果您开始在屏幕上绘图并且它工作了半秒以上,您的设置将一直工作,直到您再次尝试;但是有时候当你开始画的时候,它会在一瞬间后无缘无故地中断。
// Draws a line from point1 to point2
- (void) drawOnImage:(CGPoint *)point1 :(CGPoint *)point2 {
UIGraphicsBeginImageContext(self.view.frame.size);
[self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), point1->x, point1->y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), point2->x, point2->y);
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush );
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0);
CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal);
CGContextStrokePath(UIGraphicsGetCurrentContext());
self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
// Beginning of the drawing, only happens when you click but don't move around on the screen
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
if (currentAction == DRAW) {
[self backupImage];
//disable the renderImageView so that the gestures dont interfere
[self drawOnImage :&lastPoint :&lastPoint];
}
}
// When lines are being drawn on the image (when your finger is moving)
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (currentAction == DRAW) {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
[self drawOnImage :¤tPoint :&lastPoint];
lastPoint = currentPoint;
}
}
这是我的故事板布局;如您所见,代码会在用户绘制时创建线条的 CGRect,然后使用 CGRect 作为图像的一部分重新生成主图像。
这是一个问题的例子,看到立即结束的两个黑色绘图标记了吗?最上面的绘制工作并继续进行,其他两个立即被剪掉然后没有渲染任何其他线。
知道为什么会这样吗?起初我们以为是模拟器,但不是。
在解决另一个问题的过程中解决了这个问题。我想我有手势识别器混淆(这真的不应该发生,因为当用一根手指移动触摸时应该只有一个手势),尝试将手势识别器委托设置为正确的视图(通常是自我),如果你有
func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
函数已实现,告诉它在某些情况下仅 return 为真。不过,我怀疑任何人都会遇到这个确切的问题,所以发布这个解决方案可能有点没用。