2

I need to get the touch position from a UIScrollView. But when I create a subclass for my scrollview :

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  NSArray *touchesArray = [touches allObjects];
  UITouch *touch = (UITouch *)[touchesArray objectAtIndex:0];
  CGPoint point = [touch locationInView:self];
  self.position = point;}

The function touchesBegan isn't always called. If I swipe fast, touchesBegan is never called, but scrollViewDidScroll gets directly called instead.

I can't use UIGesture (UITapGestureRecognizer, ... ) because users don't tap or swipe.

Are there others methods to get the position from UIScrollView ?

EDIT :

Thanks ThXou : https://stackoverflow.com/a/15763450/1752843

UIScrollView subclass :

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event{
    self.position = point;
    return self;
}
Community
  • 1
  • 1
VivienCormier
  • 1,133
  • 1
  • 11
  • 16

4 Answers4

7

To detect the touch location inside scrollview, try this code

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

- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
 { 
   CGPoint touchPoint=[gesture locationInView:scrollView];
 }
iVenky
  • 441
  • 1
  • 4
  • 14
  • But the function "touchesBegan" or "touchesMoved" are never called when my scrollview is scrolled. And I want to get the touch when my scrollview is used. – VivienCormier Apr 02 '13 at 11:21
  • Even though "touchesBegan" is empty, her are never called. – VivienCormier Apr 02 '13 at 11:27
  • Sorry try the above code. other method is yu need to call the sub classes . For further details, check out this link http://iosdevelopertips.com/user-interface/detect-single-tap-in-uiscrollview.html – iVenky Apr 02 '13 at 11:32
  • To detect touchesBegan and touchesMoved, sub-classing is NOT required. You can add the UIScrollViewDelegate to the "h" file and applicable functions to detect touches based on user intention. See http://stackoverflow.com/questions/993280/how-to-detect-when-a-uiscrollview-has-finished-scrolling/20017639#20017639 – bob Nov 21 '13 at 09:22
3

You could alternatively use scrollview.panGestureRecognizer's locationInView method to get the coordinates of a touch in your view. This could be in any of scrollView's delegate method depending on your requirements.

rounak
  • 9,217
  • 3
  • 42
  • 59
2

I saw an answer in SO that I think that it can solve you problem. It seems that the SDK doesn't pass the touch handling methods to the UIScrollView subclasses anymore. So, you should to perform a hitTest: to pass the touches to your subclass.

See the answer for more information.

Community
  • 1
  • 1
thxou
  • 691
  • 7
  • 20
1
scrollView.delaysContentTouches = NO;

It will makes that touchesBegan and other touch events will be triggered before scrollViewDidScroll.

The hitTest is a hack and should not be preferred solution.