views:

138

answers:

1

I've implemented an UIScrollView which contains a grid of UIImageView objects. For both, the superview and it's subviews, userInteractionEnabled is YES. As the user scrolls and his/her finger touches an UIImageView, it imediately recognizes a tap on that, which is not what I want ;)

Inside of that UIImageView objects I have this method:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    if ([touch view] == self && [touch tapCount] >= 1) {
     NSLog(@"Cooooool! The user touched me!");
    }
}

Is there a way how I could recognize something like "oh no, the user actually wanted to scroll only, so don't take that touch too serious!"? Basically, what I need is the behavior of the home screen, where a finger may touch an icon, but when the user starts scrolling and lifts the finger off, the touched app will not be launched.

+2  A: 

UIScrollView has already implemented that behavior for you. If the user is quick enough to drag, your touchesBegan:withEvent: method will simply never be called.

If, on the other hand, the user is slow and the scroll view forwards the touch to your UIImageView subclass, but then only realizes the user meant to scroll, it will call your custom class' -(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event method.

Just implement it, and you'll be notified of cancelled touches.

Kenneth Ballenegger