標籤:長按 failed port key 事件 ges call ssg sequence
由於以前沒有很細緻的研究過長按手勢,所以今天使用的時候發現長按手勢會調用兩次響應事件。
主要原因是長按手勢會分別在UIGestureRecognizerStateBegan和UIGestureRecognizerStateEnded狀態時調用響應函數
這時就需要在響應事件中增加手勢狀態的判斷,根據具體的應用情況在相應的狀態中執行操作。
typedefNS_ENUM(NSInteger, UIGestureRecognizerState) { UIGestureRecognizerStatePossible,// the recognizer has not yet recognized its gesture, but may be evaluating touch events. this is the default state UIGestureRecognizerStateBegan,// the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop UIGestureRecognizerStateChanged,// the recognizer has received touches recognized as a change to the gesture. the action method will be called at the next turn of the run loop UIGestureRecognizerStateEnded,// the recognizer has received touches recognized as the end of the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible UIGestureRecognizerStateCancelled,// the recognizer has received touches resulting in the cancellation of the gesture. the action method will be called at the next turn of the run loop. the recognizer will be reset to UIGestureRecognizerStatePossible UIGestureRecognizerStateFailed,// the recognizer has received a touch sequence that can not be recognized as the gesture. the action method will not be called and the recognizer will be reset to UIGestureRecognizerStatePossible// Discrete Gestures – gesture recognizers that recognize a discrete event but do not report changes (for example, a tap) do not transition through the Began and Changed states and can not fail or be cancelled UIGestureRecognizerStateRecognized =UIGestureRecognizerStateEnded// the recognizer has received touches recognized as the gesture. the action method will be called at the next turn of the run loop and the recognizer will be reset to UIGestureRecognizerStatePossible};
if (longPressGesture.state == UIGestureRecognizerStateBegan) { // do something}else if (longPressGesture.state == UIGestureRecognizerStateEnded){ // do something}
iOS長按手勢調用兩次解決方案