检测并记录所有手势

时间:2015-01-05 15:57:42

标签: ios logging uigesturerecognizer

是否有方法来检测和记录项目和/或所有潜在的触摸响应者收到的所有手势消息?

我正在编写一个新的iOS 8主/细节通用项目,该项目在默认滑动返回行为和基于手指的绘制之间存在冲突。然而,当我尝试通过例如gestureRecognizerShouldBegin在详细控制器上记录手势行为时,代码永远不会被调用。

1 个答案:

答案 0 :(得分:1)

我通过遍历我的viewControllers.views列表来识别子视图,然后记录每个手势识别器 - 或者我感兴趣的任何其他东西 - 与每个视图相关联,从而解决了这个问题。 要迭代视图,如果我将视图子类化,我会使用建议的here递归方法。因为我没有,我稍微修改了代码here

然后,我编写了实用程序函数以供将来使用:

+ (NSMutableArray *)getAllSubviews:(UINavigationController *)navigationController {

    NSMutableArray *allSubviews     = [[NSMutableArray alloc] initWithObjects: nil];
    NSMutableArray *currentSubviews = [[NSMutableArray alloc] initWithObjects: nil];
    NSMutableArray *foundSubviews     = [[NSMutableArray alloc] initWithObjects: nil];

    NSLog(@"\n%lu total controllers:\n%@",navigationController.viewControllers.count, navigationController.viewControllers);

    for (UIViewController *vc in navigationController.viewControllers) {
        [currentSubviews addObject: vc.view];
        [foundSubviews addObject: vc.view];

        while (foundSubviews.count) {

            [foundSubviews removeAllObjects];

            for (UIView *view in currentSubviews) {

                for (UIView *subview in view.subviews)
                    [foundSubviews addObject:subview];
            }

            [currentSubviews removeAllObjects];
            [currentSubviews addObjectsFromArray:foundSubviews];
            [allSubviews addObjectsFromArray:foundSubviews];

        }
    }
    NSLog(@"\n%lu total subviews:\n%@",allSubviews.count, allSubviews);

    return allSubviews;
}
+ (void)logAllGestureRecognizers:(UINavigationController *)navigationController {

    NSMutableArray *allViews = [self getAllSubviews: navigationController];

    for (UIView *v in allViews) {
        for (UIGestureRecognizer *gestureRecognizer in v.gestureRecognizers) {
            NSLog(@"Gesture Recognizer  %@", gestureRecognizer);
        }
    }
}

使用[YourUtilityObject logAllGestureRecognizers:self.navigationController];

调用这些函数

一个警告:此实现假定self.navigationController知道所有活动视图控制器。如果要为当前的viewController创建新的navigationController,则结果将仅显示与该navigationController关联的视图,您必须手动调用每个导航控制器的函数。