ios - UIButton 即使在整个区域也没有接触

标签 ios iphone uibutton touch-up-inside

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

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

enter image description here


O 隐藏 View 与按钮重叠,因此触摸事件未到达按钮。

最佳答案

为确保我理解您的问题,您的 UICollectionView 为黄色,UIbutton 为粉红色,对吗?

如果是这样,您似乎想拦截黄色按钮 super View 外的 touchUpInside 事件。你可以看看this answer处理某种问题。

即使您最终找到了问题的解决方案,为了澄清我的答案,如果您想与不在其父 View 框架内的 UIButton 进行交互,您可能需要实现方法 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 父 View (这里是 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;
}

(感谢诺姆!)

关于ios - UIButton 即使在整个区域也没有接触,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27685799/

相关文章:

iphone - 如何制作可在 iOS 5 至 7 上运行的 armv7 arm64 fat 二进制文件?

iphone - UIActivityIndi​​catorView 无响应

ios - 在 Swift 中使用 UIButton 动画

ios - UIImagePickerController、自定义 UIButton 和 AutoLayout

ios - Alamofire 概括请求

iOS,创建自定义弹出窗口样式 UIView

iphone - iOS:如何旋转 CGLayer?

iphone - resignFirstResponder 导致 EXC_BAD_ACCESS

ios - 创建协议(protocol)时找不到 'UIViewController' 的协议(protocol)声明

iphone - 用于更改表格排序列的 iOS 人机界面指南