ios UIPinchGestureRecognizer在水平和垂直方向上的比例

ui7jx7zq  于 2023-06-05  发布在  iOS
关注(0)|答案(3)|浏览(351)

当使用UIPinchGestureRecognizer时,在水平和垂直方向分别检测/读取夹量秤的最佳方法是什么?我看到了这个帖子
UIPinchGestureRecognizer Scale view in different x and y directions
但我注意到有这么多人来回这样一个看似例行的任务,我不确定这是最好的答案/方式。
如果不使用UIPinchGestureRecognizer完全用于此目的是答案,那么在两个不同方向上检测捏秤的最佳方法是什么?

zed5wv10

zed5wv101#

基本上做到这一点,

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!")
    }
}
fruv7luv

fruv7luv2#

在我的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();
        }
    }

我计算捏“开始”时两点之间的距离,并将该值保存在两个私处。然后,我根据第一个测量距离和第二个(在“changed”中)计算缩放(fontScaling)。我使用我自己的视图(_previewView)设置为base(100%),但是你可以使用View.bounds.height或者width来代替。在我的例子中,我总是有一个正方形视图,所以在我的应用程序中高度==宽度。

wecizke3

wecizke33#

使用此扩展(基于先前的答案)。

extension UIPinchGestureRecognizer {

enum Direction {
    case horizontal, vertical, diagonal, undefined
}

var direction: Direction {

    if numberOfTouches < 2 {
        return .undefined
    }

    let A = location(ofTouch: 0, in: self.view)
    let B = location(ofTouch: 1, in: self.view)

    let xD = abs( A.x - B.x )
    let yD = abs( A.y - B.y )
    if (xD == 0) { return .vertical }
    if (yD == 0) { return .horizontal }
    let ratio = xD / yD

    if (ratio > 2) { return .horizontal }
    if (ratio < 0.5) { return .vertical }
    return .diagonal
}
}

相关问题