Swift - 添加的 UIButton 大于要添加到的 View ?需要更大的选择区域?

标签 swift uibutton uitapgesturerecognizer

好吧,所以我有这些 ImageView ,我使用大量代码四处移动(这是我不想重做我的整个脚本的原因移动 ImageView 是其 subview 的 UIButton - 它会搞砸一切)。

这里是小圆圈:

bubble1.frame = CGRectMake(0, 0, screenSize.width*(21/330), screenSize.width*(21/330))
        bubble1.center = CGPointMake(lineView.bounds.width * 0.2, lineView.bounds.height * 0.98) //change when make smaller
        bubble1.layer.cornerRadius = bubble1.bounds.width * 0.5
        bubble1.backgroundColor = colorWithHexString(chosenColor)
        bubble1.userInteractionEnabled = false
        //bubble1.alpha = 0.0

        let bubbleBtn1 = UITapGestureRecognizer(target:self, action:#selector(HistoryViewController.bubble1Tap(_:)))
        bubble1.addGestureRecognizer(bubbleBtn1)

如您所见,它们附有敲击手势识别器,因此当它们被触摸时会触发某些东西。

这很好用,除了因为圆圈太小(它们需要那么小)很难触发触摸 - 我四处寻找扩大识别器区域的方法但无济于事。

然后我尝试添加一个更大的 70x70 按钮作为我的 ImageView 的 subview ,我知道添加了更大的按钮是因为我看到它是我圆圈顶部的一个更大的正方形,除了出于某种原因它只触发了它底层 bubble1 所在的操作,使按钮无用。

如何让较大的按钮在较小的 ImageView 不触发的情况下触发其操作?我看过类似 How to increase selection area of UIButton? 的问题但这更适合我的按钮在其 super View 区域之外不起作用的问题。

我该如何解决这个问题?

最佳答案

我会建议采用相反的方向并创建一个包含按钮和 bubble1 作为 subview 的 View 。这就是 UIView 层次结构的工作方式,任何与此相冲突的解决方案都是脆弱的,并且违背了 API 的设计理念:)

要处理任何居中问题,您只需将气泡的中心设置为它的 superView 的边界中心,理想情况下可以使用布局约束来处理

关于Swift - 添加的 UIButton 大于要添加到的 View ?需要更大的选择区域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38706380/

相关文章:

ios - Swift - 使用 IB 的文本字段的自定义 inputView?

swift - 如何根据分数添加 Sprite ?

ios - 如果滚动,swift collectionview cell didSelectItemAtIndexPath 会显示错误的单元格

ios - UIButton以编程方式获取默认突出显示样式

ios - 点击手势对我不起作用

ios - 点击时 UIImageView 图像更改延迟

ios - 来自本地通知附件的图像未显示在 UNNotificationContentExtension 中

ios - 设置UIButton alpha在iOS 7上无法正常工作。

ios - 如何使标签的文本在图像之上看起来不错?

ios - Swift:以编程方式添加 UITapGestureRecognizer 不起作用