【问题标题】:Scale of UIPinchGestureRecognizer in horizontal and vertical directions separatelyUIPinchGestureRecognizer在水平和垂直方向上的比例分别是
【发布时间】:2017-07-13 22:49:14
【问题描述】:

当使用 UIPinchGestureRecognizer 时,分别检测/读取水平和垂直方向的捏合比例的最佳方法是什么?我看到了这个帖子

UIPinchGestureRecognizer Scale view in different x and y directions

但我注意到,对于这样一个看似例行公事的任务,有很多人来回奔波,我不确定这是最好的答案/方式。

如果不完全使用 UIPinchGestureRecognizer 来达到这个目的,那么在两个不同方向上检测捏合比例的最佳方法是什么?

【问题讨论】:

    标签: ios scale pinch uipinchgesturerecognizer pinchzoom


    【解决方案1】:

    基本上就是这样,

    func _mode(_ sender: UIPinchGestureRecognizer)->String {
    
        // very important:
        if sender.numberOfTouches < 2 {
            print("avoided an obscure crash!!")
            return ""
        }
    
        let A = sender.location(ofTouch: 0, in: self.view)
        let B = sender.location(ofTouch: 1, in: self.view)
    
        let xD = fabs( A.x - B.x )
        let yD = fabs( A.y - B.y )
        if (xD == 0) { return "V" }
        if (yD == 0) { return "H" }
        let ratio = xD / yD
        // print(ratio)
        if (ratio > 2) { return "H" }
        if (ratio < 0.5) { return "V" }
        return "D"
    }
    

    该函数将为您返回 H、V、D .. 水平、垂直、对角线。

    你会像这样使用它......

    func yourSelector(_ sender: UIPinchGestureRecognizer) {
    
        // your usual code such as ..
        // if sender.state == .ended { return } .. etc
    
        let mode = _mode(sender)
        print("the mode is \(mode) !!!")
    
        // in this example, we only care about horizontal pinches...
        if mode != "H" { return }
    
        let vel = sender.velocity
        if vel < 0 {
            print("you're squeezing the screen!")
        }
    }
    

    【讨论】:

      【解决方案2】:

      在我的 C# 中,我执行以下操作

          private double _firstDistance = 0;
          private int _firstScaling = 0;
          private void PinchHandler(UIPinchGestureRecognizer pinchRecognizer)
          {
              nfloat x1, y1, x2, y2 = 0;
              var t1 = pinchRecognizer.LocationOfTouch(0, _previewView);
              x1 = t1.X;
              y1 = t1.Y;
              var t2 = pinchRecognizer.LocationOfTouch(1, _previewView);
              x2 = t2.X;
              y2 = t2.Y;
      
              if (pinchRecognizer.State == UIGestureRecognizerState.Began)
              {
                  _firstDistance = Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2));
                  _firstScaling = _task.TextTemplates[_selectedTextTemplate].FontScaling;
              }
              if (pinchRecognizer.State == UIGestureRecognizerState.Changed)
              {
                  var distance = Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2));
                  var fontScaling = Convert.ToInt32((distance - _firstDistance) / _previewView.Frame.Height * 100);
                  fontScaling += _firstScaling;
                  _task.TextTemplates[_selectedTextTemplate].FontScaling = fontScaling;
                  UpdateBitmapPreview();
              }
          }
      

      我在捏“开始”时计算两点之间的距离,并将该值保存在两个私人中。然后我根据第一个测量距离和第二个测量距离(在“更改”中)计算缩放(fontScaling)。 我使用自己的视图 (_previewView) 设置为基础 (100%),但您可以使用 View.Bounds.height 或宽度来代替。在我的情况下,我总是有一个方形视图,所以我的应用程序中的高度 == 宽度。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-15
        • 1970-01-01
        • 2011-09-10
        相关资源
        最近更新 更多