HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 一起识别长按和平移手势识别器大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个观点,我已经添加了泛和长按UIGestureRecognizer.平底锅用于移动视图.我想做的是注意触摸已停止移动(同时保持活动状态)并触发长按.

我发现在平底锅开始后从未触发长按.我已经尝试设置委托并实现:

- (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能为我做状态机的东西.

解决方法

如果其他人需要它,这里的代码我有用.目标是使视图对长按和平移敏感,包括不按平底锅的长按,反之亦然.

// 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];
        }
    }
}

我们给视图的计时器用户信息.您可能需要存储手势状态的其他部分,如位置等.使用用户信息字典以相同的方式.

- (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 – 一起识别长按和平移手势识别器全部内容,希望文章能够帮你解决ios – 一起识别长按和平移手势识别器所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。