IPhone - UIScrollView ... touch coordinate detection

Possible duplicate:
UIScrollview receives touch events

Is it possible to detect where a finger touched in UIScrollView?

I mean, suppose the user uses his finger in this way: taps and scrolls, raises his finger and again, taps and scroll, etc. Is it possible to find out CGPoint where the taps occurred relative to the self.view scroller in? Scroller takes all self.view.

Thanks.

+6
source share
4 answers

You can do this with gesture recognizers. To locate a location with a single tap, use UITapGestureRecognizer

 UITapGestureRecognizer *tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)] autorelease]; [myScrollView addGestureRecognizer:tapRecognizer]; - (void)tapAction:(UITapGestureRecognizer*)sender{ CGPoint tapPoint = [sender locationInView:myScrollView]; CGPoint tapPointInView = [myScrollView convertPoint:tapPoint toView:self.view]; } 

To convert this tapPoint to self.view, you can use the convertPoint:toView: method in the UIView class

+12
source

Take a look at touchhesBegan: withEvent: you get an NSSet UITouch , and UITouch has a locationInView: method, which should return CGPoint to the touch.

0
source

You can find the location in the view and add a scroll to it. Now your next problem is that -(void)touchesBegan:touches:event will not be called because events will be sent to your scrollview. This can be eliminated by subclassing your UIScrollView and scrolling through the touch events to the next responder (your view).

 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // Position of touch in view UITouch *touch = [[event allTouches] anyObject]; CGPoint touchPoint = [touch locationInView:self.view]; // Scroll view offset CGPoint offset = scrollView.contentOffset; // Result CGPoint scrollViewPoint = CGPointMake(touchPoint.x, touchPoint.y + offset.y); NSLog(@"Touch position in scroll view: %f %f", scrollViewPoint.x, scrollViewPoint.y); } 
0
source

Source: https://habr.com/ru/post/900240/


All Articles