iOS長按手勢調用兩次解決方案

來源:互聯網
上載者:User

標籤:長按   failed   port   key   事件   ges   call   ssg   sequence   

由於以前沒有很細緻的研究過長按手勢,所以今天使用的時候發現長按手勢會調用兩次響應事件。

主要原因是長按手勢會分別在UIGestureRecognizerStateBeganUIGestureRecognizerStateEnded狀態時調用響應函數

這時就需要在響應事件中增加手勢狀態的判斷,根據具體的應用情況在相應的狀態中執行操作。

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長按手勢調用兩次解決方案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.