Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIScrollView touchesBegan

So all I wanna do is play a sound when the user touches a UIScrollView. The UIScrollViewDelegate has that scrollViewWillBeginDragging: method but it only gets called on touchMoved. I want it to get called on touchBegan.

Tried touchesBegan:withEvent: but it doesn't receive any touch. Anyone has a clue?

like image 459
samvermette Avatar asked Nov 06 '09 07:11

samvermette


3 Answers

Use a Tap Gesture Recognizer instead:

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touch)];
[recognizer setNumberOfTapsRequired:1];
[recognizer setNumberOfTouchesRequired:1];
[scrollView addGestureRecognizer:recognizer];  

Or

make subClass of your UIScrollView and implement all

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

// If not dragging, send event to next responder
  if (!self.dragging){ 
    [self.nextResponder touchesBegan: touches withEvent:event]; 
  }
  else{
    [super touchesEnded: touches withEvent: event];
  }
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

// If not dragging, send event to next responder
    if (!self.dragging){ 
     [self.nextResponder touchesBegan: touches withEvent:event]; 
   }
   else{
     [super touchesEnded: touches withEvent: event];
   }
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

  // If not dragging, send event to next responder
   if (!self.dragging){ 
     [self.nextResponder touchesBegan: touches withEvent:event]; 
   }
   else{
     [super touchesEnded: touches withEvent: event];
   }
}
like image 53
Rajneesh071 Avatar answered Oct 22 '22 13:10

Rajneesh071


Use a Tap Gesture Recognizer instead:

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touch)];
[recognizer setNumberOfTapsRequired:1];
[recognizer setNumberOfTouchesRequired:1];
[scrollView addGestureRecognizer:recognizer];
like image 36
Roosevelt Avatar answered Oct 22 '22 15:10

Roosevelt


I think you will have to subclass UIScrollView to be able to do this.

touchesBegan:withEvent: is only sent to subclasses of UIView. You are problably implementing touchesBegan:withEvent: in your controller, right? If so, it won't work from there...

Alternatively, if you put a subclass of UIView (that you wrote) in your UIScrollView, you can also catch the touchesBegan event from there (but only when the user touches that particular subview). UIScrollView passes on touches to its subviews by default (see touchesShouldBegin:withEvent:inContentView: in UIScrollView).

like image 6
Zoran Simic Avatar answered Oct 22 '22 14:10

Zoran Simic