簡體   English   中英

在iOS模擬器中檢測長按UIButton

[英]Detect long press on UIButton in iOS Simulator

我的自定義UITableViewCell有一個UIButton 我正在通過以下代碼在UITableViewCell上的該按鈕上處理一些控件事件。 這取自CellForRowAtIndexPath方法。

    cell.gestureButton.tag = indexPath.row ;

    [cell.gestureButton addTarget:self action:@selector(cellTapped:) forControlEvents:UIControlEventTouchUpInside];

    UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                          initWithTarget:self action:@selector(cellLongPressed:)];
    lpgr.minimumPressDuration = 2.0; //seconds
    lpgr.delegate = self ;
    [cell.gestureButton addGestureRecognizer:lpgr];

我在iOS 7模擬器上測試它。 我的問題是,對於執行UIControlEventTouchUpInside的第一個事件,我可以看到結果並正確調用了我的cellTapped方法。

但在我的按鈕上分配了UILongPressGestureRecognizer的第二種情況下,我無法在模擬器和cellLongPressed:看到結果cellLongPressed:從不調用方法。 據我所知,我的代碼還可以。 所以,我想知道,問題出在哪里? 我的代碼有什么問題,或者Simulator不支持此功能嗎? 在此先感謝您的幫助。

我打賭lpgr與另一個手勢識別器沖突。 您是否嘗試過實施UILongPressGestureRecognizer的委托方法? 您可能需要設置故障依賴性。 具體來說,你可能需要在gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:返回YES gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: .

確保您的cellLongPressed聲明如下。

- (void)cellLongPressed:(UIGestureRecognizer *)gestureRecognizer {
   NSLog(@"cellLongPressed in action");
}

或者如果聲明如下:

- (void)cellLongPressed {
   NSLog(@"cellLongPressed in action");
}

請將您的手勢初始化程序更改為:

UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc]
                                          initWithTarget:self action:@selector(cellLongPressed)];

請注意, cellLongPressed選擇器名稱末尾沒有“ ”。

暫無
暫無

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

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