ios - UIScrollView 和 touchesBegan/touchesEnded 等

标签 ios objective-c uiview uiscrollview

我有一个带有 2 个 subview 的 UIView,我们称它为 View A:

  • 一个基本的 UIView,称为 B
  • UIScrollView,调用C

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

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

当我触摸 View B 时,touchesBegan:withEvent:touchesEnded:withEvent: 被调用,但是如果我触摸 UIScrollView( View C),这里没有调用。

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

最佳答案

使用 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   
}

快速版本:

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 ")

}

关于ios - UIScrollView 和 touchesBegan/touchesEnded 等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28873530/

相关文章:

ios - 蓝色横幅 '%MyApp% is Using Your Location' 适用于仅在事件时使用定位服务的应用

ios - 从 Objective C 类的按钮操作加载基于 Swift 的 ViewController

ios - 在以编程方式创建的 UIView 层次结构之上添加 subview

ios - 子类 UIView 错误

ios - 如何在导航 Controller 导航栏顶部添加图像?

ios - 使用 swift 访问 Parse.com 对象中的指针值

ios - UISearchBar、UITableViewController 和 UINavigationController,在执行 native 代码时收到 SIGSEGV

ios - 有没有办法在进入前台时强制全局刷新 View ?

iOS :Can I use custom URL scheme to communicate a flag between three applications should be always in background

ios - 如何比较数组中的三个对象?