【问题标题】:Drawing when not supposed to..?不应该画的时候……?
【发布时间】:2013-12-01 02:26:19
【问题描述】:

在我的应用程序中,我在用户点击并拖动时画了一个圆圈。他们点击的地方是圆的中心,当他们拖动时,半径达到他们的手指。一旦你画了一个圆圈,如果你点击几次(不拖动),什么都不会发生,但下次你点击并拖动时,你之前所做的每次点击都会创建圆圈(与最后一个圆圈大小相同的圆圈) .这对我来说没有意义,因为我打电话给touchesBegan:withEvent 的唯一事情是

UITouch *touch = [touches anyObject];
center = [touch locationInView:self];

它应该每次都替换中心,但是当您最终移动手指时,它会在每次点击时绘制一个单独的圆圈。

touchesMoved:

UITouch *touch = [touches anyObject];
endPoint = [touch locationInView:self];

distance = (uses center's attributes);

[self setNeedsDisplay];

touchesEnded:

[self drawBitmap];

绘制位图:

if (!CGPointEqualToPoint(center, CGPointZero) && !CGPointEqualToPoint(endPoint, CGPointZero)) {
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0.0);

    if (!incrementalImage) { // first draw;
        UIBezierPath *rectpath = [UIBezierPath bezierPathWithRect:self.bounds]; // enclosing bitmap by a rectangle defined by another UIBezierPath object
        [[UIColor clearColor] setFill];
        [rectpath fill]; // fill it
    }
    [incrementalImage drawAtPoint:CGPointZero];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, sW);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, o);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGRect rectangle = CGRectMake(center.x - distance, center.y - distance, distance * 2, distance * 2);
    CGContextAddEllipseInRect(context, rectangle);
    CGContextStrokePath(context);

    incrementalImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
}

绘制矩形

    [incrementalImage drawInRect:rect];

    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, sW);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), r, g, b, o);
    CGContextSetLineCap(context, kCGLineCapRound);
    CGRect rectangle = CGRectMake(center.x - distance, center.y - distance, distance * 2, distance * 2);
    CGContextAddEllipseInRect(context, rectangle);
    CGContextStrokePath(context);

为什么点按后会出现圆圈,但只有在拖动时才会出现?只应在拖动时绘制圆圈……对吗?我几乎 99% 确定问题出在 drawBitmap 上,但我不确定我应该怎么做才能检查它是否只是点击或拖动。另外,如果问题是drawBitmap,为什么我在touchesEnd时看不到圆圈?我只在拖动事件中看到它们。 EDIT 明确一点,我不希望在单击时只在拖动时绘制圆圈。

【问题讨论】:

    标签: ios objective-c cocoa-touch uiview drawrect


    【解决方案1】:

    您不会自己调用绘图例程。当系统准备好您的绘图时,必须调用它们——它通过调用您的drawRect: 表示它已准备好。您可以从那里运行任何您喜欢的绘图程序。您使用setNeedsDisplay 向系统表明您有一些需要绘制的东西。这就是touchesMoved: 导致绘制圆圈的原因。在touchesEnded: 中添加对setNeedsDisplay 的调用,您应该会看到您正在寻找的结果。

    【讨论】:

    • 对不起,应该指定这是一个覆盖 drawRect 的 UIView 子类。无论如何,我对其进行了编辑以提及我根本不想在单击时绘制圆圈。一次点击不应该只改变中心指针的值吗?
    • 不管你想要什么结果,根本的问题是你不能直接从touchesEnded:绘制,只能在源自drawRect:的调用栈中绘制,你不直接调用它。跨度>
    【解决方案2】:

    好吧,touchesMoved: 中只有 [self setNeedsDisplay];,这意味着如果你只点击,视图不会重绘。您不会在 touchesEnded: 上重绘。这解释了部分行为。

    至于其余部分,我会设置断点或 NSLog 来调试真正发生的事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      • 2020-07-16
      • 2010-10-10
      • 2010-12-30
      相关资源
      最近更新 更多