UIScrollView touchesBegan

2019-01-04 13:46发布

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?

8条回答
forever°为你锁心
2楼-- · 2019-01-04 14:07

You need to subclass scroll view and then implement the touchesBegan method.You need also set the delayTouchDown property to false.

查看更多
在下西门庆
3楼-- · 2019-01-04 14:11

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];
   }
}
查看更多
我命由我不由天
4楼-- · 2019-01-04 14:12

New updates on the issue here (including a link to ZoomScrollView source code + some excellent explanation on UIScrollView internals) . Also, check out Apple's updated ScrollViewSuite example.

查看更多
Anthone
5楼-- · 2019-01-04 14:12

Rajneesh071 answer in swift 4 Change the custom class of scrollView in storyboard to CustomScrollView

import Foundation

class CustomScrollView: UIScrollView {
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if !isDragging {
            next?.touchesBegan(touches, with: event)
        } else {
        super.touchesBegan(touches, with: event)
        }
    }
}
查看更多
倾城 Initia
6楼-- · 2019-01-04 14:21

You can also respond to these events in your controller. For that to work, you have to define this feature (in your controller):

- (BOOL)canBecomeFirstResponder {
    return YES;
}

Then, in 'viewDidAppear', you need to call 'becomeFirstResponder':

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self becomeFirstResponder];
查看更多
我欲成王,谁敢阻挡
7楼-- · 2019-01-04 14:29

Use a Tap Gesture Recognizer instead:

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touch)];
[recognizer setNumberOfTapsRequired:1];
[recognizer setNumberOfTouchesRequired:1];
[scrollView addGestureRecognizer:recognizer];
查看更多
登录 后发表回答