gpt4 book ai didi

swift - 如何判断UIButton类的选择状态?

转载 作者:行者123 更新时间:2023-11-30 10:38:18 25 4
gpt4 key购买 nike

我创建了一个 UIButton 子类,单击后图像之间会发生变化。我如何确定此选择的状态,以用作定义此类的单独 View Controller 中其他位置的引用?

class ClickingCheckbox: UIButton {

convenience init() {
self.init(frame: .zero)
self.setImage(UIImage(named: "yes"), for: .normal)
self.setImage(UIImage(named: "no"), for: .selected)
self.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
}

@objc private func buttonTapped() {
UIView.animate(withDuration: 0.1, delay: 0.1, options: .curveLinear, animations: {
self.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
}) { (success) in
self.isSelected = !self.isSelected
UIView.animate(withDuration: 0.1, delay: 0.1, options: .curveLinear, animations: {
self.transform = .identity
}, completion: nil)
//self.isSelected = !self.isSelected
}
}
}

非常感谢!

最佳答案

我认为下面的代码会对您有所帮助。

import UIKit

class ViewController: UIViewController {

let button = ClickingCheckbox()
override func viewDidLoad() {
super.viewDidLoad()
button.frame = CGRect(x: self.view.frame.width/2 - button.frame.width/2 , y: self.view.frame.height/2 - button.frame.height/2 , width: 100, height: 30)
button.backgroundColor = UIColor.red
self.view.addSubview(button)
}

@IBAction func onClicksButton(_ sender: Any) {
if self.button.isSelected {
print("yes selcted ")
}
else{
print("no selcted")
}
}

}

关于swift - 如何判断UIButton类的选择状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57386213/

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