2011-10-07 17 views
3

我在單元格中使用UILongPressGestureRecognizer。 我需要的是:當用戶點擊一個單元格1.0秒時,調用一個視圖控制器。 如果用戶點擊該單元,則另一個VC。UITableViewCell上的UILongPressGestureRecognizer - 雙重調用

我可以通過使用UILongPressGestureRecognizer來實現。但問題是,這是調用viewController兩次。

代碼:

if (indexPath.section == 0 && indexPath.row == 1){ 
    UILongPressGestureRecognizer *longPressTap = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(memberListWithSearchOptions)]; 

    longPressTap.minimumPressDuration = 1.0; 

    [cell addGestureRecognizer:longPressTap]; 
    [longPressTap release]; 
} 

我想,我需要的是,認識到長按之後,禁用識別,直到上的tableView屏幕再次出現。

我該怎麼做?

感謝,

RL

+0

手勢識別器解釋的手勢可以是* discrete *或* continuous *。像UILongPressGestureRecognizer這樣的連續的手勢識別器可以多次觸發它們的動作方法。如果手勢開始時你想做一些邏輯或者UIGestureRecognizerEnded,你可以檢查手勢識別器的'state'屬性是否設置爲'UIGestureRecognizerBegan' '當手勢完成時做一些邏輯。 – albertamg

回答

7

,而不是禁用它,你可能需要做的是檢查的手勢識別的state屬性,如果狀態是UIGestureRecognizerStateBegan(或UIGestureRecognizerStateEnded)僅顯示下一個視圖控制器。

你需要改變你的方法接受手勢識別作爲參數(也更新@selector參數),並檢查它的狀態:

UILongPressGestureRecognizer *longPressTap = 
    [[UILongPressGestureRecognizer alloc] initWithTarget:self 
     action:@selector(memberListWithSearchOptions:)]; //colon at end 

//... 

- (void)memberListWithSearchOptions:(UILongPressGestureRecognizer *)lpt 
{ 
    if (lpt.state == UIGestureRecognizerStateBegan) 
     //or check for UIGestureRecognizerStateEnded instead 
    { 
     //display view controller... 
    } 
} 
-1

你必須檢查狀態,如下

- (void)memberListWithSearchOptions:(UILongPressGestureRecognizer*)sender { 
if (sender.state == UIGestureRecognizerStateEnded) { 
    NSLog(@"UIGestureRecognizerStateEnded"); 
    //Do Whatever You want on End of Gesture 
} 
else if (sender.state == UIGestureRecognizerStateBegan){ 
    NSLog(@"UIGestureRecognizerStateBegan."); 
    //Do Whatever You want on Began of Gesture 
}