欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

swift控件工廠類的實(shí)現(xiàn)代碼

 更新時(shí)間:2017年09月29日 11:07:29   作者:稻草人11223  
這篇文章主要為大家詳細(xì)介紹了swift控件工廠類的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

控件工廠類,簡(jiǎn)而言之就是,減少代碼的復(fù)用率,只在哪里用,然后在哪里調(diào):

代碼如下:

import UIKit

class 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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • iOS UITableView展開縮放動(dòng)畫實(shí)例代碼

    iOS UITableView展開縮放動(dòng)畫實(shí)例代碼

    這篇文章主要介紹了Swift UITableView展開縮放動(dòng)畫實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-08-08
  • swift 3.0 實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí)功能

    swift 3.0 實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí)功能

    這篇文章主要介紹了swift 3.0 實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí)功能的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Swift自定義iOS中的TabBarController并為其添加動(dòng)畫

    Swift自定義iOS中的TabBarController并為其添加動(dòng)畫

    這篇文章主要介紹了Swift自定義iOS中的TabBarController并為其添加動(dòng)畫的方法,即自定義TabBarController中的的TabBar并為自定義的TabBar增加動(dòng)畫效果,需要的朋友可以參考下
    2016-04-04
  • Swift 3.0 enum 的靈活使用介紹

    Swift 3.0 enum 的靈活使用介紹

    這篇文章主要介紹了Swift 3.0 enum 的靈活使用介紹,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-05-05
  • Swift?指針底層探索分析

    Swift?指針底層探索分析

    這篇文章主要為大家介紹了Swift?指針底層探索分析,主要包括指針的基本使用,以及指針的內(nèi)存綁定進(jìn)行詳細(xì)分析,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Swift用final關(guān)鍵字來防止重寫

    Swift用final關(guān)鍵字來防止重寫

    final關(guān)鍵字在大多數(shù)的編程語言中都存在,表示不允許對(duì)其修飾的內(nèi)容進(jìn)行繼承或者重新操作。下面通過實(shí)例代碼給大家介紹swift用final關(guān)鍵字來防止重寫
    2016-12-12
  • 簡(jiǎn)陋的swift carthage copy-frameworks 輔助腳本代碼

    簡(jiǎn)陋的swift carthage copy-frameworks 輔助腳本代碼

    下面小編就為大家分享一篇簡(jiǎn)陋的swift carthage copy-frameworks 輔助腳本代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • Swift圖像處理之優(yōu)化照片

    Swift圖像處理之優(yōu)化照片

    Core Image能通過分析圖片的各個(gè)屬性,人臉的區(qū)域等進(jìn)行自動(dòng)優(yōu)化圖片。我們只需要調(diào)用autoAdjustmentFiltersWithOptions這個(gè)API方法獲取各個(gè)自動(dòng)增強(qiáng)濾鏡來優(yōu)化圖片即可。不管是人物照片還是風(fēng)景照均可增強(qiáng)效果
    2015-11-11
  • 使用Swift實(shí)現(xiàn)iOS App中解析XML格式數(shù)據(jù)的教程

    使用Swift實(shí)現(xiàn)iOS App中解析XML格式數(shù)據(jù)的教程

    這篇文章主要介紹了使用Swift實(shí)現(xiàn)iOS App中解析XML格式數(shù)據(jù)的教程,講到了iOS中提供的NSXMLParser和NSXMLParserDelegate兩個(gè)API的用法,需要的朋友可以參考下
    2016-04-04
  • swift中自定義正則表達(dá)式運(yùn)算符=~詳解

    swift中自定義正則表達(dá)式運(yùn)算符=~詳解

    這篇文章主要給大家介紹了關(guān)于swift中自定義正則表達(dá)式運(yùn)算符=~的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12

最新評(píng)論