実際には、.Cancelledと.Failedの状態に違いはありません。両方ともジェスチャー認識機能がジェスチャーを処理できません。私はそれがちょうど命名規則だと思う。
ただし、違いは両方の状態がジェスチャ処理にどのように影響するかです。
ジェスチャ認識プログラムの以前の状態がどのようなものかによって異なります。
touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent)
に.Began
に.Possible
から移行ジェスチャ認識(タッチのUITouchPhase.Began
相)は、.Failed
又は.Cancelled
に同じ方法においてよりも、(図に付着)キュー内の次のジェスチャ認識はあります場合ジェスチャーを処理する機会。アクションメッセージは送信されません。
- しかし
.Failed
または.Cancelled
ジェスチャー認識へtouchesMoved(touches: Set<UITouch>, withEvent event: UIEvent)
方法に比べtouchesBegan(touches: Set<UITouch>, withEvent event: UIEvent)
(タッチのUITouchPhase.Began
相)に.Beganに.Possibleから移行したジェスチャ認識装置は、単に失敗します場合何も起こりません。しかし、アクションメッセージはとにかく送信されます。
- 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にコードのコメントを外し/コメント。