2012-09-10 22 views
10

Al usar UIPinchGestureRecognizer, ¿cuál es la mejor manera de detectar/leer la escala de pellizco en direcciones horizontal y vertical individualmente? Vi este postEscala de UIPinchGestureRecognizer en direcciones horizontal y vertical por separado

UIPinchGestureRecognizer Scale view in different x and y directions

pero me di cuenta de que había muchos que van y vienen para una tarea tan aparentemente rutinaria que no estoy seguro de que es la mejor respuesta/camino.

Si no se utiliza UIPinchGestureRecognizer en conjunto para este propósito es la respuesta, ¿cuál es la mejor manera de detectar la escala de pellizco en dos direcciones diferentes?

Respuesta

2

En mi C# Yo lo siguiente

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

puedo calcular la distancia entre los dos puntos cuando el emergente "comenzó" y mantenga ese valor en dos partes íntimas. Luego calculo una escala (fontScaling) basada en la primera distancia medida y la segunda (en "changed"). Utilizo mi propia vista (_previewView) para establecer como base (100%), pero puede usar View.Bounds.height en su lugar o el ancho en ese caso. en mi caso, siempre tengo una vista cuadrada, por lo que height == width en mi aplicación.

1

Básicamente hacer esto,

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

Esa función H, V, D volverá para ti .. horizontal, vertical, diagonal.

Se podría utilizar algo como esto ...

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