UIScrollview receives touch events

How can I detect touch points in my UIScrollView ? Touching delegation methods does not work.

+70
ios objective-c cocoa-touch uiscrollview uitouch
Mar 07 2018-11-11T00:
source share
3 answers

Set up gesture recognizer:

 UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)]; [scrollView addGestureRecognizer:singleTap]; 

and you will get the following touches:

 - (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture { CGPoint touchPoint=[gesture locationInView:scrollView]; } 
+180
Mar 07 '11 at 6:22
source share

You can create your own subclass of UIScrollview, 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]; } 
+6
May 15 '12 at 10:33
source share

If we are talking about the points inside scrollview, you can connect to the delegate method:

 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView 

and inside the method, read the property:

 @property(nonatomic) CGPoint contentOffset 

from scrollView to get coordination.

+1
Mar 07 '11 at 6:12
source share



All Articles