• UIPanGestureRecognizer判断滑动的方向


    .h文件
    
    CGFloat const gestureMinimumTranslation = 20.0 ;
    typedef enum : NSInteger {
        kCameraMoveDirectionNone,
        kCameraMoveDirectionUp,
        kCameraMoveDirectionDown,
        kCameraMoveDirectionRight,
        kCameraMoveDirectionLeft
    } CameraMoveDirection ;
    
    @interface FCDetailViewController : FCMainViewController
    {
        CameraMoveDirection direction;
    }
    
    .m文件  --------
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        //监听滑动手势
        UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
        [self.view addGestureRecognizer:recognizer];
    }
    
    - (void)handleSwipeFrom:(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];
    
            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){
            //NSLog (@ "Stop" );
        }
    }
    
    - (CameraMoveDirection)determineCameraDirectionIfNeeded:(CGPoint)translation
    {
        if (direction != kCameraMoveDirectionNone)
            return direction;
     
        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;
            }
            
        }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;
    }
  • 相关阅读:
    【2021-01-07】叫我“何工”就好
    2021年的 目标计划
    转:我在拼多多的三年 https://www.leadroyal.cn/?p=1228#more-1228
    匀强电场等效重力场
    开源图片素材管理软件-civet
    心学#传习录学习
    工作感受月记202101月
    统计字典序元音字符的数量
    可被5整除的二进制
    假期刷题--JAVA
  • 原文地址:https://www.cnblogs.com/hw140430/p/4414006.html
Copyright © 2020-2023  润新知