我有一個顯示自定義視圖單元格的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;
}
你可以更詳細在handleLongPress:選擇器上。通常問題是相反的:Gesturerecognizer竊取所有觸動:-) http://stackoverflow.com/questions/4885693/how-do-you-stop-uitapgesturerecognizer-from-catching-every-tap – thst