簡體   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