我做了UIPanGestureRecognizer只检测主要是垂直锅,我怎么做,只检测垂直真的锅?(

2019-08-23 02:15发布

我只是实现了这个:

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

(正如描述在这里 。)

但是,如果用户平底锅垂直刚刚超过对角线将开始。 如何让我的宽容更它所认为垂直严格?

基本上,下面的图片描述了我后。 第一个图是什么,现在检测到,任何区域内,第二个就是我想要做的事。

Answer 1:

您可以使用atan2f给出xy值从垂直计算角度。 例如,要启动手势如果角度是从垂直小于4度,你可以这样做:

- (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;
}


Answer 2:

检测纯垂直的姿态,我认为translation.x == 0即可。

你应该,那么请从你引用的帖子正确的答案。 在那里,他以前的位置与当前一个比较。 您可以创建的敏感性。 您可以检查我的项目 ,例如一看就知道,在这里我用这个敏感性来定义,当一个动作是有效的(小于或大于感性等于)或无效(大于感性)。 检查MOVEMENT_SENSIBILITY里面RPSliderViewController.m



Answer 3:

我写了一个UIGestureRecognizer子为此目的一次。 它只跟踪垂直转换。 也许这可以帮助你。 你可以使用它像任何其他手势识别,只需设置阈值并跟踪它的目标的操作方法翻译。

VerticalPanGestureRecognizer.h

#import <UIKit/UIKit.h>
#import <UIKit/UIGestureRecognizerSubclass.h>

@interface VerticalPanGestureRecognizer : UIGestureRecognizer

@property (assign, nonatomic)float translation;
@property (assign, nonatomic)float offsetThreshold;

@end

VerticalPanGestureRecognizer.m

#import "VerticalPanGestureRecognizer.h"

@interface VerticalPanGestureRecognizer ()
{
    CGPoint _startPoint;
}
@end

@implementation VerticalPanGestureRecognizer

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if ([touches count] > 1) {
        self.state = UIGestureRecognizerStateFailed;
    }
    else
    {
        _startPoint = [[touches anyObject] locationInView:self.view];
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (self.state == UIGestureRecognizerStateFailed || self.state == UIGestureRecognizerStateCancelled) {
        return;
    }
    CGPoint currentLocation = [[touches anyObject] locationInView:self.view];
    CGPoint translation;
    translation.x = currentLocation.x - _startPoint.x;
    translation.y = currentLocation.y - _startPoint.y;        

    if (self.state == UIGestureRecognizerStatePossible)
    {
        //if the x-translation is above our threshold the gesture fails 
        if (fabsf(translation.x) > self.offsetThreshold)
            self.state = UIGestureRecognizerStateFailed;
        //if the y-translation has reached the threshold the gesture is recognized and the we start sending action methods
        else if (fabsf(translation.y) > self.offsetThreshold)
            self.state = UIGestureRecognizerStateBegan;

        return;                
    }        
    //if we reached this point the gesture was succesfully recognized so we now enter changed state
    self.state = UIGestureRecognizerStateChanged;

    //we are just insterested in the vertical translation
    self.translation = translation.y;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    //if at this point the state is still 'possible' the threshold wasn't reached at all so we fail
    if (self.state == UIGestureRecognizerStatePossible)
    {
        self.state = UIGestureRecognizerStateFailed;
    }
    else
    {
        CGPoint currentLocation = [[touches anyObject] locationInView:self.view];
        CGPoint translation;
        translation.x = _startPoint.x - currentLocation.x;
        translation.y = _startPoint.y - currentLocation.y;
        self.translation = translation.y;
        self.state = UIGestureRecognizerStateEnded;
    }

}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.state = UIGestureRecognizerStateCancelled;
}

- (void)reset
{
    [super reset];
    _startPoint = CGPointZero;
}

@end


文章来源: I've made UIPanGestureRecognizer only detect mostly vertical pans, how do I make it only detect REALLY vertical pans?