隐形按钮不响应被点击

时间:2019-03-11 20:28:08

标签: ios swift uitableview uibutton

我试图在图像视图上放置一个不可见的按钮,因为图像视图太小而无法响应点击手势识别器。我不能只是将图像放大,因为它看起来很糟糕。这是用于增加数量的imageview的代码:

private func configureIncreaseQuantityImageView() {
    increaseQuantityImageView.contentMode = .scaleAspectFill
    increaseQuantityImageView.image = UIImage(named: "arr_up")
    //increaseQuantityImageView.isUserInteractionEnabled = true
    //increaseQuantityImageView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleIncreaseQuantityTapped)))
    contentView.addSubview(increaseQuantityImageView)
    increaseQuantityImageView.translatesAutoresizingMaskIntoConstraints = false
    increaseQuantityImageView.centerYAnchor.constraint(equalTo: verticalDivider.centerYAnchor).isActive = true
    increaseQuantityImageView.leftAnchor.constraint(equalTo: verticalDivider.rightAnchor, constant: 12).isActive = true
    increaseQuantityImageView.widthAnchor.constraint(equalToConstant: 12).isActive = true
    increaseQuantityImageView.heightAnchor.constraint(equalToConstant: 8).isActive = true
}

以下是不响应触摸的不可见按钮的代码:

private func configureInvisibleIncreaseQuantityButton() {
    invisibleIncreaseQuantityButton.backgroundColor = .clear
    invisibleIncreaseQuantityButton.addTarget(self, action: #selector(handleIncreaseQuantityTapped), for: .touchUpInside)
    contentView.addSubview(invisibleIncreaseQuantityButton)
    invisibleIncreaseQuantityButton.translatesAutoresizingMaskIntoConstraints = false
    invisibleIncreaseQuantityButton.centerYAnchor.constraint(equalTo: verticalDivider.centerYAnchor).isActive = true
    invisibleIncreaseQuantityButton.leftAnchor.constraint(equalTo: verticalDivider.rightAnchor).isActive = true
    invisibleIncreaseQuantityButton.widthAnchor.constraint(equalToConstant: 35).isActive = true
    invisibleIncreaseQuantityButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
}

这是触摸隐形按钮时不会调用的功能。我对减少数量按钮做了非常类似的操作,因此无需显示该代码。

@objc private func handleIncreaseQuantityTapped() {
    self.quantity += 1
    quantityValueLabel.text = "\(self.quantity)"
    self.menuItem?.quantity = self.quantity
    guard let item = self.menuItem else { return }
    delegate?.updateMenuItem(item)
}

2 个答案:

答案 0 :(得分:0)

通常来说,您不需要按钮即可执行此操作。您只需使用对象上分层的UIView即可完成相同的操作。

但是,以编程方式执行此操作时,需要考虑一些因素,主要是因为您实际上是在代码中而不是在情节提要中处理的,因此您实际上看不到UI对象的位置。

我的建议: 1)您可能无法在视图上启用用户交互 gainQuantityImageView.isUserInteractionEnabled = true

2)您尝试与之交互的视图被埋在子视图层次结构的另一个视图的后面

.addSubview()函数可以将要触摸的对象放置在应该覆盖的对象下。

这是一个漂亮的小片段,我经常使用它来快速找到子视图:

此函数以数组的形式返回提供的视图中包含的所有子视图。

private func getSubviewsOf<T : UIView>(view:UIView) -> [T] {
        var subviews = [T]()

        for subview in view.subviews {
            subviews += getSubviewsOf(view: subview) as [T]

            if let subview = subview as? T {
                subviews.append(subview)
            }
        }
        return subviews
    }

您可以像这样使用它:

// First, let's get an ID on the object you are trying to find
`invisibleDecreaseQuantityButton.restorationIdentifier = "targetView"`

// Then, also set a restoration ID on the view that this targetView is supposed to be on top of
increaseQuantityImageView.restorationIdentifier = "coveredView"

// Now, get all of the subviews of the main view
let subviews = self.getSubViewsOf(view: self.view)

var targetViewIndex = 0
var coveredViewIndex = 0
// Figure out which view is the correct one
for (index, view) in subviews.enumerated() {

    // When we find the target view
    if view.restorationIdentifier == "targetView" {
        // Let's print it so we know where it resides
        print("targetView's index: \(index)")
        targetViewIndex = index
    }

    if view.restorationIdentifier == "coveredView"{
        // print this one too
        print("coveredView's index: \(index)")
        coveredViewIndex = index
    }
}

这部分实际上只是为了帮助您以编程方式进行调试。如果您想在您的应用程序中使用更直观的调试方法,强烈建议使用FLEX。使用此框架,您可以直观地点击每个元素并获取其变量名,并直观地看到一个图形,其中包含整个视图层次结构在视图控制器中的位置。

请记住,在视图层次结构中,子视图数组中索引为 lower 的视图意味着它更靠近视图顶部。所以:

if targetIndex > coveredViewIndex {
    // push the coveredView behind the targetView
    self.view.insertSubview(targetView, at: coveredViewIndex)
}

有关Apple Documentation中的层次结构的更多信息

Lmk,如果我可以帮助您进一步调试。这是我刚刚制作的FLEX的小屏幕截图,它是一个很好的工具:

答案 1 :(得分:0)

雨燕4.2

您可以在const data = [ { BaseId: 1, BaseDesc: "18 BHC Baseline", StressId: 5, StressDesc: "Desc 1", bopId: 8, bopDesc: "BOP Desc 1" }, { BaseId: 1, BaseDesc: "Baseline 2", StressId: 2, StressDesc: "Desc 12", bopId: 8, bopDesc: "BOP Desc 2" }, { BaseId: 2, BaseDesc: "Baseline 3", StressId: 7, StressDesc: "Desc 3", bopId: 10, bopDesc: "BOP Desc 3" } ]; const pickSpecifiedProperties = (startingPropString, arrayOfObjects) => arrayOfObjects.map(obj => { const targetKeys = Object.keys(obj).filter( keyName => keyName.toLowerCase().indexOf(startingPropString.toLowerCase()) === 0 ); return targetKeys.reduce((data, keyName) => { const newProperty = { [keyName]: obj[keyName] }; return { ...data, ...newProperty }; }, {}); }); const baseData = pickSpecifiedProperties("Base", data); const stressData = pickSpecifiedProperties("Stress", data); const bopData = pickSpecifiedProperties("bop", data); console.log({ baseData, stressData, bopData }); 的超级视图上添加UITapGestureRecognizer,并设置一个包含imageView的位置的范围。


例如:

imageView

祝你好运,我的朋友!