我有一个添加了平移和长按UIGestureRecognizer的 View 。平移用于移动 View 。我想做的是还要注意,触摸已停止移动(同时保持事件状态)并触发了长按。

我发现,平底锅开始后,长按不会触发。我试过设置一个委托(delegate)并实现:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    NSLog(@"simultaneous %@", gestureRecognizer.class);
    return YES;
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    NSLog(@"require fail %@", gestureRecognizer.class);

    return [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer self]];
    // also tried return YES;
    // also tried return [gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer self]];
}

我试着愚弄pan gr的allowableMovement,也无济于事。我只是要放弃,而在pan gr中使用一个计时器,该计时器会失效,然后在移动时重置,但是我希望SDK可以为我做状态机的工作。

最佳答案

万一其他人需要它,这是对我有用的代码。目的是使 View 对长按和平移都敏感,包括长按之前没有平移的长按,反之亦然。

// setup
@property (strong,nonatomic) NSTimer *timer;          // triggers the long press during pan
@property (strong,nonatomic) UIView *longPressView;   // need this to track long press state

// view is the view we're interested in panning and long pressing
UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGR:)];
[view addGestureRecognizer:panGR];

// this starts a long press when no pan has occurred
UILongPressGestureRecognizer *longGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressGR:)];
[view addGestureRecognizer:longGR];

平移开始或发生变化时,请启动计时器。如果计时器在平底锅结束(触摸释放)之前到期,那么我们请长按一下。
- (void)panGR:(UIPanGestureRecognizer *)gr {
    if (gr.state == UIGestureRecognizerStateBegan) {
        [self startTimer:gr.view];
    } else if (gr.state == UIGestureRecognizerStateChanged) {
        [self startTimer:gr.view];

        // do whatever you want to do with pan state in this method
        // in my case, I'm translating the view here

    } else if (gr.state == UIGestureRecognizerStateEnded) {
        if (self.longPressView) {
            [self longPressEnded];
        } else {
            [self.timer invalidate];
        }
    }
}

我们为计时器用户提供该 View 的信息。您可能需要存储手势状态的其他部分,例如位置等。使用用户信息字典以相同的方式进行操作。
- (void)startTimer:(UIView *)view {
    if (self.longPressView) return;
    [self.timer invalidate];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.8 target:self
                                                selector:@selector(longPressTimer:)
                                                userInfo:@{ @"view": view} repeats:NO];
}

-(void)longPressTimer:(NSTimer *)timer {

    self.longPressView = timer.userInfo[@"view"];
    [self longPressBegan];
}

由于timer方法没有关联的gr,因此请排除掉我们通常放在gr处理程序中的所有逻辑,以便计时器处理程序和gr处理程序都可以调用它。
- (void)longPressGR:(UILongPressGestureRecognizer *)gr {

    if (gr.state == UIGestureRecognizerStateBegan) {
        self.longPressView = gr.view;
        [self longPressBegan];
    } else if (gr.state == UIGestureRecognizerStateEnded) {
        [self longPressEnded];
    }
}

- (void)longPressBegan {
    NSLog(@"long press began");
}

- (void)longPressEnded {

    self.longPressView = nil;
    NSLog(@"long press ended");
}

关于ios - 一起识别长按和平移手势识别器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23026047/

10-12 04:02