我让UIPanGestureRecognizer只检测大多数垂直平底锅,我怎么才能让它只检测真正的垂直平底锅?

时间:2013-04-14 19:52:33

标签: ios objective-c uigesturerecognizer uipangesturerecognizer

我刚刚实现了这个:

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

(如概述here。)

但是如果用户在对角线上垂直平移它将开始。如何使公差对于它认为垂直的更严格?

基本上,下图描述了我的目标。第一个图是它现在检测到的,该区域内的任何内容,第二个图是我希望它做的。

enter image description here

3 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:1)

检测纯垂直手势,我假设translation.x == 0

你也应该从你引用的帖子中检查正确的答案。他将以前的位置与当前位置进行比较。你可以创造感性。你可以查看我的project,例如,在我使用这种敏感性定义,行动有效(小于或等于敏感度)或无效(大于敏感度)时。查看MOVEMENT_SENSIBILITY内的RPSliderViewController.m

答案 2 :(得分:0)

我曾为此目的编写了一个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