How can I detect touch points in my UIScrollView
? The touches delegate methods are not working.

- 63,694
- 13
- 151
- 195
4 Answers
Set up a tap gesture recognizer:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
[scrollView addGestureRecognizer:singleTap];
and you will get the touches in:
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
CGPoint touchPoint=[gesture locationInView:scrollView];
}
-
6You sir are a genius! And all these posts about subclassing UIScrollView, pah! – Oliver Pearmain Jun 08 '11 at 16:10
-
1I used your code. but due to UITapGestureRecognizer, button click on scroll view is not working..? – Ankur Sep 26 '12 at 12:30
-
2@Hercules add controller as delegate to recognizer and use this function: - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ if([touch.view isKindOfClass:[UIScrollView class]]) return YES; else return NO; } – yuf Oct 12 '12 at 22:42
-
@yuf and Hercules, note that iOS 5 and 6 currently differentiate on whether the contentOffset is calculated before receiving touch. Apple decided to change this and break all custom code for who knows why. 5 doesn't, 6 does. Why do this!!? Argh. Should help other coders with no button receiving input problem). – Stephen J Dec 11 '12 at 22:12
-
In Xcode 5 IB (Interface Builder) drag on "Tap Gesture Recognizer" object to ScrollView. Then Control Drag from ScrollView to implementation code (*.m) to add a tap gesture method. – David Douglas Sep 11 '13 at 09:51
-
@yuf may you add your comment as an answer. It helped me. – Bruno Bieri Sep 25 '19 at 07:19
You can make your own UIScrollview subclass and then you can implement the following:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"DEBUG: Touches began" );
UITouch *touch = [[event allTouches] anyObject];
[super touchesBegan:touches withEvent:event];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"DEBUG: Touches cancelled");
// Will be called if something happens - like the phone rings
UITouch *touch = [[event allTouches] anyObject];
[super touchesCancelled:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"DEBUG: Touches moved" );
UITouch *touch = [[event allTouches] anyObject];
[super touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"DEBUG: Touches ending" );
//Get all the touches.
NSSet *allTouches = [event allTouches];
//Number of touches on the screen
switch ([allTouches count])
{
case 1:
{
//Get the first touch.
UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
switch([touch tapCount])
{
case 1://Single tap
break;
case 2://Double tap.
break;
}
}
break;
}
[super touchesEnded:touches withEvent:event];
}

- 22,355
- 2
- 39
- 64

- 1,095
- 16
- 15
If we're talking about the points inside the scrollview then you can hook with the delegate method:
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
and inside the method, read the property:
@property(nonatomic) CGPoint contentOffset
from the scrollView to get the coordination.

- 7,689
- 1
- 23
- 25
-
2hi, i don't want to get touches when the scrollview Dragging, i need like **TouchesBegan** – Mar 07 '11 at 06:22
This works also on touch down event.
In the currently as correct marked answer you can get a touch point
only at a "Tap" event. This event seems only to fire on "finger up" not on down.
From the comment of yuf in the same answer you can get the touch point
from the underlying view also within the UIScrollView
.
- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch*)touch
{
CGPoint touchPoint = [touch locationInView:self.view];
return TRUE; // since we're only interested in the touchPoint
}
According to Apple's documentation the gestureRecognizer
does:
Ask the delegate if a gesture recognizer should receive an object representing a touch.
which means to me that I can decide if a gestureRecognizer
should recieve a touch or not.

- 9,724
- 11
- 63
- 92