【问题标题】:Drawing GUI will sometimes cut out绘图 GUI 有时会被切掉
【发布时间】:2016-08-04 17:19:49
【问题描述】:

所以我正在开发一个具有 GUI 的 iOS 应用程序,您可以在其中绘制图像、添加过滤器等。绘图功能有效,但有时会在绘图开始时随机剪切。基本上,如果你开始在屏幕上绘图并且它工作了半秒以上,你的设置,它会一直工作,直到你再试一次;但是有时当您开始绘图时,它会在瞬间无缘无故地消失。

// 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 :&currentPoint :&lastPoint];
        lastPoint = currentPoint;

    }
}

这是我的故事板布局;如您所见,代码在用户绘制时创建线条的 CGRects,然后重新生成主图像,并将 CGRect 作为图像的一部分。

这是问题的一个例子,看到两个立即结束的黑色绘图标记了吗?顶部的绘制成功并继续进行,其他两条立即切掉,然后没有渲染任何其他线条。

任何想法为什么会发生这种情况?一开始我们以为是模拟器,其实不是。

【问题讨论】:

  • 我在这里看不到任何会导致您描述的行为的东西。当然,这可能没有它应该的那么有效,但我认为没有理由“删减”它。我建议将日志消息放在其中一些例程中,这样您就可以缩小范围(它根本没有收到touchesMoved,还是在图中)。
  • 嘿,我实际上解决了这个问题。我不确定是什么做的,但我会在下面的帖子中给出我最好的想法
  • 是的,问题不在问题中提供的代码中。我测试了它,它工作得很好。一定是其他原因(例如,某些东西将 currentAction 设置为 DRAW 以外的东西,阻塞了主队列等)。
  • 给你的一些不相关的建议: 1. 当你重绘图像时,你可能想使用UIGraphicsBeginImageContextWithOptions,比例为0。现在,在视网膜设备上,图像会有点像素化。 2. 与其每次都重绘整个图像,不如考虑使用带有路径的CAShapeLayer。它将更加高效。仅每 50-100 个点执行此快照方法(并重置路径)。如果你这样做,你的道路会更加顺畅。 3. 考虑使用合并触摸(使其平滑)和预测触摸(使其感觉更灵敏)。
  • 如果您有兴趣,这里有一篇展示如何绘制平滑路径的帖子。这是一个 Swift 实现,但说明了我上面讨论过的一些概念。 stackoverflow.com/a/34583708/1271826

标签: ios objective-c drawing draw


【解决方案1】:

在修复另一个问题的过程中修复了该问题。我想我有手势识别器混淆(这真的不应该发生,因为当触摸用一根手指移动时应该只有一个手势),尝试将手势识别器代表设置为正确的视图(通常是自我),如果你有

func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWithGestureRecognizer otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

函数实现,告诉它只在某些情况下返回true。不过,我怀疑有人会遇到这个确切的问题,所以发布这个解决方案可能有点没用。

【讨论】:

    猜你喜欢
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 1970-01-01
    相关资源
    最近更新 更多