1

我有一个带有各种 IBOutlets 的自定义单元格,但是在一个按钮上,我想添加一个 UILongPressGestureRecognizer 用于长按手势。这是我的代码(顺便说一句,插座连接正确,IBAction按钮的方法被正确调用):

MyCustomCell.h

@interface MyCustomCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UIButton *myButton;
@property (strong, nonatomic) UILongPressGestureRecognizer *longPressGestureRecognizer;
@end

MyCustomCell.m

- (void)awakeFromNib
{
    // Initialization code
    self.longPressGestureRecognizer = nil;
}

MyViewController.m

#import MyCustomCell.h

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"MyCell";
    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (!cell){
        cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }

    cell.longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGestures:)];
    cell.longPressGestureRecognizer.minimumPressDuration = 1.0f;
    cell.longPressGestureRecognizer.allowableMovement = 300.0f;
    [cell.myButton addGestureRecognizer:cell.longPressGestureRecognizer];
}

- (void)handleLongPressGestures:(UIGestureRecognizer *)recognizer
{
    if ([recognizer.view isKindOfClass:[UIButton class]]){
        if (recognizer.state == UIGestureRecognizerStateBegan){
            NSLog(@"Long press began");
        } else if (recognizer.state = UIGestureRecognizerStateEnded){
            NSLog(@"Long press ended");
        }
    }
}

问题handleLongPressGestures:是永远不会调用方法。

4

3 回答 3

1

longPressGestureRecognizer应该是控制器上的属性,而不是视图(MyCustomCell)。将属性移到 MyViewController 并重试。我的猜测是当它对 MyCustomCell 进行排队和出队时会发生一些奇怪的事情。

重用的对象(单元)应该是轻量级的。在这种情况下,longPressGestureRecognizer 的目标是视图控制器并且是讨厌的。

于 2015-06-03T18:03:52.330 回答
0

尝试这个!

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UILongPressGestureRecognizer *LongPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPressGestures:)];
    LongPress.minimumPressDuration = 1.0f;
    LongPress.allowableMovement = 300.0f;
    [cell.myButton addGestureRecognizer:LongPress];
}

- (void)handleLongPressGestures:(UIGestureRecognizer *)recognizer
{
      if ([recognizer.view isKindOfClass:[UIButton class]]){
         if (recognizer.state == UIGestureRecognizerStateBegan){
             NSLog(@"Long press began");
         } 
         else if (recognizer.state = UIGestureRecognizerStateEnded)
         {
             NSLog(@"Long press ended");
         }
      }
 }
于 2015-06-04T03:50:33.060 回答
0

试试这种方式

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"MyCell";
    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    if (!cell){
        cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if ([[cell gestureRecognizers] count]<1) {
        UILongPressGestureRecognizer *longPressGestureRecognizer;
        longPressGestureRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPressGestures:)];
        longPressGestureRecognizer.minimumPressDuration = 1.0f;
        longPressGestureRecognizer.allowableMovement = 300.0f;
        longPressGestureRecognizer.delegate = self;
        [cell.myButton addGestureRecognizer:cell.longPressGestureRecognizer];
    }
}

这种类型的代码对我有用。

于 2015-06-04T05:15:57.520 回答