本文主要是介绍iOS/swift 单选框和复选框,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.********复选框*******
/**复选框使用: let single = LYBmutipleSelectView.init(frame: CGRect.init(x: 0, y: 0, width: WIDTH, height: 550))single.titleArr=["one","two","three"]self.view.addSubview(single)*/
import UIKitclass LYBmutipleSelectView: UIView {var selectindexs:[Int]=[]//选中的//标题数组var titleArr:[String]=[""]{didSet{for i in 0..<titleArr.count{//组装按钮和labellet singleselectview:UIView=UIView.init(frame: CGRect.init(x: i*100, y:0, 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: 5, y: 5, width: 40, height: 40))leftBtn.tag=130+i;leftBtn.setImage(UIImage.init(named: "other"), 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: WIDTH-100, y: 30, 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.isSelectedlet btnTag:Int=sender.tag-130if sender.isSelected{//选中selectindexs.append(btnTag)//吧按钮的索引存储起来sender.setImage(UIImage.init(named: "center"), for: UIControl.State.normal)}else {//删除数组中的元素,采用过滤的方法,swift中没有现成的删除方法let fiflter:[Int]=selectindexs.filter {$0 != btnTag}selectindexs = fifltersender.setImage(UIImage.init(named: "other"), for: UIControl.State.normal)}}}
2。********* 单选框*******
/**单选框使用:let single = LYBSingleselectview.init(frame: CGRect.init(x: 0, y: 0, width: WIDTH, height: 550))single.titleArr=["one","two","three"]self.view.addSubview(single)*/import UIKitclass LYBSingleselectview: UIView {var selectindex:Int=0//选中的var lastbtn:UIButton=UIButton.init()//保存上一个按钮var selectblock:(Int)->()={(Int)->() in }//标题数组var titleArr:[String]=[""]{didSet{for i in 0..<titleArr.count{//组装按钮和labellet singleselectview:UIView=UIView.init(frame: CGRect.init(x: i*100, y: 0, 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: 5, y: 5, width: 40, height: 40))leftBtn.tag=130+ileftBtn.setImage(UIImage.init(named: "other"), for: UIControl.State.normal)leftBtn.addTarget(self, action: #selector(leftBtnClcik(sender:)), for: UIControl.Event.touchUpInside)singleselectview.addSubview(leftBtn)addSubview(singleselectview)}}}override init(frame: CGRect) {super.init(frame: frame)initViews()self.selectblock = {(index) inprint("\(index)")}}required init?(coder aDecoder: NSCoder) {fatalError("init(coder:) has not been implemented")}func initViews(){let sureBtn:UIButton=UIButton.init(frame: CGRect.init(x: WIDTH-100, y: 30, 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){print("左边按钮")let btnTag:Int=sender.tag-130sender.isSelected=truelastbtn.isSelected=falselastbtn.setImage(UIImage.init(named: "other"), for: UIControl.State.selected)sender.setImage(UIImage.init(named: "center"), for: UIControl.State.selected)lastbtn=senderselectindex = btnTagself.selectblock(selectindex)}}
这篇关于iOS/swift 单选框和复选框的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!