想要阻止位于UICollectionViewCell上的UIButton的UIEvent

问题描述 投票:0回答:2

我正在创建如下的UICollectionView。

enter image description here

我想要做的关于UICollectionView的是,

  1. 每个UICollectionViewCell都有UIButton([cell.contentView addSubview:button];)
  2. UIButton需要处理UIControlEventTouchDown和UIControlEventTouchUpInside
  3. 通过长按UICollectionView Cell开始interactiveMovement
  4. 不想通过长按UIButton来开始interactiveMovement。 UIButton只处理在步骤2中分配的UIControlEvent。
  5. 当点击UIBollectionViewCell而非UIButton的区域时,想要处理(didSelectItemAtIndexPath :)。

我的问题是,即使长时间点击UIButton,handleLongPress for UICollectionView也会响应并启动interactiveMovement。

我想只在轻触UIBollection以外的UICollectionView区域时启动interactiveMovement。

当长按UIButton时,我只想处理分配给UIButton的UIConrolEvent,并且不希望UICollectionView响应。

如果你有一些解决方案我会很感激。

我的代码如下,谢谢。

MyCollectionView:UICollectionView

- (id)init
{
    self = [super initWithFrame:CGRectZero collectionViewLayout:[[UICollectionViewFlowLayout alloc] init]];

    if (self) {
        self.delegate = self;
        self.dataSource = self;
        --- snip ---
        [self addLongPressGesture];
        --- snip ---        
    }
    return self;
}

- (void)addLongPressGesture
{
    UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self
                                                                                     action:@selector(handleLongPress:)];
    [self addGestureRecognizer:gesture];
}

- (void)handleLongPress:(UILongPressGestureRecognizer *)sender
{
    CGPoint point = [sender locationInView:sender.view];
    NSIndexPath *indexPath = [self indexPathForItemAtPoint:point];

    switch (sender.state) {
        case UIGestureRecognizerStateBegan:
            [self beginInteractiveMovementForItemAtIndexPath:indexPath];
            break;

        case UIGestureRecognizerStateChanged:
            [self updateInteractiveMovementTargetPosition:point];
            break;

        case UIGestureRecognizerStateEnded:
            [self endInteractiveMovement];
            break;

        default:
            [self cancelInteractiveMovement];
            break;
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    // something to do
}

MyUICollectionViewCell:UICollectionViewCell

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initButton];
    }
    return self;
}

- (void)initButton
{
    _button = [[UIButton alloc] init];

    [_button addTarget:self action:@selector(touchUpButton:) forControlEvents:UIControlEventTouchUpInside];
    [_button addTarget:self action:@selector(touchDownButton:) forControlEvents:UIControlEventTouchDown];
    --- snip ---

    [self addSubview:_button];
}

- (void)touchUpButton:(UIButton *)button
{
    // something to do
}
- (void)touchDownButton:(UIButton *)button
{
    // something to do
}
ios objective-c uibutton uicollectionview uicollectionviewcell
2个回答
0
投票

在UIButton下面添加一个透明的UIView并向其添加长按不到整个集合视图,因为UIButton被认为是单元格的子控件

要么

看到长按点x,y,如果它与方法的按钮框架返回相交


0
投票

我已经解决了我的问题。我不是一个好的解决方案,但它正如我所期望的那样工作。

我在UIButton下添加了ButtonBaseView(:UIView),并在ButtonBaseView上添加了LongPressGesture。

LongPressGesture没有采取任何行动。该角色是阻止LongPressGesture不将事件传递给UICollectionView。同时,设置cancelsTouchesInView=NO来处理TouchUpInsideTouchDragExit

enter image description here

我的代码如下,

- (void)addDummyLongTapGesture
{
    UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:nil];
    gesture.cancelsTouchesInView = NO;
    [buttonBaseView addGestureRecognizer:gesture];
}
© www.soinside.com 2019 - 2024. All rights reserved.