Swift最最基础控件学习

一些基本的Swift入门级别的控件使用方法,只适合入门的人看。大神请绕道demo

Swift最最基础控件学习_第1张图片
效果图

Label的创建

//一个构造方法
 func creatlabel(tittle:String,fontSize:CGFloat ,textAligement:NSTextAlignment, backgroudColor:UIColor,textColor:UIColor,frame:CGRect)  {
        let label = UILabel.init(frame: frame)
       label.font = UIFont.systemFont(ofSize: fontSize)
        label.text = tittle
        label.textAlignment = textAligement
        label.textColor = textColor
        label.backgroundColor = backgroudColor
        self.view.addSubview(label)
    }
// 调用方法创建lable
 override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        creatlable(tittle: "测试创建Lable", fontSize: 14, textAligement: NSTextAlignment.center, backgroudColor: .red, textColor: .white, frame:CGRect.init(x: (self.view.frame.size.width-200)/2, y: 100, width: 200, height: 45))
    }

Button的创建

//一个构造方法
func creatlable(tittle:String,fontSize:CGFloat ,textAligement:NSTextAlignment, backgroudColor:UIColor,textColor:UIColor,frame:CGRect)  {
        let button = UIButton.init(frame: frame)
        button.titleLabel?.font = UIFont.systemFont(ofSize: fontSize)
        button .setTitle(tittle, for: UIControlState.normal)
        button.titleLabel?.textAlignment = textAligement
        button.setTitleColor(textColor, for: .normal)
        button.backgroundColor = backgroudColor
        button.addTarget(self, action: #selector(tapAction), for: .touchUpInside)
        self.view.addSubview(button)
    }
//添加方法
override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        creatlable(tittle: "测试创建Button", fontSize: 14, textAligement: NSTextAlignment.center, backgroudColor: .red, textColor: .white, frame:CGRect.init(x: (self.view.frame.size.width-200)/2, y: 100, width: 200, height: 45))
    }
//点击事件
 @objc func tapAction()  {
     let alert = UIAlertController.init(title: "这是一个button", message: nil, preferredStyle: .actionSheet)
     let action = UIAlertAction.init(title: "确定", style: .default) { (action) in
    }
    let action1 = UIAlertAction.init(title: "取消", style: .cancel) { (action) in
    }
    alert.addAction(action)
    alert.addAction(action1)
    self .present(alert, animated: true, completion: nil)
    }

UITextField的使用

//懒加载一个UITextField
lazy var textfield: UITextField = {
        let textfield = UITextField.init(frame: CGRect.init(x: (self.view.frame.size.width-self.view.frame.size.width*0.8)/2, y: 100, width: self.view.frame.size.width*0.8, height: 45))
        textfield.backgroundColor = UIColor.groupTableViewBackground
        textfield.placeholder = "这是一个输入框"
        textfield.layer.cornerRadius = 5
        textfield.font = UIFont.systemFont(ofSize: 12)
        textfield.clipsToBounds = true
        let image = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: 25, height: 25))
        image.image = UIImage.init(named: "tab_mine_selected@2")
        textfield.leftView = image
        textfield.leftViewMode = .always
        return textfield
    }()
//添加UITextField
 override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.view.addSubview(self.textfield)
    }

TableViewController的使用

//懒加载一个tableivew
lazy var tableview: UITableView = {
        let tableview = UITableView.init(frame: self.view.bounds, style: UITableViewStyle.plain)
        tableview.backgroundColor = UIColor.white
        tableview.rowHeight = 55
        tableview.delegate = self
        tableview.dataSource = self
        tableview.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        return tableview
    }()
//添加和一些代理
  override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.view.addSubview(self.tableview)
    }
  func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableview.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = (indexPath.row+1).description
        return cell
    }
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return  "这是第"+(section+1).description + "区"
    }

CollectionViewController的使用

override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.view.addSubview(self.collectionview)
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 20
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionview.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
        cell.backgroundColor = UIColor.red
        return cell
    }
//懒加载UICollectionView
    lazy var collectionview: UICollectionView = {
        let flowlayout = UICollectionViewFlowLayout.init()
        flowlayout.itemSize = CGSize.init(width: (self.view.frame.size.width-40)/3-20, height: 60)
        flowlayout.sectionInset = UIEdgeInsets.init(top: 10, left: 10, bottom: 10, right: 10)
        let collection = UICollectionView.init(frame: self.view.bounds, collectionViewLayout: flowlayout)
        collection.delegate = self
        collection.dataSource = self
        collection.backgroundColor = UIColor.white
        collection.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        return collection
    }()

自定义view动画弹出

//点击button的调用弹出view方法
@objc  func tapAction()  {
    print("点击弹出动画")
    let anition = AnimationVIew.init(frame: self.view.bounds)
    anition.show()
    }
//自定义view的主要方法
lazy var baseView:UIView = UIView()
    //显示
    func show()  {
        UIApplication.shared.keyWindow?.addSubview(self)
        self.baseView.transform =  CGAffineTransform(scaleX: 0.5, y: 0.5)
        self.alpha = 0
        UIView.animate(withDuration: 0.3) {
            self.baseView.transform = CGAffineTransform(scaleX: 1, y: 1)
            self.alpha = 1
        }
    }
    //消失
    @objc func hide()  {
        UIView.animate(withDuration: 0.25, animations: {
            self.alpha = 0
        }) { (complete) in
            self.removeFromSuperview()
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.groupTableViewBackground
        setUpsubviews()
        let tapgesture = UITapGestureRecognizer.init(target: self, action: #selector(hide))
        self.addGestureRecognizer(tapgesture)
        
    }
    func setUpsubviews()  {
        baseView = UIView.init(frame: CGRect.init(x: (self.frame.size.width-self.frame.size.width*0.8)/2, y: 100, width: self.frame.size.width*0.8, height: 300))
        baseView.layer.cornerRadius = 10
        baseView.clipsToBounds = true
        baseView.backgroundColor = UIColor.red
        self.addSubview(self.baseView)
    }

你可能感兴趣的:(Swift最最基础控件学习)