Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scale of UIPinchGestureRecognizer in horizontal and vertical directions separately

When using UIPinchGestureRecognizer what is the best way to detect/read the pinch scale in horizontal and vertical directions individually? I saw this post

UIPinchGestureRecognizer Scale view in different x and y directions

but I noticed there were so many going back and forth for such a seemingly routine task that I am not sure that is the best answer/way.

If not using UIPinchGestureRecognizer altogether for this purpose is the answer, what's the best way to detect pinch scale in two different directions?

like image 697
Dogahe Avatar asked Sep 10 '12 15:09

Dogahe


2 Answers

Basically do this,

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"
}

That function will return H, V, D for you .. horizontal, vertical, diagonal.

You would use it something like this ...

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!")
    }
}
like image 130
Fattie Avatar answered Nov 18 '22 23:11

Fattie


In my C# I do the following

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

I calculate the distance between the two points when pinch "began" and hold that value in two privates. Then I calculate a scaling (fontScaling) based on the first measured distance and the second one (in "changed"). I use my own view (_previewView) to set as base (100%), but you can use View.Bounds.height instead or the width for that matter. in my case, I always have a square view, so height == width in my app.

like image 2
Stefan Bol Avatar answered Nov 18 '22 21:11

Stefan Bol