How to disable UISwipeGestureRecognizer when load a new view?

空扰寡人 提交于 2019-12-24 01:18:25

问题


in my viewDidLoad I set

UISwipeGestureRecognizer *swipeRecognizerU = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeUpDetected:)]; swipeRecognizerU.direction = UISwipeGestureRecognizerDirectionUp; [self.view addGestureRecognizer:swipeRecognizerU];

when I load a new view through popup I need to disable that gesture

// show popup view
-(IBAction)showPopup:(id)sender
{
    MJDetailViewController *detailViewController = [[MJDetailViewController alloc] initWithNibName:@"MJDetailViewController" bundle:nil];
    [self presentPopupViewController:detailViewController animationType:MJPopupViewAnimationSlideBottomBottom];
}

after popup view is dismissed, I need to set swipe gesture back.

// hide popup view
-(IBAction)hidePopup:(id)sender
{
    [self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationSlideBottomBottom];
}

how this can be done?


回答1:


I think there is property named enabled for UIGestureRecognizer. Have you try this, it should be ok to disable your swipes:

swipeGestureRecognizer.enabled = NO;



回答2:


You need set delegate at here.

Ex :

swipeleft=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeleft:)];
        swipeleft.direction=UISwipeGestureRecognizerDirectionLeft;
        swipeleft.delegate = self;
        [self.view addGestureRecognizer:swipeleft];

Then add function

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {


    if ((touch.view == test[1]) || (touch.view == test[2]) || (touch.view == test[3])) {

        [gestureRecognizer setCancelsTouchesInView:YES];
        [swipeleft setCancelsTouchesInView:YES];

        [gestureRecognizer setEnabled:NO];
        [swipeleft setEnabled:NO];



        return NO;

    }
    else
    {
        [gestureRecognizer setCancelsTouchesInView:NO];
        [swipeleft setCancelsTouchesInView:NO];

        [gestureRecognizer setEnabled:YES];
        [swipeleft setEnabled:YES];

    return YES;
    }
}

I think useful for you



来源:https://stackoverflow.com/questions/11376389/how-to-disable-uiswipegesturerecognizer-when-load-a-new-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!