UIScrollView touchesBegan

时间:2009-11-06 07:21:09

标签: iphone uiscrollview

所以我想做的就是当用户触摸UIScrollView时发出声音。 UIScrollViewDelegate具有scrollViewWillBeginDragging:方法,但它只在touchMoved上调用。我想让它在touchBegan上调用。

尝试touchesBegan:withEvent:但它没有任何接触。 任何人都有线索?

8 个答案:

答案 0 :(得分:18)

改为使用Tap Gesture识别器:

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

制作UIScrollView的子类并实施所有

    -(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];
   }
}

答案 1 :(得分:13)

改为使用Tap Gesture识别器:

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

答案 2 :(得分:6)

我认为你必须将UIScrollView子类化才能做到这一点。

touchesBegan:withEvent:仅发送到UIView的子类。您可以在控制器中实现touchesBegan:withEvent:对吧?如果是这样,它就不会起作用......

或者,如果您在UIScrollView中放置了UIView的子类(您编写的),您也可以从那里捕获touchesBegan事件(但仅当用户触摸该特定子视图时)。 默认情况下,UIScrollView会将其触摸传递给其子视图(请参阅touchesShouldBegin:withEvent:inContentView:在UIScrollView中)。

答案 3 :(得分:5)

您还可以在控制器中响应这些事件。要实现这一点,您必须定义此功能(在您的控制器中):

- (BOOL)canBecomeFirstResponder {
    return YES;
}

然后,在'viewDidAppear'中,你需要调用'becomeFirstResponder':

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self becomeFirstResponder];

答案 4 :(得分:1)

问题here的新更新(包括ZoomScrollView源代码的链接+ UIScrollView内部的一些优秀解释)。另外,请查看Apple更新的ScrollViewSuite示例。

答案 5 :(得分:0)

您应该在UIS​​crollView上添加touchesBegan:withEvent :,而不是在UIScrollViewDelegate上。 UIScrollView是UIResponder的子类,它具有触摸事件。

答案 6 :(得分:0)

Rajneesh071在swift 4中回答 将故事板中scrollView的自定义类更改为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)
        }
    }
}

答案 7 :(得分:0)

您需要子类化滚动视图,然后实现touchesBegan方法。您还需要将delayTouchDown属性设置为false。