Ios “如何检测”;“着陆”;在UIScrollView的超级视图中?
我有一个Ios “如何检测”;“着陆”;在UIScrollView的超级视图中?,ios,objective-c,uiview,uiscrollview,touch-event,Ios,Objective C,Uiview,Uiscrollview,Touch Event,我有一个UIView包含UIScrollView,我希望能够在用户点击UIScrollView时捕获UIView中的“触地”事件 我已经尝试在我的UIViewController中包含所有touchsbegind/Ended/Cancelled处理程序,但是当点击主UIView中包含的uicrollview时,没有一个处理程序被触发 实现这一点的最佳方法是什么?在UIView中,实施ToucheSStart: - (void)touchesBegan:(NSSet *)touches withE
UIView
包含UIScrollView
,我希望能够在用户点击UIScrollView
时捕获UIView
中的“触地”事件
我已经尝试在我的UIViewController
中包含所有touchsbegind/Ended/Cancelled处理程序,但是当点击主UIView
中包含的uicrollview
时,没有一个处理程序被触发
实现这一点的最佳方法是什么?在UIView中,实施ToucheSStart:
- (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
}
}
旁注:请确保在UIView中将userInteractionEnabled设置为YES。您需要禁用用户与滚动视图的交互
scrollView.userInteractionEnabled = NO;
禁用后,UIScrollView的superview将获得TouchesBegind事件。您还可以在UIView子类中实现
hitTest:withEvent:
。调用此方法以确定哪个子视图应接收触摸事件。因此,在这里,您可以只跟踪通过视图的所有事件,而不是从子视图中隐藏某些事件。在这种情况下,您可能不需要为scrollview禁用用户交互
有关此方法的更多详细信息,请参见
UIView
类参考。您还可以将手势识别器添加到superview中。
例如,如果您需要激活/停用覆盖滚动视图的按钮,请点击手势:
self.tap = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap:)] autorelease];
tap.numberOfTapsRequired = 1;
["containerView" addGestureRecognizer:tap];
手势确实保留了滚动视图的交互,并且根据我下面的自我回答,使滚动视图的用户userInteractionEnabled=NO。这违背了uiscrollview的目的,因为我们必须在“这里做东西”下执行滚动操作。TapGestureRecognitor只检测点击(触地+触地)。这就是我们现在想要的接地事件检测。这首先破坏了滚动视图的目的。