5

我使用手勢識別:如何確定用戶是否在UITableViewCell上按下了2秒鐘?

初始化viewDidLoad

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

這是longPress樣子:

- (void)longPress:(UILongPressGestureRecognizer*)gestureRecognizer { 
if (gestureRecognizer.minimumPressDuration == 2.0) { 
    NSLog(@"Pressed for 2 seconds!"); 
} 
} 

我怎麼能扎到這一點?

- (void)tableView:(UITableView *)tblView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 

didSelectRowAtIndexPath將如何獲得對gestureRecognizer.minimumPressDuration的引用?

基本上我想要實現的是:

**If a user clicks on a cell, check to see if the press is 2 seconds.** 

回答

3

嘗試通過提供的tableView將它添加到的UITableViewCell代替的UITableView:willDisplayCell:forRowAtIndexPath:方法如下所示:

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    [cell addGestureRecognizer:longPressRecognizer]; 
}
2

你可以嘗試,而不是tableview中添加gesturerecognizer到tableviewcell。 UITableViewCells派生自UIView,因此他們可以接受手勢識別器。

+0

你能告訴我的代碼? – 2010-07-23 00:29:33

+1

史蒂夫似乎已經做到了。 – 2010-07-23 01:03:24

1

添加的indexpath.row到tableviewcell的標籤

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
} 
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(rowButtonAction:)]; 
[cell setTag:indexPath.row]; 
[cell addGestureRecognizer:longPressRecognizer]; 
[longPressRecognizer release]; 

// Configure the cell... 
Group *gp = [_currentItemArray objectAtIndex:indexPath.row]; 
cell.textLabel.text = gp.name; 


return cell; 

}

然後通過使用[[gestureRecognizer視圖]標籤] (在我的代碼中它的長按myModalViewController.previousObject = [_currentItemArray objectAtIndex:[[sender view] tag]];

-(IBAction)rowButtonAction:(UILongPressGestureRecognizer *)sender{ 
if (sender.state == UIGestureRecognizerStateEnded) { 
    GroupsAdd_EditViewController *myModalViewController = [[[GroupsAdd_EditViewController alloc] initWithNibName:@"GroupsAdd_EditViewController" bundle:nil] autorelease]; 
    myModalViewController.titleText = @"Edit Group"; 
    myModalViewController.context = self.context; 
    myModalViewController.previousObject = [_currentItemArray objectAtIndex:[[sender view] tag]]; 
    [self.navigationController presentModalViewController:myModalViewController animated:YES]; 
} 

}

相關問題