如何在UIScrollView中的上海华检测“触落”?
-
25-09-2019 - |
题
我有一个包含UIView
一个UIScrollView
,我希望能够随时捕捉“触摸下”事件在UIView
上UIScrollView
用户的水龙头。
我试过,包括所有的touchesBegan /端/取消我的UIViewController
但他们没有处理敲击时里面的UIScrollView
包含在主UIView
会被解雇。
什么是做到这一点?
最好的办法解决方案
在的UIView,实施的touchesBegan:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// assign a UITouch object to the current touch
UITouch *touch = [[event allTouches] anyObject];
// if the view in which the touch is found is myScrollView
// (assuming myScrollView is the UIScrollView and is a subview of the UIView)
if ([touch view] == myScrollView) {
// do stuff here
}
}
一个侧面说明:确保userInteractionEnabled被设置为YES的UIView的
。其他提示
您也可以在你的UIView子类实现hitTest:withEvent:
。此方法被调用,以确定哪些子视图应该接收触摸事件。所以在这里,你可以只跟踪通过您的视图中的所有事件没有或隐藏某些从子视图的事件。在这种情况下,你可能不需要禁用你的滚动型用户交互。
请参阅上在UIView
类引用此方法的更多细节。
您还可以添加一个手势识别到你的上海华。 例如敲击手势如果需要激活/停用之类的按钮重叠滚动视图:
self.tap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)] autorelease];
tap.numberOfTapsRequired = 1;
["containerView" addGestureRecognizer:tap];
的动作做保持滚动视图相互作用
您需要禁用用户交互与所述滚动视图这样...
scrollView.userInteractionEnabled = NO;
一旦禁用,UIScrollView的上海华得到的touchesBegan事件。
不隶属于 StackOverflow