Swipe Method is getting called twice

2019-08-30 19:02发布

问题:

I have to use swipe functionality in my view controller. so, whenever Iam swiping, my swipe method is getting called twice and the NSlogs which I Wrote inside the (swipe:) method is displaying the content two times.

Here is the code which i have used.

UIView *swipeView=[[UIView alloc]initWithFrame:CGRectMake(405, 420, 265, 35)];
    swipeView.backgroundColor=[UIColor clearColor];
    [self.view addSubview:swipeView];
    UISwipeGestureRecognizer *gesture;
    gesture=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
    [gesture setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [swipeView addGestureRecognizer:gesture];
    [gesture release];
    [swipeView release];


 -(void)swipe:(UISwipeGestureRecognizer *)recognizer {
    NSLog(@"Swipe received.");
    NSLog(@"HIJ");
}

please tell me what i have to do for calling it only one time.

回答1:

That's what's supposed to happen. You need to look at the state property where you'll find things like UIGestureRecognizerStateBegan and UIGestureRecognizerStateEnded.



回答2:

Try this, recognizer has various state like

UIGestureRecognizerStatePossible,
UIGestureRecognizerStateBegan,
UIGestureRecognizerStateChanged,
UIGestureRecognizerStateEnded,
UIGestureRecognizerStateCancelled,
UIGestureRecognizerStateFailed,
UIGestureRecognizerStateRecognized = UIGestureRecognizerStateEnded




-(void)swipe:(UISwipeGestureRecognizer *)recognizer {


    if (recognizer.state == UIGestureRecognizerStateEnded) {


        NSLog(@"Swipe received.");
        NSLog(@"HIJ");

    }
}