zoukankan      html  css  js  c++  java
  • 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
    
  • 相关阅读:
    用MFC(C++)实现拼音搜索
    MFC里ON_COMMAND_RANGE消息映射的ID问题
    01:MFC应用程序编程
    MFC笔记(DN)
    linuxmint系统定制与配置(2)-输入法
    linuxmint系统定制与配置(3)-字体
    linuxmint系统定制与配置(1)-系统初始配置
    笔记-读官方Git教程(2)~安装与配置
    笔记-读官方Git教程(1)~认识Git
    Python中多层List展平为一层
  • 原文地址:https://www.cnblogs.com/qqcc1388/p/6489753.html
Copyright © 2011-2022 走看看