UIScrollView 和 touchesBegan / touchesEnded 等

UIScrollView and touchesBegan / touchesEnded etc

我有一个带有 2 个子视图的 UIView,我们称它为视图 A:

我在视图 A 中覆盖了以下方法:

当我触摸视图 B 时,touchesBegan:withEvent:touchesEnded:withEvent: 被调用,但是如果我触摸 UIScrollView(视图 C),则有未调用。

我知道这是预期的行为,但我正在尝试了解原因。我找不到任何明确的答案。有什么想法吗?

您至少有 2 个选项:

1) "By setting userInteractionEnabled to NO for your scroll view"。 更多信息在这里

2) 将您的 UIScrollView 子类化 - 例如使用此 https://dl.dropboxusercontent.com/u/19438780/ClassScrollViewDelegate.zip

更新

为什么?请再次阅读文档:

"Because a scroll .. it must know whether a touch signals an intent to scroll ... it temporarily intercepts a touch-down event ... .. the scroll view cancels any tracking in the subview and performs the scrolling itself"

最重要的是:**

Subclasses can override the touchesShouldBegin:withEvent:inContentView:, pagingEnabled, and touchesShouldCancelInContentView: methods

来自这里:https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScrollView_Class/index.html#//apple_ref/doc/uid/TP40006922

使用 TapGestureRecognizer 代替 touchBegan 方法

UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
singleTapGestureRecognizer.numberOfTapsRequired = 1;
singleTapGestureRecognizer.enabled = YES;
singleTapGestureRecognizer.cancelsTouchesInView = NO;
[scrollView addGestureRecognizer:singleTapGestureRecognizer];

- (void)singleTap:(UITapGestureRecognizer *)gesture {  
//handle taps   
}

Swift版本:

let singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: "singleTap:")
    singleTapGestureRecognizer.numberOfTapsRequired = 1
    singleTapGestureRecognizer.enabled = true
    singleTapGestureRecognizer.cancelsTouchesInView = false
    scrollViewH.addGestureRecognizer(singleTapGestureRecognizer)

func singleTap(sender: UITapGestureRecognizer) {
    println("handle taps ")

}

如果您有滚动视图,thouchesBegan 方法将不起作用,因为如果想要滚动,您必须在情节提要中将 Enable userInteractionEnabled 设置为 YES。如果您将其设置为“否”,它将触发。所以解决方案是使用 TapGestureRecognizer 将此添加到您的 viewDidLoad:

let singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: "MyTapMethod:")
    singleTapGestureRecognizer.numberOfTapsRequired = 1
    singleTapGestureRecognizer.enabled = true
    singleTapGestureRecognizer.cancelsTouchesInView = false
    myscrollView.addGestureRecognizer(singleTapGestureRecognizer)

然后在你的控制器中添加这个 MyTapMethod。

func MyTapMethod(sender: UITapGestureRecognizer) {
        //NSLog("touchesBegan")
        self.view.endEditing(true)
    }

这工作正常。

Swift 3/4 版本:

let singleTapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MyViewController.tapRecognized))
singleTapGestureRecognizer.numberOfTapsRequired = 1
singleTapGestureRecognizer.isEnabled = true
singleTapGestureRecognizer.cancelsTouchesInView = false
scrollView.addGestureRecognizer(singleTapGestureRecognizer)

然后:

@objc func tapRecognized() {
    view.endEditing(true)
}

请选中此项以使用滚动视图的平移手势识别触摸事件(开始、移动、结束),

Class CustomScrollView:UIScrollView
{

CustomScrollView()
{

 (this.GestureRecognizers[1] as UIPanGestureRecognizer).AddTarget(() => this.PanHandle(this.GestureRecognizers[1] as UIPanGestureRecognizer));
    

}


 private void PanHandle(UIPanGestureRecognizer gestureRecognizer)
          
  {
  var touchLocation=gestureRecognizer.LocationInView(this)
    
       

  if (gestureRecognizer.State == UIGestureRecognizerState.Began)
            
    {
             
      //Touches began
             
   }
     
else if (gestureRecognizer.State == UIGestureRecognizerState.Changed)
            
   {
           
        //Touches Moved
             
   }
               
 else if (gestureRecognizer.State == UIGestureRecognizerState.Ended)
         
       {
             
       //Touches ended
              
  }
           
 }

    }

Regards,
Ashwin