2017-02-27 92 views
0

我有一個顯示自定義視圖單元格的tableview。在viewWillAppear我已經設置了一個長按手勢識別器在UITableView。我長時間的按下是發射並顯示關於長時間按壓的細胞的信息。但是,當我放開按下時,didSelectRowAtIndexPath方法正在觸發。是否有辦法在長按之後取消觸摸,以便選擇行不會被觸發?長按後如何取消觸摸?

我見過didSelectRowAtIndexPath called after long press這個問題似乎沒有足夠的答案來解決問題。

@implementation ViewController 
UILongPressGestureRecognizer *lpgr; 
. 
. 
. 

- (void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:animated]; 

    // setup long press 

    lpgr = [[UILongPressGestureRecognizer alloc] 
              initWithTarget:self action:@selector(handleLongPress:)]; 
    lpgr.minimumPressDuration = 0.5; //seconds 
    lpgr.delegate = self; 
    lpgr.cancelsTouchesInView = true; 
    [self.myTableview addGestureRecognizer:lpgr]; 
    [self.myTableview.panGestureRecognizer requireGestureRecognizerToFail:lpgr]; ... 
. 
. 
. 

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer 
{ 

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { 

     CGPoint p = [gestureRecognizer locationInView:self.myTableview]; 

     NSIndexPath *indexPath = [self.myTableview indexPathForRowAtPoint:p]; 
     if (indexPath == nil) { 
      NSLog(@"long press on table view but not on a row"); 
     } else { 
      UITableViewCell *cell = [self.myTableview cellForRowAtIndexPath:indexPath]; 
      CensusData *currentUser; 
      if(self.isFiltered){ 
       currentUser = (CensusData*)[self.filteredTableData objectAtIndex:indexPath.row]; 
      }else{ 
       currentUser = (CensusData*)[self.dataArray objectAtIndex:indexPath.row]; 
      } 
      NSLog(@"CURRENT ROW WITH LONG PRESS: %@", currentUser.friendlyName); 

     } 
    } 
} 
. 
. 
. 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ 
    return YES; 
} 
+0

你可以更詳細在handleLongPress:選擇器上。通常問題是相反的:Gesturerecognizer竊取所有觸動:-) http://stackoverflow.com/questions/4885693/how-do-you-stop-uitapgesturerecognizer-from-catching-every-tap – thst

回答

1

雖然姿態是積極的(開始而尚未結束)在表視圖禁用選擇...

- (void)handleLongPress:(UILongPressGestureRecognizer *)gr { 
    if (gr.state == UIGestureRecognizerStateBegan) { 
     self.myTableview.allowsSelection = NO; 
    } else if (gr.state == UIGestureRecognizerStateEnded) { 
     self.myTableview.allowsSelection = YES; 
    } 
} 

無需設置代理,就將cancelsTouches,或實施shouldRecognize ... (除非你需要這些東西)。

編輯此vc是一個最低限度完整的測試。它需要與連接到出口和VC爲數據源和委託表視圖故事板...

#import "ViewController.h" 

@interface ViewController() <UITableViewDataSource, UITableViewDelegate> 
@property(weak,nonatomic) IBOutlet UITableView *tableView; 
@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    UILongPressGestureRecognizer *gr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)]; 
    [self.tableView addGestureRecognizer:gr]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return 50; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 
    cell.textLabel.text = [NSString stringWithFormat:@"Row %ld", indexPath.row]; 
    return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSLog(@"selected %@", indexPath); 
} 

- (void)handleLongPress:(UILongPressGestureRecognizer *)gr { 
    if (gr.state == UIGestureRecognizerStateBegan) { 
     NSLog(@"long press began"); 
     self.tableView.allowsSelection = NO; 
    } else if (gr.state == UIGestureRecognizerStateEnded) { 
     NSLog(@"long press ended"); 
     self.tableView.allowsSelection = YES; 
    } 
} 


- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 
+0

我'我試着添加代碼來禁用/重新啓用選擇。這不起作用。看起來這是因爲這會在觸發長按之後禁用對觸摸的選擇,並因此導致點擊,而不是觸摸本身。 – propstm

+0

嗯。驚訝聽到這一點。我在回答之前對此進行了測試。我應該發佈全班嗎? – danh

+0

請。我還在示例項目中構建了此表,並嘗試確定是否有其他內容導致此功能/問題 – propstm

0

您可以禁用的tableview則僅longGesture工作正常

UILongPressGestureRecognizer* longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(onLongPress:)]; 
[self.myTableview addGestureRecognizer:longPressRecognizer]; 



-(void)onLongPress:(UILongPressGestureRecognizer*)pGesture 
{ 

    if (pGesture.state == UIGestureRecognizerStateRecognized) 
    { 
     //Do something to tell the user! 
    } 
    if (pGesture.state == UIGestureRecognizerStateEnded) 
    { 
     CGPoint p = [pGesture locationInView:self.myTableview]; 

     NSIndexPath *indexPath = [self.myTableview indexPathForRowAtPoint:p]; 

    } 

} 

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ 
    if ([touch.view isDescendantOfView:self.myTableview]) { 

     // Don't let selections of auto-complete entries fire the 
     // gesture recognizer 
     return NO; 
    } 

    return YES; 

}