2012-07-29 58 views
0

我正在設置一個LPGR,所以我想知道是否可以在每個LPGR中創建一個標籤。我需要做到這一點,所以我知道被按下的我所有的按鈕...UILongPressGestureReconizer標籤? - iPhone

UILongPressGestureRecognizer *longpressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressHandler:)]; 
longpressGesture.minimumPressDuration = 2; 
[longpressGesture setDelegate:self]; 
[pushButton addGestureRecognizer:longpressGesture]; 

而且我下面的方法...

- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer { 
NSLog(@"longPressHandler"); 
} 

我知道你無法通過選擇傳遞參數,所以我想知道是否可以爲LPGR分配標籤,或者如果在方法中我可以獲取使用LPGR的按鈕標籤?這是否有可能>?

編輯:

NSInteger *tag = [gestureRecognizer.view.tag]; 
NSLog(@"%@ longPressHandler",tag); 

回答

2

UIGestureRecognizer有一個屬性view這是手勢識別器連接到的視圖。

因此,在您的處理程序方法中,gestureRecognizer.view是LPGR所連接的按鈕,而gestureRecognizer.view.tag是該按鈕的標記。

新增: 示例代碼:

- (void)longPressHandler:(UILongPressGestureRecognizer *)gestureRecognizer { 
    NSLog(@"longPressHandler"); 
    NSInteger tag = gestureRecognizer.view.tag; 
    NSLog(@"%d longPressHandler",tag); 
} 
+0

現在我得到意想不到的標識...我只是編輯我的問題... – 2012-07-29 22:01:52

+2

有在你的代碼的幾個語法錯誤:'tag'是一個屬性,因此沒有方括號,「NSInteger」是標量類型而不是對象。我已將正確的代碼添加到我的答案中。 – 2012-07-29 22:08:04

0

您可以簡單地創建的UILongPressGestureRecognizer一個子類和tag屬性添加到它。您還可以使用associated objects將屬性添加到類別中。