Josue Espinosa
Josue Espinosa

Reputation: 5089

Drawing when not supposed to..?

In my app, I draw a circle on the user's tap and drag. Where they tapped is the center of the circle and as they drag, the radius is up to their finger. Once you draw one circle, if you tap around a couple of times (without dragging), nothing happens but the next time you tap and drag, circles get created (the same sized circles as the last circle made) at each tap you made previously. This doesn't make sense to me because the only things I call on touchesBegan:withEvent are

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

It should be replacing center each time, but it draws an individual circle at each tap when you finally move your finger.

touchesMoved:

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

distance = (uses center's attributes);

[self setNeedsDisplay];

touchesEnded:

[self drawBitmap];

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();
}

drawRect

    [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);

Why do circles get made after the taps, but only appear when you drag? Circles should only be drawn when you drag... Right? I'm almost 99% sure the problem is drawBitmap, but i'm not sure what I should do to check if it was only a tap or a drag. Also, if the problem is drawBitmap, why don't I see circles when touchesEnd? It's only on drag events that I see them. EDIT to be clear, I don't want circles to be drawn at all on single taps, only on drags.

Upvotes: 0

Views: 81

Answers (2)

Erik B
Erik B

Reputation: 42554

Well, you only have [self setNeedsDisplay]; in touchesMoved:, which means that if you only tap, the view will not redraw. You do not redraw on touchesEnded:. So that explains part of the behavior.

As for the rest of it, I would put breakpoints or NSLogs to debug what's really happening.

Upvotes: 0

jscs
jscs

Reputation: 64002

You don't call drawing routines on your own. They have to be called when the system is ready for you do to your drawing -- it signals that it's ready by calling your drawRect:. You can run whatever drawing routines you like from there. You indicate to the system that you have something that needs to be drawn as soon as it's ready with setNeedsDisplay. That's why touchesMoved: is causing circles to be drawn. Add a call to setNeedsDisplay in touchesEnded: and you should see the results you're looking for.

Upvotes: 1

Related Questions