2012-09-10 13 views
10

Quando si utilizza UIPinchGestureRecognizer qual è il modo migliore per rilevare/leggere la scala pizzica in direzione orizzontale e verticale singolarmente? Ho visto questo postScala UIPinchGestureRecognizer in direzione orizzontale e verticale separatamente

UIPinchGestureRecognizer Scale view in different x and y directions

ma ho notato che c'erano così tanti andando avanti e indietro per un compito così apparentemente di routine che non sono sicuro che è la migliore risposta/modo.

Se non si utilizza UIPinchGestureRecognizer per questo scopo è la risposta, qual è il modo migliore per rilevare la scala di pizzico in due direzioni diverse?

risposta

2

Nel mio C# io il seguente

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

a calcolare la distanza tra i due punti quando pinch "iniziato" e sostengono che il valore in due privati. Quindi calcolo un ridimensionamento (fontScaling) in base alla prima distanza misurata e al secondo (in "modificato"). Uso la mia vista personale (_previewView) per impostare come base (100%), ma è possibile utilizzare View.Bounds.height o la larghezza per quella materia. nel mio caso, ho sempre una vista quadrata, quindi altezza == larghezza nella mia app.

1

Fondamentalmente fare questo,

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

Tale funzione ritorna H, V, D per te .. orizzontale, verticale, diagonale.

Si potrebbe utilizzare qualcosa di simile ...

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