1

我刚刚实现了这个:

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

(如此所述。)

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

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

在此处输入图像描述

4

3 回答 3

4

您可以使用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;
}
于 2015-02-17T00:00:45.423 回答
1

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

您还应该从您引用的帖子中检查正确答案。他将先前的位置与当前位置进行比较。你可以创造感性。您可以查看我的项目,例如查看我使用这种敏感性定义的地方,一个动作何时有效(小于或等于敏感性)或无效(大于敏感性)。检查MOVEMENT_SENSIBILITY内部RPSliderViewController.m

于 2013-04-14T19:54:58.837 回答
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
于 2013-04-14T20:50:02.883 回答