ECSlidingViewController 传递 UILongPressGestureRecognizer

ECSlidingViewController passing UILongPressGestureRecognizer

我四处寻找解决方案,但其中 none 回答了我的问题。因此,我从我的项目中退了一步,试图解决这个问题。我正在使用 AppDelegate 来帮助我更清楚地查看代码。

我想让菜单在用户触摸并按住屏幕时滑出。它应该在用户松开触摸时滑回。

但是,我无法发送手势参数。

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main"
                                                         bundle: nil];
    ScrollViewController *topViewController = [mainStoryboard instantiateViewControllerWithIdentifier:@"Scroll"];
    MenuViewController *underLeftViewController = (MenuViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:@"Menu"];

    ECSlidingViewController *slidingViewController = [[ECSlidingViewController alloc] initWithTopViewController:topViewController];
    slidingViewController.underLeftViewController = underLeftViewController;

    [slidingViewController.view addGestureRecognizer:slidingViewController.panGesture];
    self.window.rootViewController = slidingViewController;

    // HOW DO I PASS GESTURE DATA?

    UILongPressGestureRecognizer *revealMenuRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:slidingViewController.underLeftViewController action:@selector(revealMenu:  GESTURE PARAMETERS?  )];

    [topViewController.view addGestureRecognizer:revealMenuRecognizer];
    return YES;
}

MenuViewController.m

- (void)revealMenu:(UILongPressGestureRecognizer *)longPressRecognizer {
    if (longPressRecognizer.state == UIGestureRecognizerStateBegan) {
        [self.slidingViewController anchorTopViewToRightAnimated:YES];
    } else {
        if (longPressRecognizer.state == UIGestureRecognizerStateCancelled
            || longPressRecognizer.state == UIGestureRecognizerStateFailed
            || longPressRecognizer.state == UIGestureRecognizerStateEnded)
        {
            [self.slidingViewController resetTopViewAnimated:YES];
        }
    }
}

应该是action:@selector(revealMenu:)];放置一个冒号就足以让手势识别器将UIGestureRecognizer对象传递给动作。阅读 UIGestureRecognizer 参考指南

The action methods invoked must conform to one of the following signatures:

  • (void)handleGesture;
  • (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer;

Methods conforming to the latter signature permit the target in some cases to query the gesture recognizer sending the message for additional information. For example, the target could ask a UIRotationGestureRecognizer object for the angle of rotation (in radians) since the last invocation of the action method for this gesture.

如果您不关心该对象,请不要使用冒号;如果您需要它的数据,请将其包括在内。你想要后者。