我有UIView
个子视图,我们称之为A
:
UIView
,名为B
UIScrollView
,名为C
我在视图A
中覆盖了以下方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
当我触摸视图B
时,touchesBegan:withEvent:
和touchesEnded:withEvent:
会被调用,但如果我触摸UIScrollView
(查看C
),则不会调用
我知道这是预期的行为,但我想了解原因。我找不到任何明确的答案。有什么想法吗?
答案 0 :(得分:6)
使用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 ")
}
答案 1 :(得分:6)
如果你有滚动视图thouchesBegan方法将无法工作,因为如果想要滚动你必须在storyboard中启用userInteractionEnabled为YES。如果你把它变为NO,它就会触发。所以解决方案是使用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)
}
这很好。
答案 2 :(得分:5)
您至少有两个选项:
1)“通过为您的滚动视图设置userInteractionEnabled
到NO
”。
更多信息https://stackoverflow.com/a/25108078/1702413
2)将您的UIScrollView子类化 - 例如使用此https://dl.dropboxusercontent.com/u/19438780/ClassScrollViewDelegate.zip
<强>更新强>
为什么呢?请再次阅读文档:
“因为滚动..它必须知道触摸是否表示意图 滚动...它暂时拦截了一个降落事件...... 滚动视图取消子视图中的任何跟踪并执行 滚动自己“
最重要的是: **
子类可以覆盖 touchesShouldBegin:withEvent:inContentView:,pagingEnabled,和 touchesShouldCancelInContentView:方法
答案 3 :(得分:1)
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)
}
答案 4 :(得分:0)
请选中此项以使用滚动视图的平移手势识别触摸事件(开始,移动,结束)
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