亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

按下時會兩次調用UILongPressGestureRecognizer

按下時會兩次調用UILongPressGestureRecognizer

iOS
PIPIONE 2019-12-06 06:06:44
我正在檢測用戶是否已按下2秒鐘:UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]                                             initWithTarget:self                                              action:@selector(handleLongPress:)];        longPress.minimumPressDuration = 2.0;        [self addGestureRecognizer:longPress];        [longPress release];這是我處理長按的方式:-(void)handleLongPress:(UILongPressGestureRecognizer*)recognizer{    NSLog(@"double oo");}當我按下2秒鐘以上時,文本“ double oo”被打印兩次。為什么是這樣?我該如何解決?
查看完整描述

3 回答

?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

UILongPressGestureRecognizer是連續事件識別器。您必須查看狀態以查看這是事件的開始,中間還是結束,并采取相應的措施。即,您可以在開始之后放棄所有事件,或者僅根據需要查看運動。從 類參考:


長按手勢是連續的。當在指定時間段內(minimumPressDuration)按下了允許的手指數(numberOfTouchesRequired),并且觸摸沒有移動超出允許的移動范圍(allowableMovement)時,手勢即開始(UIGestureRecognizerStateBegan)。每當手指移動時,手勢識別器都會轉換為“更改”狀態,并且在任何手指抬起時手勢識別器都會終止(UIGestureRecognizerStateEnded)。


現在您可以像這樣跟蹤狀態


-  (void)handleLongPress:(UILongPressGestureRecognizer*)sender { 

    if (sender.state == UIGestureRecognizerStateEnded) {

      NSLog(@"UIGestureRecognizerStateEnded");

    //Do Whatever You want on End of Gesture

     }

    else if (sender.state == UIGestureRecognizerStateBegan){

       NSLog(@"UIGestureRecognizerStateBegan.");

   //Do Whatever You want on Began of Gesture

     }

  }



查看完整回答
反對 回復 2019-12-07
?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

要檢查UILongPressGestureRecognizer的狀態,只需在選擇器方法上添加if語句:


- (void)handleLongPress:(UILongPressGestureRecognizer *)sender {    

    if (sender.state == UIGestureRecognizerStateEnded) {

        NSLog(@"Long press Ended");

    } else if (sender.state == UIGestureRecognizerStateBegan) {

        NSLog(@"Long press detected.");

    }

}



查看完整回答
反對 回復 2019-12-07
?
撒科打諢

TA貢獻1934條經驗 獲得超2個贊

您需要檢查正確的狀態,因為每種狀態都有不同的行為。您最有可能需要UIGestureRecognizerStateBegan帶有狀態UILongPressGestureRecognizer。


UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]

                                             initWithTarget:self 

                                             action:@selector(handleLongPress:)];

longPress.minimumPressDuration = 1.0;

[myView addGestureRecognizer:longPress];

[longPress release];

...


- (void)handleLongPress:(UILongPressGestureRecognizer *)gesture {

    if(UIGestureRecognizerStateBegan == gesture.state) {

        // Called on start of gesture, do work here

    }


    if(UIGestureRecognizerStateChanged == gesture.state) {

        // Do repeated work here (repeats continuously) while finger is down

    }


    if(UIGestureRecognizerStateEnded == gesture.state) {

        // Do end work here when finger is lifted

    }

}



查看完整回答
反對 回復 2019-12-07
  • 3 回答
  • 0 關注
  • 560 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號