我正在尝试使用滑动侧菜单实现 View ,例如 PKRevealController在 iOS 6.1 中。 A simple demo of this issue with source code on github is here ,但是如果您已经了解了 gestureRecognizer 委托(delegate)实现,则可能不需要捕获它。
我看到的问题是,我的用户想要使用的两个手势会相互混淆。应用程序中心(主屏幕)中的 UITableView 应该能够使用向右滑动手势来删除,但我仍然希望在顶部导航区域发生滑动以暴露侧边菜单。我也打算显示除表格 View 之外的其他内容,并且在运行时,每当用户在其中一个侧面菜单上选择一个按钮时,我计划将主视图换成不同的 View 。这有点像我要使用的“隐藏侧托盘 UITabBarController”,但我希望仅当主“前 View ” Controller 不是 UITableView 或其 subview 时才显示侧栏。
目前,使用PKRevealController自带的demo源码,并在主视图的UITableView中加入删除支持,已经无法通过滑动手势删除一行。 (您必须添加一种 TableView 方法才能在 UITable View 中启用删除支持,我确实添加了。)
有人问过 here ,但所陈述的答案是不完整的,如下所示,对我不起作用,我不知道为什么,因为似乎在我返回 YES 的任何时候都不会调用此委托(delegate)方法,但无论如何它都会继续进行并开始做一个手势。
更新 与我放在下面的 WIKI/FAQ 答案相比,上一个问题的答案也是错误的。
我只通过添加以下内容修改了 PKRevealController.m 类:
- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
UIView *view1 = otherGestureRecognizer.view;
UIView *view2;
if (view1) {
view2 = view1.superview;
};
if ([gestureRecognizer.view isKindOfClass:[UITableView class]])
{
return NO;
}
// Co-operate by not stealing gestures from UITableView.
if ([view1 isKindOfClass:[UITableView class]]) {
return NO;
}else if ([view1 isKindOfClass:[UITableViewCell class]]) {
return NO;
// UITableViewCellContentView
}
else if (view2 && [view2 isKindOfClass:[UITableViewCell class]]) {
return NO;
// UITableViewCellContentView
}
else
{
return YES; // NEVER GETS HIT. BREAKPOINT HERE!
}
}
让我感到困惑的是,上面的返回 YES 代码在任何时候都没有被命中(我有一个断点),然而,手势 Controller 仍在窃取手势。
注意:我做了一个邪恶的黑客攻击,但我认为我可以干净地阻止这种情况。这是我的邪恶黑客:
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer == self.revealPanGestureRecognizer)
{
CGPoint translation = [self.revealPanGestureRecognizer translationInView:self.frontViewContainer];
BOOL begin = (fabs(translation.x) >= fabs(translation.y));
// BEGIN EVIL HACK
if (_topLimitY > 0) {
CGPoint location = [gestureRecognizer locationInView:gestureRecognizer.view];
if (location.y>_topLimitY) // _topLimitY = 55 for instance.
begin = NO;
}
// END EVIL HACK.
return begin;
}
else if (gestureRecognizer == self.revealResetTapGestureRecognizer)
{
return ([self isLeftViewVisible] || [self isRightViewVisible]);
}
return YES;
}
现在,在我的恶意黑客演示中,我将 topLimitY 属性(我添加到 PKRevealController 的属性中)设置为 55,这允许我在前 View 的导航栏区域上滑动,但不能在占据演示的其余部分。
请注意,我计划有多个主视图,并且如果 View 是 UITableView 或其某些 subview ,则只想在整个主区域上击败手势识别。这就是为什么我将我的 hack 称为 hack 之上的原因。因为我认为你可以告诉手势识别器离开而不打扰你,但它不起作用,它甚至没有调用 shouldRecognize 方法,它只是继续执行其列表中的下一个事情做。
最佳答案
我真的应该先阅读WIKI,不是吗?
这是一个常见问题解答,它说得对here :
实例化 Controller 时,在您的选项字典中传递此选项:
NSDictionary *options = @{
PKRevealControllerRecognizesPanningOnFrontViewKey : @NO
};
这将禁用整个前 View 的基于平移的显示。现在,您可以使用revealPanGestureRecognizer 并将其添加到您希望在不干扰表格 View 的情况下平移的任何 View 中,以启用基于手势的显示。
我建议(如果使用带有可滑动单元格的基于表格的环境)您,将revealPanGestureRecognizer 添加到您的前 View Controller 的导航栏(它很可能有):
[self.navigationController.navigationBar addGestureRecognizer:self.revealController.revealPanGestureRecognizer];
瞧——平移不再干扰您的表格 View 。
更多信息:
https://github.com/pkluz/PKRevealController/issues/76
谢谢维基。要是我先读完就好了。
以上完全回答了我的问题,并且已经在 wiki 上。我正在回答我自己的问题,因为 Google 似乎总是首先使用 Stackoverflow,这可能会在 future 帮助其他困惑的开发人员。
更新 如果上述事情在您尝试时爆炸,则可能为时过早。这是上述修复的一个更强大的版本:
// Additional gesture recognition linkups. The underscore variables here
// are implementation-section ivars in my app-delegate, that I have already
// checked are valid and initialized, and this is the last thing in my app delegate
// didFinishLaunch... method, before the return YES:
UIGestureRecognizer *rec = _revealController.revealPanGestureRecognizer;
if (rec) {
[_frontViewNavController.navigationBar addGestureRecognizer:rec];
}
关于ios - TableView 和 PKRevealController 手势冲突 - 如何真正解决它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15820684/