簡體   English   中英

UIImageView無法檢測到自定義可滑動顯示的UITableViewCell中的長按手勢,該UITableViewCell是滾動視圖

[英]UIImageView can't detect long press gesture in a customized swippable UITableViewCell which is a scrollview

我正在使用可滑動的UITableViewCell子類,如下所示:

https://github.com/CEWendel/SWTableViewCell

在我的單元格中,有一個UIImageView 我在UIImageView添加了一個UILongPressGestureRecognizer 長按UIImageView時,我想制作一個“微動搖晃”效果。 但是每次我長按UIImageView ,都會選擇整個單元格。 因此,我研究了可滑動的UITableViewCell代碼。 我發現單元格本身還設置了一個UILongPressGestureRecognizer ,如下所示:

    self.longPressGestureRecognizer = [[SWLongPressGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewPressed:)];
    self.longPressGestureRecognizer.cancelsTouchesInView = NO;
    self.longPressGestureRecognizer.minimumPressDuration = kLongPressMinimumDuration;
    self.longPressGestureRecognizer.delegate = self;
    [self.cellScrollView addGestureRecognizer:self.longPressGestureRecognizer];

該代碼來自SWTableViewCell.m ,您可以從上面的鏈接中找到。

現在我想知道的是有辦法的鎖定UILongPressGestureRecognizer的細胞,並且觸發了動作UIImageViewUIImageView被長按?

非常感謝您的所有幫助。

終於我找到了原因。 我將其發布在這里,以防將來其他人遇到同樣的困惑。

在我使用的庫中,有幾行代碼來設置longPressGestureRecognizer

self.longPressGestureRecognizer = [[SWLongPressGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewPressed:)];
    self.longPressGestureRecognizer.cancelsTouchesInView = NO;
    self.longPressGestureRecognizer.minimumPressDuration = kLongPressMinimumDuration;
    self.longPressGestureRecognizer.delegate = self;
    [self.cellScrollView addGestureRecognizer:self.longPressGestureRecognizer];

只有一行: self.longPressGestureRecognizer.minimumPressDuration = kLongPressMinimumDuration; #define kLongPressMinimumDuration 0.16f 所以我的問題的原因是持續時間0.16f太小。 它會比我的長按早觸發。 如果我更改持續時間,例如將其更改為0.5s,一切都很好。

您可以嘗試下一步(認為不是最佳解決方案,但是可以嘗試)

為tableView創建自定義單元格,將UIView添加為內容所有者。 比在cell.m文件中執行下一步

  1. 注冊您的手勢

     - (void)initGestrudeRecognizer{ UILongPressGestureRecognizer *longGestrudeRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)]; [self addGestureRecognizer:longGestrudeRecognizer]; } 

2.implement longPress action.In這種方法也都需要動畫(你可以很容易地修改它-下面只是為例)

- (IBAction)longPress:(id)sender{
    NSLog(@"long press gestrude recognized");
    [UIView animateWithDuration:0.05 animations:^{
        self.uiview.frame = CGRectMake(-20, 0, self.uiview.frame.size.width, self.uiview.frame.size.height);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.05 animations:^{
                self.uiview.frame = CGRectMake(20, 0, self.uiview.frame.size.width, self.uiview.frame.size.height);
            } completion:^(BOOL finished){
               self.uiview.frame = CGRectMake(0, 0, self.uiview.frame.size.width, self.uiview.frame.size.height);
            }];
        }];
    }
  1. 如果您需要識別多個手勢,則需要實現<UIGestureRecognizerDelegate>協議並添加簡單返回YES的方法

     - (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{ return YES; } 

如果您不希望您的單元格變為可選擇狀態-只需選擇單元格的選擇類型為NONE

在此處輸入圖片說明

結果,我得到了類似瘦的東西(在longPress上)

在此處輸入圖片說明

關於swipeGestrude用法的好文章也可以在這里找到。 對我來說非常有幫助。

暫無
暫無

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

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