swift笔记(基础UI实现)

2022-02-21  本文已影响0人  Harry__Li
UILalbel
 //注意书写的规范 在等号前后空一格
        let testlabel:UILabel = UILabel(frame: CGRect(x: 50, y: 150, width: 100, height: 30))
        self.view.addSubview(testlabel)
        testlabel.text = "纯代码创建的"
        testlabel.font = UIFont.systemFont(ofSize: 14)
        testlabel.numberOfLines = 0
        testlabel.textColor = UIColor.red
        testlabel.backgroundColor = UIColor.gray
        testlabel.textAlignment = .center

从上面看,其实相对于oc,它的属性都没有发生变化,只不过是写完发生了改变。在下面的UIButton imageview uitextview等等中是类似的情况

UIButton
        let testbutton:UIButton = UIButton(type: UIButton.ButtonType.custom)
        testbutton.frame = CGRect(x: 50, y: 200, width: 200, height: 30)
        testbutton.setTitle("存代码创建按钮", for: UIControl.State.normal)
        //设置字体
        testbutton.setTitleColor(UIColor.orange, for: .normal)
        testbutton.backgroundColor = UIColor.gray
        testbutton.titleLabel?.font=UIFont.systemFont(ofSize: 13)
//        testbutton.layer.cornerRadius = 5.0
//        testbutton.layer.masksToBounds = true
        self.view.addSubview(testbutton)
        testbutton.addTarget(self, action:#selector(btnclick(button:)), for: .touchUpInside)
        //设置某几个角为圆角
        let shape:CAShapeLayer = CAShapeLayer()
        let bepath:UIBezierPath = UIBezierPath(roundedRect: testbutton.bounds, byRoundingCorners:  UIRectCorner(rawValue: UIRectCorner.topLeft.rawValue | UIRectCorner.bottomLeft.rawValue) , cornerRadii: CGSize(width: 5, height: 5));
        UIColor.black.setStroke();
        shape.path = bepath.cgPath;
        testbutton.layer.mask = shape;

//@objc 1 允许这个函数"在运行时"通过oc的消息机制调用
    //      2
    @objc func btnclick(button:UIButton){
        print("dianji 了按钮")
        UIView.animate(withDuration: 3.0) {
            self.bgview.backgroundColor = .purple
            self.bgview.center.y = 400
        } completion: { Bool in
            print("动画完成")
        }

    }
imageview
 //====================图片==============
        let iconimage:UIImageView = UIImageView()
        iconimage.image = UIImage(named: "gender_male")
        iconimage.frame = CGRect(x: 50, y: 250, width: 80, height: 80)
        self.view.addSubview(iconimage)
        iconimage.layer.masksToBounds = true
        iconimage.layer.cornerRadius = 5.0
        iconimage.contentMode = .scaleAspectFill
        iconimage.layer.borderWidth = 2.0
        iconimage.layer.borderColor = UIColor.orange.cgColor
        
        iconimage.isUserInteractionEnabled = true
        let tapges:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(tapbtnclick(tap:)))
        iconimage.addGestureRecognizer(tapges)
 @objc func tapbtnclick(tap:UITapGestureRecognizer){
        print("手势点击")
    }
uiview
//+++++++++++++++++++uiview视图+++++++++++++
            self.bgview.frame=CGRect(x: 50, y: 340, width: 200, height: 50)
            self.bgview.backgroundColor = .orange
            self.bgview.layer.cornerRadius = 5.0
        self.view.addSubview(self.bgview)
uitextfield
 let testTextField:UITextField = UITextField.init()
        testTextField.frame = CGRect(x: 40, y: 400, width: 200, height: 30)
        testTextField.borderStyle = .roundedRect
        testTextField.placeholder = "请输入手机号"
        //返回键盘
        testTextField.keyboardType = .numberPad
        testTextField.textAlignment = .left
        self.view.addSubview(testTextField)
        testTextField.becomeFirstResponder()
        //键盘返回键
        testTextField.returnKeyType = .done
        //遵守协议
        testTextField.delegate = self;
        //监测text内容变化
        testTextField.addTarget(self, action: #selector(textchange(textfiled:)), for: UIControl.Event.editingChanged)
        //键盘监听
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(noti:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboradhide(noti:)), name: UIResponder.keyboardDidHideNotification, object: nil)

 //协议方法
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        return true
    }
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
        return true
    }
 @objc func keyboardWillShow(noti:NSNotification){
        print("键盘弹出")
    }
    @objc func keyboradhide(noti:NSNotification){
        print("键盘收起")
    }
    
    @objc func textchange(textfiled:UITextField){
        //可选型
        print("输入的是:\(textfiled.text)")
    }
uitextview
//++++++++++++++++++++++++uitextview ++++++++++++
        let contentTextView:UITextView = UITextView.init()
        contentTextView.frame = CGRect(x: 50, y: 440, width: 100, height: 70)
        contentTextView.layer.masksToBounds = true
        contentTextView.layer.cornerRadius = 5.0
        contentTextView.keyboardType = .default
        self.view.addSubview(contentTextView)
        contentTextView.textColor = .orange
        contentTextView.backgroundColor = .gray
        contentTextView.textAlignment = .left
        contentTextView.font = .systemFont(ofSize: 14)
        contentTextView.isSelectable = true
        contentTextView.delegate = self

//+++++++++++++textview delegate ++++++++++++
    func textViewDidChange(_ textView: UITextView) {
        print(textView.text)
    }
scrollview
 let bannerscrollview:UIScrollView = UIScrollView.init()
        bannerscrollview.backgroundColor = .red
        bannerscrollview.frame = CGRect(x: 50, y: 520, width: 250, height: 100)
        bannerscrollview.contentSize = CGSize(width: 250*3, height: 100)
        bannerscrollview.isPagingEnabled = true
        self.view.addSubview(bannerscrollview)
        //回弹设置
        bannerscrollview.bouncesZoom = false
        bannerscrollview.showsHorizontalScrollIndicator = false
        bannerscrollview.showsVerticalScrollIndicator = false
        bannerscrollview.bounces = false
        //点击设备状态栏是否回到顶部
        bannerscrollview.scrollsToTop = true

 //=========scrollview delegate
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
    }
uitableview
import UIKit

class ListTableViewVC: UIViewController, UITableViewDelegate,UITableViewDataSource {

    var names = ["A","C","D","F"]
    var listtableview : UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        self.listtableview = UITableView.init(frame: CGRect(x: 0, y: 0, width: self.view.bounds.size.width, height: self.view.bounds.size.height), style: .plain)
        self.listtableview?.delegate = self
        self.listtableview?.dataSource = self
        self.view.addSubview(self.listtableview)
        self.listtableview.register(UINib.init(nibName: "ListTabelCell", bundle: nil), forCellReuseIdentifier: "ListTabelCell")
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell:ListTabelCell? = (tableView.dequeueReusableCell(withIdentifier: "ListTabelCell") as! ListTabelCell)
        cell?.namelabel.text = "\(indexPath.row)"
        
        return cell!
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }

相同的collctionview也是类似的。相较于oc只是书写的格式不同了,协议方法的名字没有发生改变

上一篇 下一篇

猜你喜欢

热点阅读