美文网首页
利用UIPanGestureRecognizer 判断手势滑动方

利用UIPanGestureRecognizer 判断手势滑动方

作者: juefeiye | 来源:发表于2019-07-10 13:55 被阅读0次

    /**

    • 添加平移手势
    • @param view
      */
    • (void)addTapGestureWithView:(UIView *)view
      {

      UIPanGestureRecognizer *recognizer = [[UIPanGestureRecognizer alloc]
      initWithTarget:self action:@selector(handleSwipe:)];
      [self.view addGestureRecognizer:recognizer];
      [recognizer release];
      }

    /**

    • 平移手势响应事件
    • @param swipe swipe description
      */
    • (void)handleSwipe:(UIPanGestureRecognizer *)swipe
      {

      if (swipe.state == UIGestureRecognizerStateChanged) {
      [self commitTranslation:[swipe translationInView:self.view]];
      }
      }

    /**

    • 判断手势方向
    • @param translation translation description
      */
    • (void)commitTranslation:(CGPoint)translation
      {

      CGFloat absX = fabs(translation.x);
      CGFloat absY = fabs(translation.y);

      // 设置滑动有效距离
      if (MAX(absX, absY) < 10)
      return;

    if (absX > absY ) {
        
        if (translation.x<0) {
            
            //向左滑动
        }else{
        
            //向右滑动
        }
      
    } else if (absY > absX) {
        if (translation.y<0) {
            
            //向上滑动
        }else{
            
            //向下滑动
        }
    }
    

    }

    相关文章

      网友评论

          本文标题:利用UIPanGestureRecognizer 判断手势滑动方

          本文链接:https://www.haomeiwen.com/subject/slqhkctx.html