首页 文章

CollectionView单元格上的按钮仅在长按后接收轻击

提问于
浏览
0

我以编程方式将UIButton添加到自定义UICollectionViewCell . 只有在我首先长按单元格时,才会检测到按钮上的点击 . 然后按钮按预期工作 . 我必须分别对每个单元格进行相同的长按,以便让各自的按钮识别水龙头 .

请注意,单元格上的单击(即didSelectItemAtIndexPath)按预期工作 . 长按一个细胞也是如此 . 值得注意的是,UILongPressGestureRecognizer是添加到collectionView的唯一手势 .

UICollectionViewCell类

- (UIButton *)stackInfoButton
{
    if(_stackInfoButton == nil)
    {
        self.stackInfoButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
        [self.stackInfoButton setShowsTouchWhenHighlighted:YES];
    }
    return _stackInfoButton;
}

- (void)layoutSubviews
{
  // Other stuff here. Removed for readability
    self.stackInfoFrame = CGRectMake(4.0f, self.bounds.size.height - self.stackInfoButton.bounds.size.height - 4, self.stackInfoButton.bounds.size.width, self.stackInfoButton.bounds.size.height);
}

- (void)drawRect:(CGRect)rect
{
  // Other stuff here. Removed for readability
  self.stackInfoButton.frame = self.stackInfoFrame;
  [[self contentView] addSubview:self.stackInfoButton];
}

UICollectionViewController类

- (void)viewDidLoad
{
  [super viewDidLoad];
  // Other stuff here. removed for readability

  self.longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
  self.longPressGesture.delegate = self;
  [self.collectionView addGestureRecognizer:self.longPressGesture];
  self.longPressGesture.cancelsTouchesInView = NO;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
  DVCardViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kDVCollectionViewCellReuseIdentifier forIndexPath:indexPath];
  cell.indexPath = indexPath;
  // Other stuff here. Removed for readability
  [cell.stackInfoButton addTarget:self action:@selector(stackInfoButtonTapped:) forControlEvents:UIControlEventTouchUpInside];     
  return cell;
}

- (void)stackInfoButtonTapped:(id)sender
 {
   NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
 }

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    NSLog (@"Touch View : %@", touch.view);
// tried both.
//    DVCardViewCell *cell;
//    if (touch.view == cell.stackInfoButton)
        if ( [touch.view isKindOfClass:[UIButton class]] )
{
    NSLog(@"Gesture on button, returns no");
    return NO;
}
NSLog(@"Gesture returns outside, yes");
return YES;
}

建议表示赞赏 . 我在SO上经历了很多UICollectionView线程 . 然而,找到一个解决我的问题 . 谢谢 .

2 回答

  • 0

    你说你已经在集合视图中添加了一个手势,所以你需要教这个手势,不要偷走它不应该涉及的触摸 . 添加您的控制器作为手势的代表并实现 gestureRecognizer:shouldReceiveTouch: . 在该方法中,检查触摸点击的视图,如果是按钮,则返回 NO 以防止手势涉及 .

  • 0

    试试这个

    self.LongPressGesture.cancelsTouchesInView = NO;
    

    Sorry was mistaken. never mind.
    这看起来很奇怪: - (UIButton *)stackInfoButton
    {
    if(_stackInfoButton == nil)
    {
    self.stackInfoButton = [UIButton buttonWithType:UIButtonTypeInfoDark]; //理论上你的应用程序现在应该崩溃了
    [self.stackInfoButton setShowsTouchWhenHighlighted:YES];
    }
    return _stackInfoButton;
    }
    看看self.stackInfoButton = [UIButton buttonWithType:UIButtonTypeInfoDark];它必须是_stackInfoButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
    [_stackInfoButton setShowsTouchWhenHighlighted:YES];
    也许有帮助 .

相关问题