如何在不中断下方视图的手势识别的情况下识别弹出视图中的手势?
How to recognise gestures on pop-up view without interrupting gesture recognition on the view underneath?
我有一个带有自定义手势识别器的自定义半透明视图,可以识别单指手势。它会弹出全屏视图。用户使用捏合和旋转手势与全屏视图交互。
所以我想要实现的是防止弹出视图干扰用户继续捏合和旋转全屏视图的能力,即使在弹出视图的范围内也是如此。弹出视图只需要响应我自定义的单指手势识别器。
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
if (event.allTouches.count > 1) {
return NO;
}
...
}
我认为上面的代码可能已经破解了它,但遗憾的是调用 pointInside
时没有填充触摸。还有其他建议吗?
我认为您将需要 UIGestureRecognizerDelegate 协议。特别是那些方法:
- gestureRecognizer:shouldReceiveTouch:
- gestureRecognizer:shouldRequireFailureOfGestureRecognizer:
- gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer:
您可以使用以下方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
我已经使用了 touchesBegan,在那里我没有接触,并且根据条件你可以做任何你想做的事情。
下面是示例代码
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//Varible used for counting touches
int nFingers += [touches count];
//finger points
for (UITouch *touch in touches) {
if (nFingers < 1){
CGPoint location = [touch locationInView:self];
}
}
}
经过两天的工作,我找到了一个非常简单的答案。我希望它能帮助别人。在弹出视图的初始化中,只需将手势识别器添加到超级视图或任何全屏视图,而不是弹出窗口自己的视图。目标仍然可以是弹出视图:
self.recognizer = [[MyGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[fullscreenView addGestureRecognizer:self.recognizer];
然后禁用用户交互:
self.userInteractionEnabled = NO;
这不会影响弹出视图上的手势,与弹出视图下方的视图关联的手势现在也可以正常工作。如果他们相互竞争,那么 lorenzoliveto 的答案中的覆盖可以对其进行排序。
我有一个带有自定义手势识别器的自定义半透明视图,可以识别单指手势。它会弹出全屏视图。用户使用捏合和旋转手势与全屏视图交互。
所以我想要实现的是防止弹出视图干扰用户继续捏合和旋转全屏视图的能力,即使在弹出视图的范围内也是如此。弹出视图只需要响应我自定义的单指手势识别器。
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
if (event.allTouches.count > 1) {
return NO;
}
...
}
我认为上面的代码可能已经破解了它,但遗憾的是调用 pointInside
时没有填充触摸。还有其他建议吗?
我认为您将需要 UIGestureRecognizerDelegate 协议。特别是那些方法:
- gestureRecognizer:shouldReceiveTouch:
- gestureRecognizer:shouldRequireFailureOfGestureRecognizer:
- gestureRecognizer:shouldBeRequiredToFailByGestureRecognizer:
您可以使用以下方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;
我已经使用了 touchesBegan,在那里我没有接触,并且根据条件你可以做任何你想做的事情。
下面是示例代码
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//Varible used for counting touches
int nFingers += [touches count];
//finger points
for (UITouch *touch in touches) {
if (nFingers < 1){
CGPoint location = [touch locationInView:self];
}
}
}
经过两天的工作,我找到了一个非常简单的答案。我希望它能帮助别人。在弹出视图的初始化中,只需将手势识别器添加到超级视图或任何全屏视图,而不是弹出窗口自己的视图。目标仍然可以是弹出视图:
self.recognizer = [[MyGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[fullscreenView addGestureRecognizer:self.recognizer];
然后禁用用户交互:
self.userInteractionEnabled = NO;
这不会影响弹出视图上的手势,与弹出视图下方的视图关联的手势现在也可以正常工作。如果他们相互竞争,那么 lorenzoliveto 的答案中的覆盖可以对其进行排序。