5

używam aparatów rozpoznawania gestuJak ustalić, czy użytkownik nacisnął przycisk UITableViewCell na 2 sekundy?

Inicjowanie w viewDidLoad:

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

To co longPress wygląda następująco:

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

Jak mogę związać to do?

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

Jak didSelectRowAtIndexPath uzyskać odniesienie do gestureRecognizer.minimumPressDuration?

Zasadniczo co próbuję osiągnąć to:

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

Odpowiedz

3

spróbuj dodać go do UITableViewCell zamiast UITableView dostarczając tableView: willDisplayCell: forRowAtIndexPath: Metoda tak:

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

Można spróbować dodawania gesturerecognizer do tableviewcell zamiast tableview. UITableViewCells pochodzą z UIView, jako że mogą akceptować rozpoznawanie gestów.

+0

Czy możesz mi pokazać w kodzie? –

+1

Steve wydaje się, że właśnie to zrobił. –

1

dodać do indexpath.row do tagu tableviewcell za

- (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; 

}

a następnie dostęp do tego znacznika w longPress przy użyciu znacznika [[gestureRecognizer view]] (w moim kodzie część z tagiem myModalViewController.previousObject = [_currentItemArray objectAtIndex: [[widok nadawcy]]];

-(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]; 
} 

}

Powiązane problemy