0

我必须在我的视图控制器中使用滑动功能。因此,每当我滑动时,我的滑动方法都会被调用两次,并且我在 (swipe:) 方法中编写的 NSlogs 会显示两次内容。

这是我使用的代码。

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

请告诉我我必须怎么做才能只调用一次。

4

2 回答 2

0

试试这个,识别器有各种状态,比如

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




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


    if (recognizer.state == UIGestureRecognizerStateEnded) {


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

    }
}
于 2011-09-02T09:53:48.713 回答
0

这就是应该发生的事情。您需要查看可以找到和之state类的属性。UIGestureRecognizerStateBeganUIGestureRecognizerStateEnded

于 2011-09-02T09:29:07.970 回答