【问题标题】:Swipe Down Gesture with Navigation Item使用导航项向下滑动手势
【发布时间】:2014-08-11 04:40:18
【问题描述】:

您好,我想使用向下滑动手势导航回主菜单。问题是我正在使用导航项(源自导航控制器),因此没有检测到向下滑动。我该如何解决这个问题?

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIScreenEdgePanGestureRecognizer *bezelSwipeGestureRecognizer = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(swipeBack:)];
    bezelSwipeGestureRecognizer.edges = UIRectEdgeTop;
    bezelSwipeGestureRecognizer.delegate = self;
    [self.view addGestureRecognizer:bezelSwipeGestureRecognizer];

    UIView *invisibleScrollPreventer = [UIView new];
    invisibleScrollPreventer.frame = CGRectMake(0, 0, self.view.frame.size.width, 100);
    [self.view addSubview:invisibleScrollPreventer];

}

-(void)swipeBack:(UIScreenEdgePanGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateEnded) {
        NSLog(@"Top Swipe");

        [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];

    }
}

谢谢!

【问题讨论】:

    标签: ios objective-c menu navigation swipe


    【解决方案1】:

    试试这个

    -(void)swipeBack:(UIScreenEdgePanGestureRecognizer *)recognizer
    {
        if (recognizer.state == UIGestureRecognizerStateEnded)
        {
            NSLog(@"Top Swipe");
    
            [self.navigationController popViewControllerAnimated:YES];
    
    
        }
    }
    

    【讨论】:

    • 很抱歉我没有把我的问题说得很清楚。基本上我遇到的问题是我目前使用的滑动甚至没有注册!我使用的代码在 ViewControllers 上工作得非常好,只是在 UITableViews 和 navigationsItems 上工作得很好。
    【解决方案2】:

    首先,为您的手势识别器创建一个出口:

    @IBOutlet var swipeDownGestureRecognizer: UISwipeGestureRecognizer!
    

    其次,将识别器添加到导航栏:

    navigationController?.navigationBar.addGestureRecognizer(swipeDownGestureRecognizer)
    

    【讨论】: