控件工廠類,簡(jiǎn)而言之就是,減少代碼的復(fù)用率,只在哪里用,然后在哪里調(diào):
代碼如下:
import UIKitclass ViewFactory: UIView,UITextFieldDelegate { //默認(rèn)控件的尺寸 class func getDefaultFrame( ) -> CGRect { let defaultFrame = CGRect(x:0,y:0,width:100,height:30) return defaultFrame } //類方法 class func createControl(type:String,title:[String],action:Selector,sender:AnyObject) -> UIView { switch type { case "label": return ViewFactory.creatLabel(title: title[0]) case "button": return ViewFactory.createButton(title: title[0], action: action, sender: sender as! UIViewController) case "text": return ViewFactory.creatTextField(value: title[0], action: action, sender: sender as! UIViewController as UIViewController as! UITextFieldDelegate) case "segment": return ViewFactory.creatSegment(items: [title[0]], action: action, sender: sender as! UIViewController) default: return ViewFactory.creatLabel(title: title[0]) } } //創(chuàng)建按鈕控件 class func createButton(title:String, action:Selector, sender:UIViewController) -> UIButton { let button = UIButton(frame:ViewFactory.getDefaultFrame()) button.backgroundColor = UIColor.orange button.setTitle(title, for:.normal) button.titleLabel!.textColor = UIColor.white button.titleLabel!.font = UIFont.systemFont(ofSize: 14) button.addTarget(sender, action:action, for:.touchUpInside) return button } //創(chuàng)建文本輸入框控件 class func creatTextField(value:String,action:Selector,sender:UITextFieldDelegate) -> UITextField { let textField = UITextField(frame:ViewFactory.getDefaultFrame()) textField.backgroundColor = UIColor.clear textField.textColor = UIColor.black textField.text = value textField.borderStyle = .roundedRect textField.adjustsFontSizeToFitWidth = true textField.delegate = sender return textField } //創(chuàng)建分段單選組件 class func creatSegment(items:[String],action:Selector,sender:UIViewController) -> UISegmentedControl { let segment = UISegmentedControl(items:items) segment.frame = ViewFactory.getDefaultFrame() segment.isMomentary = false segment.addTarget(self, action: action, for: .valueChanged) return segment } //創(chuàng)建文本標(biāo)簽控件 class func creatLabel(title:String) -> UILabel { let label = UILabel() label.textColor = UIColor.black label.backgroundColor = UIColor.white label.text = title label.frame = ViewFactory.getDefaultFrame() label.font = UIFont(name:"微軟雅黑",size:16) return label }}
調(diào)用:
func initVIewFactory() { //創(chuàng)建文本標(biāo)簽 let labelNum = ViewFactory.creatLabel(title: "閾值") labelNum.frame = CGRect(x:20,y:100,width:60,height:30) self.view.addSubview(labelNum) let labelDm = ViewFactory.creatLabel(title: "維度") labelDm.frame = CGRect(x:20,y:200,width:60,height:30) self.view.addSubview(labelDm) //創(chuàng)建文本輸入框 textNum = ViewFactory.creatTextField(value: "", action:#selector(factoryAction), sender: self as UITextFieldDelegate) textNum.frame = CGRect(x:80,y:100,width:200,height:30) textNum.returnKeyType = .done self.view.addSubview(textNum) let textNumSecond = ViewFactory.creatTextField(value: "", action: #selector(factoryActionSecond), sender: self as UITextFieldDelegate) textNumSecond.frame = CGRect(x:80,y:200,width:200,height:30) textNum.returnKeyType = .done self.view.addSubview(textNumSecond) //創(chuàng)建分段單選控件 segmentC = ViewFactory.creatSegment(items: ["3*3","4*4","5*5"], action: #selector(segmentAction), sender: self) segmentC.frame = CGRect(x:80,y:200,width:200,height:30) self.view.addSubview(segmentC) segmentC.selectedSegmentIndex = 0 //創(chuàng)建按鈕控件 factorybtn = ViewFactory.createButton(title: "確定", action: #selector(factoryClick), sender: self) factorybtn.frame.origin = CGPoint(x:80,y:300) self.view.addSubview(factorybtn) } func factoryAction() { } func factoryActionSecond() { } func segmentAction() { } func factoryClick() { print("我點(diǎn)擊了") }
效果如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選