6

Possible Duplicate:
UIScrollview getting touch events

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

I mean, suppose the user uses his finger in this way: taps and scroll, lifts the finger and again, taps and scroll, etc. Is it possible to know the CGPoint where the taps happened in relation to the self.view the scroller is in? The scroller occupies the whole self.view.

Thanks.

Community
  • 1
  • 1
Duck
  • 34,902
  • 47
  • 248
  • 470

4 Answers4

12

You can do it with gesture recognizers. For detect single tap location 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 that tapPoint to self.view you can use convertPoint:toView: method in UIView class

beryllium
  • 29,669
  • 15
  • 106
  • 125
  • 1
    nope. tapAction is just triggered if I tap and don't scroll. If I do like I said, tap, scroll, lift finger, tap, scroll, lift finger, etc., no tap is ever detected. No tap is ever detected also if the scroller is also scrolling after a big swipe. The scroller has to be still and one dry tap has to be made, for this to work. – Duck Oct 27 '11 at 21:14
  • 1
    There are a lot of other gesture recognizers - UIPanGestureRecognizer , UIPinchGestureRecognizer, UISwipeGestureRecognizer, UIRotationGestureRecognizer. Moreover, you can create custom recognizer to detect very complex touches. I gave the more popular example with simple touch. – beryllium Oct 27 '11 at 21:25
  • @RubberDuck Have you figured out this? I'm on the same boat... I'm working on a UICollectionView, I want detect if finger touch is on the cell view – X.Y. Apr 06 '13 at 18:11
  • I've `UIkit.framework` under my frameworks but I cannot find out `UITapGestureRecognizer`. When I try to write `UITapGestureRecognizer` it gives me error as not found. – Adil Malik Jan 11 '14 at 16:51
  • @beryllium Why you won't just do [sender locationInView: myScrollView] ? – Paweł Brewczynski Jan 02 '15 at 17:41
0

Take a look at touchesBegan:withEvent: You will get a NSSet of UITouch's, and a UITouch contains a locationInView: method that should return the CGPoint of the touch.

kcharwood
  • 2,501
  • 19
  • 22
0

You'd probably be able to subclass it and look at touchesBegan.

http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIResponder_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40006783-CH4-SW1

logancautrell
  • 8,762
  • 3
  • 39
  • 50
  • it is not that easy. TouchesBegan just detects a touch if the scrollview is still if it is moving already and I touch it, touchesBegan will not detect anything. – Duck Oct 27 '11 at 21:22
-1

You could find the location in the view and add the scroll ofset to it. Now, your next problem is that -(void)touchesBegan:touches:event won't be called because the events will be sent to your scrollview. This can be fixed by subclassing your UIScrollView and have the scrollview send 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);
}
simonbs
  • 7,932
  • 13
  • 69
  • 115
  • 1
    nope. touchesBegan is just triggered if I tap and don't scroll. If I do like I said, tap, scroll, lift finger, tap, scroll, lift finger, etc., no tap is ever detected. No tap is ever detected also if the scroller is also scrolling after a big swipe. The scroller has to be still and one dry tap has to be made, for this to work. It is not easy as it seems. – Duck Oct 27 '11 at 21:20