• swift学习


    swift学习之计时器

    这个demo主要学习在swift中如何操作计时器(Timer),按钮(UIButton),文本(Label)

    效果图:

    代码

    import UIKit
    
    class ViewController: UIViewController {
    
        var btn1:UIButton?
        var timer:Timer?
        var label:UILabel?
        var a = 0.0
        
        override func viewDidLoad() {
            super.viewDidLoad()
            setupUI()
        }
        
        func setupUI() {
            
            let button = UIButton(frame:CGRect(x: 20, y: 400, 50, height: 50))
            button.backgroundColor = .red
            button.addTarget(self, action: #selector(startCLick(item:)), for: .touchUpInside)
            button.layer.cornerRadius = 25
            button.clipsToBounds = true
            button.setTitle("开始", for: .normal)
            btn1 = button
            view.addSubview(button)
            
            let btn = UIButton(frame:CGRect(x: 300, y: 400,  50, height: 50))
            btn.backgroundColor = .blue
            btn.addTarget(self, action: #selector(startCLick(item:)), for: .touchUpInside)
            btn.setTitle("暂停", for: UIControlState.normal)
            view .addSubview(btn)
            
            let lab = UILabel(frame: CGRect(x: 50, y: 50,  view.frame.size.width - 100, height: 50))
            lab.textAlignment = .center
            lab.font = .systemFont(ofSize: 18)
            lab.backgroundColor = .red
            lab.textColor = .white
            view.addSubview(lab)
            label = lab
            lab.text = "swift stopWatch Demo"
            
        }
    
        func timerIntervalx() {
            a+=1;
            label?.text = "swift stopWatch Demo (a)"
        }
        
        func startCLick(item:UIButton) {
            if item.isEqual(btn1) {
                timeStart()
            }
            else
            {
                timePause()
            }
        }
        
        func timeStart() {
            
            if !(timer != nil) {
                timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerIntervalx), userInfo: nil, repeats: true)
            }
        }
        
        func timePause() {
            timer?.invalidate()
            timer = nil
        }
        
        
    }
    

    基本控件的创建:

    UIButton:

    let btn = UIButton(frame:CGRect(x: 300, y: 400,  50, height: 50))
    

    UILabel:

     let lab = UILabel(frame: CGRect(x: 50, y: 50,  view.frame.size.width - 100, height: 50))
    

    Timer:

     timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(timerIntervalx), userInfo: nil, repeats: true)
    

    swift中方法名更加简单,枚举类型可以直接使用点语法操作

    .touchUpInside  .white .center
    
  • 相关阅读:
    HTML链接/实施CSS的三种方法
    XML之Well-Formed文档规则
    【摘】SVN提交与版本冲突
    Web开发之404小结
    TCP 连接的要点
    [转] Epoll 相对Poll和Select的优点
    [转] 剖析 epoll ET/LT 触发方式的性能差异误解(定性分析)
    GDB调试技巧
    [转] 关于c++的头文件依赖
    [转] Linux中gcc,g++常用编译选项
  • 原文地址:https://www.cnblogs.com/qqcc1388/p/6489753.html
Copyright © 2020-2023  润新知