我已经让 UIPanGestureRecognizer 只检测大部分垂直平移,我如何让它只检测真正的垂直平移? [英] I've made UIPanGestureRecognizer only detect mostly vertical pans, how do I make it only detect REALLY vertical pans?

查看:52
本文介绍了我已经让 UIPanGestureRecognizer 只检测大部分垂直平移,我如何让它只检测真正的垂直平移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚实现了这个:

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)panGestureRecognizer {
    CGPoint translation = [panGestureRecognizer translationInView:someView];
    return fabs(translation.y) > fabs(translation.x);
}

(如此处所述.)

但是如果用户在对角线上垂直平移,它将开始.对于它认为的垂直方向,我如何使其容差更加严格?

But if the user pans vertically just over the diagonal it will start. How do I make the tolerance much more strict for what it considers vertical?

基本上,下图描述了我所追求的.第一张图是它现在检测到的,该区域内的任何东西,第二张图是我想要它做什么.

Basically, the image below describes what I'm after. The first diagram is what it detects now, anything within that area, and the second is what I want it to do.

推荐答案

您可以使用 atan2f 给定 xy 值来计算从垂直角度.例如,要在与垂直方向的角度小于 4 度时开始手势,您可以执行以下操作:

You can use atan2f given x and y values to calculate the angle from vertical. For example, to start the gesture if the angle is less than 4 degrees from vertical, you can do something like this:

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gesture {
    CGPoint translation = [gesture translationInView:gesture.view];
    if (translation.x != 0 || translation.y != 0) {
        CGFloat angle = atan2f(fabs(translation.x), fabs(translation.y));
        return angle < (4.0 * M_PI / 180.0); // four degrees, but in radians
    }
    return FALSE;
}

这篇关于我已经让 UIPanGestureRecognizer 只检测大部分垂直平移,我如何让它只检测真正的垂直平移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆