繁体   English   中英

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

[英]iOS: UIButton Appearing, But Not Functioning On UIView

对此也有一些类似的问题,但是它们在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)))

}

更新:由于按钮位于可点击视图的顶部,因此请确保按钮显示在可点击视图的前面。 如果使用情节提要,则该按钮应列在可点击视图下方。

另外,在您的touchesBegan方法中,确保您不对按钮内的触摸做出响应:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    if touch?.view != button && touch?.view == tappableView {
        // Next picture
    }
}

如果仍然无法解决问题,请尝试以下touchesBegan实现:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first
    if touch?.view == button {
        // performSegue or call button's target function
    } else if touch?.view == tappableView {
        // Next picture
    }
}

您缺少按钮的目标:

moreInfoButton.addTarget(self, action: #selector(buttonTapped(_:)), forControlEvents: .TouchUpInside)

和一个处理水龙头的功能:

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

我认为UITapGestureRecognizer与按钮点击事件冲突。 您可以尝试使用override touchesBegan事件代替轻击手势。

禁用手势的cancelsTouchesInView属性。 默认情况下,我认为它设置为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
}()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM