ios - 一定时间后取消 UILongPressGestureRecognizer

标签 ios objective-c uicollectionview uigesturerecognizer

我在我的 UICollectionView 中使用了 UILongPressGestureRecognizer。现在,当我在一定时间后(例如 1 秒)将手指放在 CollectionView 项目上时,我希望我的 UILongPressGestureRecognizer 结束并执行特定代码:

if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {}

这是我的代码:

- (void)viewDidLoad {
  [super viewDidLoad];
  Public = [[PublicMethods alloc]init];
  self.view.backgroundColor = [UIColor whiteColor];
  [self.view addSubview:self.collect];

  UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(handleLongPress:)];
  lpgr.minimumPressDuration = 3; //seconds
  lpgr.delaysTouchesBegan = YES;
  lpgr.delegate = self;
  [self.collect addGestureRecognizer:lpgr];


}
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {
  if (gestureRecognizer.state != UIGestureRecognizerStateEnded) {
    return;
  }

  CGPoint p = [gestureRecognizer locationInView:self.collect];
  NSIndexPath *indexPath = [self.collect indexPathForItemAtPoint:p];
  if (indexPath == nil){
    NSLog(@"couldn't find index path");
  } else {
    // get the cell at indexPath (the one you long pressed)
    //CollectionViewCell* cell = (CollectionViewCell*)[self.collect cellForItemAtIndexPath:indexPath];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"bala" message:@"jalaaaa" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
    [alert show];
  }
}

最佳答案

您可以在 UILongPressGestureRecognizer 启动时实例化一个计时器,然后在计时器完成后取消手势并执行“结束手势”代码,例如(使用 1 秒的时间限制):

- (void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        // Create a timer that calls cancel: 2.5 second after the 
        // gesture begins (i.e. 3 seconds after the button press if
        // if lpgr.minimumPressDuration = .5;. Pass the gesture
        // recognizer along within the user info dictionary parameter.
        timer = [NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:@selector(cancel:) userInfo:[NSDictionary dictionaryWithObjectsAndKeys:gestureRecognizer, @"gestureRecognizer", nil] repeats:NO];

    } else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        // Assuming you still want to execute the end code
        // if the user lifts their finger before the 3 seconds
        // is complete, use the same method called in the timer.
        [self cancel:nil];
    }
}

- (void)cancel:(NSTimer*)timerObject {

    NSLog(@"%@",[timer.userInfo objectForKey:@"gestureRecognizer"]);
    // Get the gesture recognizer from the info dictionary
    UIGestureRecognizer *gestureRecognizer = [timer.userInfo objectForKey:@"gestureRecognizer"];

    CGPoint p = [gestureRecognizer locationInView:self.collect];
    NSIndexPath *indexPath = [self.collect indexPathForItemAtPoint:p];
    if (indexPath == nil){
        NSLog(@"couldn't find index path");
    } else {
        // get the cell at indexPath (the one you long pressed)
        //CollectionViewCell* cell = (CollectionViewCell*)[self.collect cellForItemAtIndexPath:indexPath];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"bala" message:@"jalaaaa" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
        [alert show];
    }

    // Disable it and re-enable it to cancel the gesture
    gestureRecognizer.enabled = NO;
    gestureRecognizer.enabled = YES;

    // Invalidate the timer
    [timer invalidate];
    timer = nil;
}

关于ios - 一定时间后取消 UILongPressGestureRecognizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28414690/

相关文章:

ios - UISwitch:之前做了什么:附加选择器,还是改变状态?

objective-c - 合成的实例变量是作为私有(private)的而不是 protected 生成的吗?

ios - 带有 Xcode 代码完成的 Objective-C block 属性

ios - Swift 2,滚动后的 UiCollectionview - 更改的单元格数据

ios - 由于代码生成核心数据问题,Swift 3 Xcode 8 无法存档应用程序

iphone - SQLite 或 Core Data 用于只有查询的大型数据库

ios - uibutton 图像在左侧,文本在中心

ios - 无法设置引用 socket

ios - UICollectionViewFlowLayout Voice Over 使用流布局读取无序的项目

ios - UICollectionView 给出 "Index out of range"错误