Swipe Method is getting called twice

蓝咒 提交于 2019-12-12 01:42:11

问题


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");

    }
}


来源:https://stackoverflow.com/questions/7281512/swipe-method-is-getting-called-twice

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