1

I have subclassed UIScrollView and implemented -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event method in it.

But Nothing happens when click or hold the scroll view!

Edit: I also need to use touchesEnded method

Burhanuddin Sunelwala
  • 5,318
  • 3
  • 25
  • 51

4 Answers4

2

I think UIScrollView has two gesture recognizer. They are responsible for handling touch sequences, so they swallow the touch events.

Use scrollView delegate methods to handle drag gestures or the

touchesShouldBegin:withEvent:inContentView:

method to handle scrollview content touches and

touchesShouldCancelInContentView:

to cancel it.

As alternative you can manipulate the panGesture recognizer of the scrollView to pass the event to the next responser.

Peteee24
  • 510
  • 4
  • 8
  • That' what I was looking for! Does UIScrollView have a method which gets called when touches ends? – Burhanuddin Sunelwala May 03 '13 at 07:11
  • Yes, but as i told the event call gets intercepted by the gestures. Use touchesShouldBegin:withEvent:inContentView: to return YES for your special views, so they will get the touchedBegan/ended events – Peteee24 May 04 '13 at 14:08
1

In your subclass of UIScrollView override the hitTest method like this:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

  UIView *result = nil;
  for (UIView *child in self.subviews)
    if ([child pointInside:point withEvent:event])
      if ((result = [child hitTest:point withEvent:event]) != nil)
        break;

  return result;
}
Thilina Chamath Hewagama
  • 9,039
  • 3
  • 32
  • 45
  • Ya i had used that before. Anyways, could you explain the code? – Burhanuddin Sunelwala May 03 '13 at 06:47
  • [hitTest:withEvent:](http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIView_Class/UIView/UIView.html) "Returns the farthest descendant of the receiver in the view hierarchy (including itself) that contains a specified point." – Kasper Munck May 03 '13 at 07:10
1

I think you are using scrollview as a subview.So in that case you can use gesture coz same problem i've face it.

You can do using UITapGestureRecognizer like this ...

-(void)viewDidLoad
{
   UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
   gr.delegate = self;
    [self.scrollView addGestureRecognizer:gr];
}
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer
{
    // do here whatever you wanna ..........
}
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
0

Solved!

Used LongPressedGestureRecognizer. The action method keeps getting called till the user holds the view. From that i can figure out the state of the gestureRecognizer (UIGestureRecognizerStateBegan, ...Ended, ...Cancelled)

Burhanuddin Sunelwala
  • 5,318
  • 3
  • 25
  • 51