【问题标题】:Cancel a UILongPressGestureRecognizer after a certain time一定时间后取消 UILongPressGestureRecognizer
【发布时间】:2015-02-09 16:38:39
【问题描述】:

我在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];
  }
}

【问题讨论】:

  • 嘿,您希望它在您的 minimumPressDuration = 3 后 1 秒或 3 秒后发生吗?
  • @LyndseyScott 我希望在 3 秒结束手势后将手指放在收集单元上...
  • 如果您希望minimumPressDuration 和最大持续时间都为 3,您可以简单地将结束代码放在 if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { 条件中。
  • 我当前的答案假设您希望手势在被识别后 3 秒取消,例如,如果手势在用户开始按下屏幕后 3 秒开始,它将在 6 秒内取消。仅供参考
  • @LyndseyScott 我的朋友,你的答案不起作用!!!

标签: ios objective-c uicollectionview uigesturerecognizer


【解决方案1】:

您可以在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;
}

【讨论】:

  • 我的朋友这个答案不起作用。我得到这个按摩“找不到索引路径”
  • @mamal10 那行代码对你有用吗?我根本没有改变你的那部分代码。
  • @mamal10 如果它实际上对您有用,请查看我更新的答案,因为它可能会有所作为。我已经移动了手势取消代码,所以它在你的“结束手势”代码之后。
  • 我的朋友我想要在 3 秒结束手势后将手指放在单元格上,即使我将手指放在单元格上
  • @mamal10 然后(就像我在各种 cmets 中提到的那样)要么更改您的 minimumPressDuration 和计时器持续时间,使得 minimumPressDuration + 计时器持续时间 == 3 以便自动识别手势,或者只是将您的结束代码放入if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { conditional,这样一旦手势在 3 秒(minimumPressDuration)被识别,代码就会立即执行。
【解决方案2】:
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
    longPress.minimumPressDuration = 0.4; // !!! Your certain time
    [self.view addGestureRecognizer:longPress];
    
    - (void)handleLongPress:(UILongPressGestureRecognizer *)gesture
    {
    // Catch first gesture changing and reset gesture
    if (gesture.state == UIGestureRecognizerStateChanged) {
        if (self.delegate) {
            AudioServicesPlaySystemSound(1520);
            [self.delegate call];
            gesture.enabled = NO; // Reset gesture
            gesture.enabled = YES;
        }
    }

【讨论】:

    猜你喜欢
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多