• 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
    
  • 相关阅读:
    day1-python简介+安装
    dgango中admin下添加搜索功能
    调用zabbix 分组api
    python 调用zabbix api实现查询主机信息,输出所有主机ip
    python实现用户登录界面
    怎样过滤跨站恶意脚本攻击(XSS)
    java服务安装(一):使用java service wrapper及maven打zip包
    详解Maven项目利用java service wrapper将Java程序生成Windows服务
    使用tomcat7-maven-plugin部署Web项目
    常用Maven插件介绍
  • 原文地址:https://www.cnblogs.com/qqcc1388/p/6489753.html
Copyright © 2020-2023  润新知