UIScrollview getting touch events

Objective CIosCocoa TouchUiscrollviewUitouch

Objective C Problem Overview


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

Objective C Solutions


Solution 1 - Objective C

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];
}

Solution 2 - Objective C

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];
}

Solution 3 - Objective C

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.

Solution 4 - Objective C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser559005View Question on Stackoverflow
Solution 1 - Objective CSuresh DView Answer on Stackoverflow
Solution 2 - Objective CVickyView Answer on Stackoverflow
Solution 3 - Objective CNevinView Answer on Stackoverflow
Solution 4 - Objective CBruno BieriView Answer on Stackoverflow