iOS/swift 单选框和复选框

时间:2023-03-09 21:59:26
iOS/swift 单选框和复选框
/**
复选框
*/
import UIKit class LYBmutipleSelectView: UIView { var selectindexs:[Int]=[]//选中的 //标题数组
var titleArr:[String]=[""]{
didSet{
for i in ..<titleArr.count{
//组装按钮和label
let singleselectview:UIView=UIView.init(frame: CGRect.init(x: i*, y: , width: , height: )) let rightLbel:UILabel=UILabel.init(frame: CGRect.init(x: , y: , width: , height: ))
rightLbel.text=titleArr[i]
singleselectview.addSubview(rightLbel) let leftBtn:UIButton=UIButton.init(frame: CGRect.init(x: , y: , width: , height: ))
leftBtn.tag=+i;
leftBtn.setImage(UIImage.init(named: "fuxuankuangUnselect"), for: UIControl.State.normal)
leftBtn.addTarget(self, action: #selector(leftBtnClcik), for: UIControl.Event.touchUpInside)
singleselectview.addSubview(leftBtn) addSubview(singleselectview)
}
}
} override init(frame: CGRect) {
super.init(frame: frame)
initViews()
} required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
} func initViews(){
let sureBtn:UIButton=UIButton.init(frame: CGRect.init(x: , y: , width: , height: ))
sureBtn.setTitle("确认", for: UIControl.State.normal)
sureBtn.setTitleColor(UIColor.black, for: UIControl.State.normal)
sureBtn.addTarget(self, action: #selector(sureBtnClcik), for: UIControl.Event.touchUpInside)
addSubview(sureBtn)
} //确认按钮,根据选中的按钮索引做相应的操作
@objc func sureBtnClcik(){
print("\(selectindexs)")
} //点击按钮选中或取消
@objc func leftBtnClcik(sender:UIButton){
sender.isSelected = !sender.isSelected
let btnTag:Int=sender.tag-
if sender.isSelected{//选中
selectindexs.append(btnTag)//吧按钮的索引存储起来
}else {
//删除数组中的元素,采用过滤的方法,swift中没有现成f的方法
let fiflter:[Int]=selectindexs.filter {
$ != btnTag
}
selectindexs = fiflter
} sender.setImage(UIImage.init(named: "fuxuankuangUnselect"), for: UIControl.State.selected)
sender.setImage(UIImage.init(named: "fuxuankuangselect"), for: UIControl.State.selected)
} }
/**
单选框
*/ import UIKit class LYBSingleselectview: UIView { var selectindex:Int=//选中的
var lastbtn:UIButton=UIButton.init()//保存上一个按钮 //标题数组
var titleArr:[String]=[""]{
didSet{
for i in ..<titleArr.count{
//组装按钮和label
let singleselectview:UIView=UIView.init(frame: CGRect.init(x: i*, y: , width: , height: )) let rightLbel:UILabel=UILabel.init(frame: CGRect.init(x: , y: , width: , height: ))
rightLbel.text=titleArr[i]
singleselectview.addSubview(rightLbel) let leftBtn:UIButton=UIButton.init(frame: CGRect.init(x: , y: , width: , height: ))
leftBtn.tag=+i
leftBtn.setImage(UIImage.init(named: "fuxuankuangUnselect"), for: UIControl.State.normal)
leftBtn.addTarget(self, action: #selector(leftBtnClcik), for: UIControl.Event.touchUpInside)
singleselectview.addSubview(leftBtn) addSubview(singleselectview)
}
}
} override init(frame: CGRect) {
super.init(frame: frame)
initViews()
} required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
} func initViews(){
let sureBtn:UIButton=UIButton.init(frame: CGRect.init(x: , y: , width: , height: ))
sureBtn.setTitle("确认", for: UIControl.State.normal)
sureBtn.setTitleColor(UIColor.black, for: UIControl.State.normal)
sureBtn.addTarget(self, action: #selector(sureBtnClcik), for: UIControl.Event.touchUpInside)
addSubview(sureBtn)
} //确认按钮,根据选中的按钮索引做相应的操作
@objc func sureBtnClcik(){
print("\(selectindex)")
} //点击按钮选中或取消
@objc func leftBtnClcik(sender:UIButton){
let btnTag:Int=sender.tag-
sender.isSelected=true
lastbtn.isSelected=false
lastbtn.setImage(UIImage.init(named: "fuxuankuangUnselect"), for: UIControl.State.selected)
sender.setImage(UIImage.init(named: "fuxuankuangselect"), for: UIControl.State.selected)
lastbtn=sender
selectindex = btnTag
} }

原文链接:https://blog.****.net/u011146511/java/article/details/86578730