Search code examples
iphoneuiimageviewtouchesbegantouchesmoved

In subclassed UIImageView, touchesBegan fires, touchesMoved fires a couple times, but then stops for no reason


I have subclassed a UIImageView, and have implemented touchesBegan/Moved/Finished like this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"Began");

mouseSwiped = NO;
UITouch *touch = [touches anyObject];

if ([touch tapCount] == 2) {
    self.image = nil;
    return;
}

lastPoint = [touch locationInView:self];
lastPoint.y -= 20;

}




- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwiped = YES;
        NSLog(@"Moved");
    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self];
    currentPoint.y -= 20;


    UIGraphicsBeginImageContext(self.bounds.size);

    [self.image drawInRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 18.0);
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.5, 1.0, 1.0);
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    self.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        NSLog(@"Ended");
    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
        self.image = nil;
        return;
    }


    if(!mouseSwiped) {
        NSLog(@"here?");
        UIGraphicsBeginImageContext(self.bounds.size);
        [self.image drawInRect:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 18.0);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 0.5, 1.0, 1.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        self.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
}

And it appears to be working for the first little bit. TouchesBegan() fires every time, then when I start moving, touchesMoved() is fired sometimes 5 times, sometimes 3 times, sometimes 7 times, but then it just stops. The touchesEnded() is never fired, and I just don't see what is going on!

I've been staring at this for a while now, does anyone see something I am missing?


Solution

  • Override and implement: - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event

    See if that is being called.

    If so, something is cancelling your touch event. Could be low memory.