簡體   English   中英

如何在iOS中確定“ TapEnded”

[英]How to determine “TapEnded” in iOS

我想更改UIView點擊的背景顏色,我知道如何在iOS的UIView上捕獲觸摸事件和手勢。

我可以在touchesBegan觸發時更改顏色,然后在touchesEndedtouchesCancelled觸發時將其更改為原始顏色。

這可以正常工作,直到用戶真正快速地單擊UIView為止,這可以由UITapGestureRecognizer確定並更改背景顏色,但是UITapGestureRecognizer我不知道如何更改顏色!

我不想使用某種“計時器”或類似的方法。 有什么建議嗎?

您可以通過檢查水龍頭的狀態來檢測它是否結束:

目標C

if (recognizer.state == UIGestureRecognizerStateEnded) {
    // change the color here
}

迅速

if recognizer.state == .Ended {
    // change the color here
}
  1. 從您的問題來看,您似乎已經通過以下UIView代表成功完成了此任務。

    • 開始:
    • touchesEnded:
    • 取消:
  2. 第二個選項是使用您已經提到的UITapGestureRecognizer。 請嘗試在手勢識別器方法中使用以下代碼。

向您的視圖添加手勢:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTap:)];

[yourView addGestureRecognizer:tapGesture ];

您的手勢識別器方法

-(void) viewTap:(UITapGestureRecognizer *) gestureRecognizer
{
   if(gestureRecognizer.state == UIGestureRecognizerStateEnded)
   {
      //All fingers are lifted.
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM