gpt4 book ai didi

ios - 单击 UIButton 时没有任何反应

转载 作者:搜寻专家 更新时间:2023-11-01 06:30:39 24 4
gpt4 key购买 nike

我创建了一个 UIView 的子类,其中包含一个 SwitchButton,但是即使我添加了一个目标,当我点击按钮时仍然没有反应/打印.怎么会这样?

View Controller

self.tradeView = TradeView()
self.tradeView.translatesAutoresizingMaskIntoConstraints = false
self.tradeView.backgroundColor = Color.theme.value
self.view.addSubview(self.tradeView)

子类

class TradeView: UIView {

var waveView: UIImageView!

var bottomView: UIView!
var topView: UIView!
var centerView: UIView!

var switchButton: UIButton!






override init(frame: CGRect) {
super.init(frame: frame)
setUp()


}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUp()
}

func setUp() {
waveView = UIImageView()
waveView.translatesAutoresizingMaskIntoConstraints = false
waveView.isUserInteractionEnabled = false
waveView.image = UIImage(named: "wave")
self.addSubview(waveView)

topView = UIImageView()
topView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(topView)

centerView = UIImageView()
centerView.isUserInteractionEnabled = false
centerView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(centerView)

bottomView = UIImageView()
bottomView.isUserInteractionEnabled = false
bottomView.translatesAutoresizingMaskIntoConstraints = false
self.addSubview(bottomView)

waveView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 1).isActive = true
waveView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
waveView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
waveView.heightAnchor.constraint(equalToConstant: 32).isActive = true
waveView.topAnchor.constraint(equalTo: self.bottomView.bottomAnchor).isActive = true

topView.bottomAnchor.constraint(equalTo: self.centerView.topAnchor).isActive = true
topView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
topView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
topView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
topView.heightAnchor.constraint(equalTo: bottomView.heightAnchor).isActive = true

centerView.bottomAnchor.constraint(equalTo: self.bottomView.topAnchor).isActive = true
centerView.topAnchor.constraint(equalTo: self.topView.bottomAnchor).isActive = true
centerView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
centerView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
centerView.heightAnchor.constraint(equalToConstant: 46).isActive = true

bottomView.bottomAnchor.constraint(equalTo: self.waveView.topAnchor).isActive = true
bottomView.topAnchor.constraint(equalTo: self.centerView.bottomAnchor).isActive = true
bottomView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
bottomView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
bottomView.heightAnchor.constraint(equalTo: self.topView.heightAnchor).isActive = true

setUpCenterView()

}


func setUpCenterView() {
let borderView = UIView()
borderView.translatesAutoresizingMaskIntoConstraints = false
borderView.backgroundColor = Color.lightButton.withAlpha(0.3)
self.centerView.addSubview(borderView)

self.switchButton = UIButton(type: .custom)
switchButton.translatesAutoresizingMaskIntoConstraints = false
self.switchButton.setImage(UIImage(named: "switch"), for: .normal)
self.switchButton.addTarget(self, action: #selector(switchExchange), for: UIControlEvents.touchUpInside)
self.centerView.addSubview(switchButton)

borderView.centerYAnchor.constraint(equalTo: self.centerView.centerYAnchor).isActive = true
borderView.leftAnchor.constraint(equalTo: self.centerView.leftAnchor, constant: 24).isActive = true
borderView.rightAnchor.constraint(equalTo: self.switchButton.leftAnchor, constant: -24).isActive = true
borderView.heightAnchor.constraint(equalToConstant: 1).isActive = true

self.switchButton.centerYAnchor.constraint(equalTo: self.centerView.centerYAnchor).isActive = true
self.switchButton.leftAnchor.constraint(equalTo: borderView.rightAnchor, constant: 24).isActive = true
self.switchButton.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -24).isActive = true
self.switchButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
self.switchButton.widthAnchor.constraint(equalToConstant: 40).isActive = true


}

@objc func switchExchange() {
print("swap")
(bottomObj,topObj) = (topObj,bottomObj)
}

}

最佳答案

您的 centerView 已将 isUserInteractionEnabled 设置为 false。这会阻止其所有 subview (包括 switchButton)进行交互。

替换

centerView.isUserInteractionEnabled = false

centerView.isUserInteractionEnabled = true

关于ios - 单击 UIButton 时没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47871733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com