【问题标题】:How can I determine if a user has pressed on a UITableViewCell for 2 seconds?如何确定用户是否在 UITableViewCell 上按下了 2 秒?
【发布时间】:2010-07-23 00:22:38
【问题描述】:

我正在使用手势识别器:

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.**

【问题讨论】:

    标签: iphone objective-c cocoa-touch uitableview uigesturerecognizer


    【解决方案1】:

    尝试通过提供 tableView:willDisplayCell:forRowAtIndexPath: 方法将其添加到 UITableViewCell 而不是 UITableView:

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

    【讨论】:

      【解决方案2】:

      您可以尝试将手势识别器添加到 tableviewcell,而不是 tableview。 UITableViewCells 派生自 UIView,因此它们可以接受手势识别器。

      【讨论】:

        【解决方案3】:

        将 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 view] 标签] 在 longPress 中访问该标签 (在我的代码中,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];
        }
        

        }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-01-07
          • 2012-05-04
          • 2011-08-01
          • 2011-10-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多