单击UIButton时没有任何反应

问题描述 投票:1回答:1

我已经创建了一个包含UIViewSwitchButton子类,但是即使我添加了一个目标,当我点击按钮时仍然没有反应/打印。那怎么样?

的viewController

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)
    }

}
ios swift uibutton
1个回答
3
投票

你的centerViewisUserInteractionEnabled设置为false。这会阻止其所有子视图(包括switchButton)进行交互。

更换

centerView.isUserInteractionEnabled = false

centerView.isUserInteractionEnabled = true
© www.soinside.com 2019 - 2024. All rights reserved.