用导航项向下滑动手势

时间:2014-08-11 04:40:19

标签: ios objective-c menu navigation swipe

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

- (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];

    }
}

谢谢!

2 个答案:

答案 0 :(得分:0)

试试这个

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

        [self.navigationController popViewControllerAnimated:YES];


    }
}

答案 1 :(得分:0)

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

@IBOutlet var swipeDownGestureRecognizer: UISwipeGestureRecognizer!

第二,将识别器添加到导航栏中:

navigationController?.navigationBar.addGestureRecognizer(swipeDownGestureRecognizer)