0

.Cancelled.Failed州の違いは何ですか?UIGestureRecognizerState.CancelledとUIGestureRecognizerState.Failed

ジェスチャ認識プログラムの状態を.Cancelledまたは.Failedに設定すると、ジェスチャ認識プログラム自体にどのような影響がありますか?

ジェスチャ認識機能の状態は、いつ.Cancelled.Failedになりますか?

どの時点で「認識済み」とマークされたジェスチャ認識プログラムですか? .Beganに移行したら?

はいの場合、.Beganに設定されているジェスチャの状態もtouchesMovedにできますか?

たとえば、UIPinchGestureRecognizerによって認識されるピンチジェスチャはどの段階ですか?ピンチングは連続的なジェスチャーなので、私はtouchesMovedにしかないと思います。ここ

答えて

1

実際には、.Cancelledと.Failedの状態に違いはありません。両方ともジェスチャー認識機能がジェスチャーを処理できません。私はそれがちょうど命名規則だと思う。

ただし、違いは両方の状態がジェスチャ処理にどのように影響するかです。

ジェスチャ認識プログラムの以前の状態がどのようなものかによって異なります。

  1. touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent).Began.Possibleから移行ジェスチャ認識(タッチのUITouchPhase.Began相)は、.Failed又は.Cancelledに同じ方法においてよりも、(図に付着)キュー内の次のジェスチャ認識はあります場合ジェスチャーを処理する機会。アクションメッセージは送信されません。
  2. しかし.Failedまたは.Cancelledジェスチャー認識へtouchesMoved(touches: Set<UITouch>, withEvent event: UIEvent)方法に比べtouchesBegan(touches: Set<UITouch>, withEvent event: UIEvent)(タッチのUITouchPhase.Began相)に.Beganに.Possibleから移行したジェスチャ認識装置は、単に失敗します場合何も起こりません。しかし、アクションメッセージはとにかく送信されます。
  3. 8行目のコードをコメントアウトすると、ジェスチャ認識は失敗し、ビューに添付された次のジェスチャ認識プログラムはジェスチャを処理する機会を得ます。だからここ

ビューコントローラ:

class ViewController: UIViewController { 

    func panHandler(sender: UIPanGestureRecognizer) { 
     print("panHandler") 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     let customRecognizer = CustomGestureRecognizer(target: self, action: #selector(ViewController.customHandler(_:))) 
     view.addGestureRecognizer(customRecognizer) 

     let panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(ViewController.panHandler(_:))) 
     view.addGestureRecognizer(panRecognizer) 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func customHandler(c: CustomGestureRecognizer) { 
     print("customHandler") 
    } 
} 

、ここでカスタムジェスチャー認識:

import UIKit 
import UIKit.UIGestureRecognizerSubclass 

class CustomGestureRecognizer: UIGestureRecognizer { 

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) { 
     super.touchesBegan(touches, withEvent: event) 
     state = .Began 
     if touches.count == 1 { 
      //state = .Failed 
     } 
     print("CustomGestureRecognizer.touchesBegan") 
    } 

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent) { 
     super.touchesMoved(touches, withEvent: event) 

     if state == .Failed { 
      return 
     } 

     if touches.count == 1 { 
      state = .Failed //.Cancelled 
     } 

     state = .Changed 
     print("CustomGestureRecognizer.touchesMoved") 
    } 

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent) { 
     super.touchesEnded(touches, withEvent: event) 
     state = .Ended 
     print("CustomGestureRecognizer.touchesEnded") 
    } 
} 

だけで違いを確認するためにライン8、10と23にコードのコメントを外し/コメント。

1

は、ジェスチャ認識装置は、連続ジェスチャの解除を生じるタッチを受信した

.Cancelled

の違いです。実行ループの次のサイクルでアクションメッセージ(または複数のメッセージ)を送信し、その状態をUIGestureRecognizerStatePossibleにリセットします。

.Failed

ジェスチャ認識装置は、そのジェスチャとして認識することができないマルチタッチシーケンスを受信して​​います。アクションメッセージは送信されず、ジェスチャ認識機能はUIGestureRecognizerStatePossibleにリセットされます。

言い換えれば、連続的なジェスチャが中断されたときに.Cancelledが呼び出されます。 .Failedは、ある種のジェスチャーとしてジェスチャーが認識されない場合に呼び出されます。

関連する問題