UIPanGestureRecognizer上下左右滑动方向判断算法

CGFloat const gestureMinimumTranslation = 20.0 ;

typedef enum : NSInteger {

    kCameraMoveDirectionNone,

    kCameraMoveDirectionUp,

    kCameraMoveDirectionDown,

    kCameraMoveDirectionRight,

    kCameraMoveDirectionLeft

} CameraMoveDirection ;

@interface ViewController ()

{

    CameraMoveDirection direction;

}

@end

@implementation ViewController

- ( void )viewDidLoad

{

    [ super viewDidLoad];

    UIPanGestureRecognizer *recognizer = [[ UIPanGestureRecognizer alloc] initWithTarget: self action: @selector (handleSwipe:)];

    [ self .viewWithGestureRecognizer addGestureRecognizer:recognizer];

}

// This is my gesture recognizer handler, which detects movement in a particular

// direction, conceptually tells a camera to start moving in that direction

// and when the user lifts their finger off the screen, tells the camera to stop.

- ( void )handleSwipe:( UIPanGestureRecognizer *)gesture

{

    CGPoint translation = [gesture translationInView: self .view];

    if (gesture.state == UIGestureRecognizerStateBegan )

    {

        direction = kCameraMoveDirectionNone;

    }

    else if (gesture.state == UIGestureRecognizerStateChanged && direction == kCameraMoveDirectionNone)

    {

        direction = [ self determineCameraDirectionIfNeeded:translation];

        // ok, now initiate movement in the direction indicated by the user's gesture

        switch (direction) {

            case kCameraMoveDirectionDown:

                NSLog (@ "Start moving down" );

                break ;

            case kCameraMoveDirectionUp:

                NSLog (@ "Start moving up" );

                break ;

            case kCameraMoveDirectionRight:

                NSLog (@ "Start moving right" );

                break ;

            case kCameraMoveDirectionLeft:

                NSLog (@ "Start moving left" );

                break ;

            default :

                break ;

        }

    }

    else if (gesture.state == UIGestureRecognizerStateEnded )

    {

        // now tell the camera to stop

        NSLog (@ "Stop" );

    }

}

// This method will determine whether the direction of the user's swipe

- ( CameraMoveDirection )determineCameraDirectionIfNeeded:( CGPoint)translation

{

    if (direction != kCameraMoveDirectionNone)

        return direction;

    // determine if horizontal swipe only if you meet some minimum velocity

    if (fabs(translation.x) > gestureMinimumTranslation)

    {

        BOOL gestureHorizontal = NO;

        if (translation.y == 0.0 )

            gestureHorizontal = YES;

        else

            gestureHorizontal = (fabs(translation.x / translation.y) > 5.0 );

        if (gestureHorizontal)

        {

            if (translation.x > 0.0 )

                return kCameraMoveDirectionRight;

            else

                return kCameraMoveDirectionLeft;

        }

    }

    // determine if vertical swipe only if you meet some minimum velocity

    else if (fabs(translation.y) > gestureMinimumTranslation)

    {

        BOOL gestureVertical = NO;

        if (translation.x == 0.0 )

            gestureVertical = YES;

        else

            gestureVertical = (fabs(translation.y / translation.x) > 5.0 );

        if (gestureVertical)

        {

            if (translation.y > 0.0 )

                return kCameraMoveDirectionDown;

            else

                return kCameraMoveDirectionUp;

        }

    }

    return direction;

}

@end


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值