1

我有UIBUtton如图UICollectionViewCell所示。我在 xib 中设置了 touchUpInside Action。我已将按钮的背景图像设置为粉红色,并将标签设置为黄色。UIButton位于标签顶部。现在的问题是,UIButton在粉色区域没有获得触摸事件时,它只会在橙色区域获得触摸事件。为什么会这样。

- (IBAction)checkButtonTap:(id)sender event:(id)event
{
    DLog(@"%s",__func__);
}

在此处输入图像描述


O 隐藏视图与按钮重叠,因为它是触摸事件未到达按钮。

4

3 回答 3

1

只是为了确保我理解你的问题,你有UICollectionView黄色和UIbutton粉红色,对吗?

如果是这样,您似乎想以黄色拦截按钮的超级视图之外的 touchUpInside 事件。你可以看看这个处理某种问题的答案。

即使您最终找到了问题的解决方案,为了澄清我的答案,如果您想与UIButton不在其父视图框架内的对象交互,您可能需要实现- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event父视图的方法(此处为UICollectionViewCell):

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
if (!self.clipsToBounds && !self.hidden && self.alpha > 0) {
    for (UIView *subview in self.subviews.reverseObjectEnumerator) {
        CGPoint subPoint = [subview convertPoint:point fromView:self];
        UIView *result = [subview hitTest:subPoint withEvent:event];
        if (result != nil) {
            return result;
        }
    }
}

return nil;
}

(感谢诺姆!)

于 2014-12-29T09:11:04.020 回答
0

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageTapped:)]; [按钮名称 addGestureRecognizer:tapGesture]; buttonname.userInteractionEnabled = YES;

  • (IBAction)imageTapped:(UITapGestureRecognizer *)gestureRecognizer { if(gestureRecognizer.state == UIGestureRecognizerStateRecognized) { [buttonname becomeFirstResponder]; } }
于 2014-12-29T09:50:34.367 回答
0

抱歉,由于触摸事件未到达按钮,因此存在与按钮重叠的隐藏视图。

于 2014-12-29T10:50:45.887 回答