/** 復選框 */ import UIKit class LYBmutipleSelectView: UIView { var selectindexs:[Int]=[]//選中的 //標題數組 var titleArr:[String]=[""]{ didSet{ for i in 0..<titleArr.count{ //組裝按鈕和label let singleselectview:UIView=UIView.init(frame: CGRect.init(x: i*100, y: 100, width: 100, height: 50)) let rightLbel:UILabel=UILabel.init(frame: CGRect.init(x: 50, y: 0, width: 50, height: 50)) rightLbel.text=titleArr[i] singleselectview.addSubview(rightLbel) let leftBtn:UIButton=UIButton.init(frame: CGRect.init(x: 10, y: 10, width: 30, height: 30)) leftBtn.tag=130+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: 200, y: 10, width: 100, height: 50)) 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-130 if sender.isSelected{//選中 selectindexs.append(btnTag)//吧按鈕的索引存儲起來 }else { //刪除數組中的元素,采用過濾的方法,swift中沒有現成f的方法 let fiflter:[Int]=selectindexs.filter { $0 != 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=0//選中的 var lastbtn:UIButton=UIButton.init()//保存上一個按鈕 //標題數組 var titleArr:[String]=[""]{ didSet{ for i in 0..<titleArr.count{ //組裝按鈕和label let singleselectview:UIView=UIView.init(frame: CGRect.init(x: i*100, y: 100, width: 100, height: 50)) let rightLbel:UILabel=UILabel.init(frame: CGRect.init(x: 50, y: 0, width: 50, height: 50)) rightLbel.text=titleArr[i] singleselectview.addSubview(rightLbel) let leftBtn:UIButton=UIButton.init(frame: CGRect.init(x: 10, y: 10, width: 30, height: 30)) leftBtn.tag=130+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: 200, y: 10, width: 100, height: 50)) 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-130 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.csdn.net/u011146511/java/article/details/86578730