2011-09-02 134 views
0

我必須在我的視圖控制器中使用滑動功能。因此,無論何時Iam滑動,我的滑動方法都會被調用兩次,而且我在(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"); 
} 

請告訴我,我只需要調用一次就可以做什麼。

回答

0

這就是應該發生的事情。你需要看看state屬性,你會發現像UIGestureRecognizerStateBeganUIGestureRecognizerStateEnded

0

試試這個,識別有各種狀態像

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




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


    if (recognizer.state == UIGestureRecognizerStateEnded) { 


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

    } 
}