iOS:UIView上出现UIButton,但不起作用

iOS:UIView上出现UIButton,但不起作用,ios,swift,Ios,Swift,在这方面也有一些类似的问题,但它们在Obj-C中,没有具体回答我的问题 我正在构建一个类似Tinder的约会应用程序,卡上有一个按钮,允许当前用户在显示的卡上查看有关该用户的更多信息。我以编程方式编写了这个按钮(moreInfoButton),并将其显示在卡(UIView)上,没有任何问题。但当我点击按钮时,它就不起作用了。我试过isEnabled和isUserInteractionEnabled,但都不起作用。这是我的密码: import UIKit import SDWebImage cl

在这方面也有一些类似的问题,但它们在Obj-C中,没有具体回答我的问题

我正在构建一个类似Tinder的约会应用程序,卡上有一个按钮,允许当前用户在显示的卡上查看有关该用户的更多信息。我以编程方式编写了这个按钮(moreInfoButton),并将其显示在卡(UIView)上,没有任何问题。但当我点击按钮时,它就不起作用了。我试过isEnabled和isUserInteractionEnabled,但都不起作用。这是我的密码:

import UIKit
import SDWebImage

class CardView: UIView {

var imageView = UIImageView(image: #imageLiteral(resourceName: "lady5c"))

var informationLabel = UILabel()

var images: [String]?

var userId: String?

var stackView: UIStackView?

let moreInfoButton: UIButton = {
    let button = UIButton(type: .system)
    button.setImage(#imageLiteral(resourceName: "info_icon").withRenderingMode(.alwaysOriginal), for: .normal)
    return button
}()

override init(frame: CGRect) {
    super.init(frame: frame)
    layer.cornerRadius = 15
    clipsToBounds = true

    imageView.contentMode = .scaleAspectFill
    addSubview(imageView)
    imageView.fillSuperview()

    addSubview(informationLabel)
    informationLabel.numberOfLines = 0
    informationLabel.anchor(top: nil, leading: leadingAnchor, bottom: bottomAnchor, trailing: trailingAnchor, padding: .init(top: 0, left: 16, bottom: 16, right: 16))
    informationLabel.text = ""
    informationLabel.textColor = .white
    informationLabel.layer.zPosition = 1

    addSubview(moreInfoButton)
    moreInfoButton.anchor(top: nil, leading: nil, bottom: bottomAnchor, trailing: trailingAnchor, padding: .init(top: 0, left: 0, bottom: 20, right: 20), size: .init(width: 50, height: 50))
    moreInfoButton.layer.zPosition = 1
    moreInfoButton.isUserInteractionEnabled = true

    // let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan))
    // addGestureRecognizer(panGesture)
    addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleTap)))

}

更新:由于按钮位于可点击视图的顶部,请确保按钮显示在可点击视图的前面。如果您使用的是故事板,则按钮应列在可点击视图下方

此外,在ToucheSBegind方法中,请确保您对按钮内的触摸没有反应:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    if touch?.view != button && touch?.view == tappableView {
        // Next picture
    }
}
moreInfoButton.addTarget(self, action: #selector(buttonTapped(_:)), forControlEvents: .TouchUpInside)
和一个处理抽头的功能:

@objc func buttonTapped(sender: UIButton!) {
    // action
}

我认为UITapgestureRecognitor与按钮点击事件冲突。
您可以尝试使用override TouchesBegind事件而不是点击手势。

禁用手势的
取消触摸视图属性。默认情况下,我认为它设置为
true
。这意味着手势会消耗触摸事件,而不会传递到按钮

let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTap))
tapGesture.cancelsTouchesInView = false
addGestureRecognizer(tapGesture)

如果以编程方式添加按钮,我认为应该使用addTarget,而不是使用整个uiview手势。这使得按钮的功能与视图手势没有任何关系

lazy var button: UIButton = {
    let temp = UIButton(type: .system)
    temp.isUserInteractionEnabled = true
    temp.addTarget(self, action: someFunction, for: UIControl.Event.touchUpInside)
    return temp
}()

如果您希望整个视图都可以单击,您应该在
init
中添加
self.isUserInteractionEnabled=true
向按钮添加操作谢谢,但这似乎仍然不起作用。点击此按钮时,应打开包含用户信息(来自卡)的单独视图控制器。我忘了提到显示用户照片(按钮位于其上)的UIView是可点击的,因此当前用户可以循环浏览用户的照片(就像Tinder一样)。因此,当我点击按钮时,它会将其识别为视图的“前进”部分,并循环到下一张照片,而不是执行按钮的代码。有什么建议吗?有没有可能分享一张图纸或类似的东西,我们可以根据您的具体需要制作图像?谢谢,但这似乎仍然不起作用。点击此按钮时,应打开包含用户信息(来自卡)的单独视图控制器。我忘了提到显示用户照片(按钮位于其上)的UIView是可点击的,因此当前用户可以循环浏览用户的照片(就像Tinder一样)。因此,当我点击按钮时,它会将其识别为视图的“前进”部分,并循环到下一张照片,而不是执行按钮的代码。有什么建议吗?
lazy var button: UIButton = {
    let temp = UIButton(type: .system)
    temp.isUserInteractionEnabled = true
    temp.addTarget(self, action: someFunction, for: UIControl.Event.touchUpInside)
    return temp
}()