UIControl子类-如何停止触摸事件流



我正在写一个棋盘游戏应用程序(像国际象棋)。主视图识别在其全屏视图中的任何位置开始的滑动手势(UISwipeGestureRecognizer),这会使板旋转。

现在,我添加了一个正方形的透明子视图正好在板上。这是一个UIControl子类,用于检测触摸——作为棋盘上棋子的移动:

[self.view addSubview:self.boardControl]

我预计在UIControl子类覆盖的屏幕区域会阻止滑动手势。但事实并非如此。因此,当我在我的方形木板Control上快速触摸并拖动(滑动)时,它首先被检测为典当移动,但随后再次被检测为滑动,从而旋转木板。

如何使UIControl子类阻止从其框架开始的触摸事件流到其超级视图?


我可以通过以下方式防止我的应用程序对滑动手势做出反应:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
CGPoint location = [gestureRecognizer locationInView:self.view];
CGRect frame = self.boardControl.frame;
if ( CGRectContainsPoint(frame, location) )
return NO;
return YES;
}

或通过:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
shouldReceiveTouch:(UITouch *)touch
{
CGPoint location = [touch locationInView:self.view];
CGRect frame = self.boardControl.frame;
if (CGRectContainsPoint(frame, location))
return NO;
return YES;
}

但我想提前一级解决这个问题:我希望boardControl不要错过任何在视图层次结构中更高的帧内启动的触摸事件。

UIControl子类是否可以"覆盖"它的超级视图,并"吃掉"它得到的所有触摸,这样超级视图就不需要访问它的框架来猜测这种触摸是否必须被过滤掉?

您所需要的只是实现UIGestureRecognizerDelegate,它提供了制作所需内容的方法。

我认为你应该从gestureRecognizer:shouldReceiveTouch:例子开始

最新更新