Day 1 - 计时器

作者: Codepgq | 来源:发表于2016-09-05 11:11 被阅读64次

1、创建工程

创建工程

<br />

2、布局UI

UI

<br />

3、关联

关联

<br />

4、添加方法

拖线到工程中

5、开始做事:

1、点击开始按钮,开始计时,并显示的到label上,所以需要一个变量去存储。
2、点击停止按钮,停止计时。

//定义变量
    var Time = 0.0;
    var Timer = NSTimer()

开始按钮方法:

 //点击开始
    @IBAction func startClick(sender: UIButton) {
        startBtn.enabled = false;
        pauseBtn.enabled = true;
        Timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: #selector(ViewController.UpdateTimeLabel), userInfo: nil, repeats: true);
        timeLabel.text = String(Time);
    }
startBtn/pauseBtn.enable 设置当你点击开始了,就不允许点击开始,只能点击停止
Timer:创建一个定时器,当点击停止,把定时器停止
timeLabel:设置开始之前的值为0

点击停止按钮

//点击暂停
    @IBAction func pauseClick(sender: UIButton) {
        Timer.invalidate();
        Time = 0.0
        startBtn.enabled = true;
        pauseBtn.enabled = false;
    }
line 1 invalidate()关闭定时器
line 2 计数归零
line 3 4:设置停止按钮不能点击,只能点击开始按钮

更新label的方法:

//更新label
    func UpdateTimeLabel() {
        Time += 0.1;
        timeLabel.text = String(format: "%.1f", Time)
    }
line 1 累加
line 2 更新label

VC全部代码

//
//  ViewController.swift
//  Day_01计时器
//
//  Created by ios on 16/9/5.
//  Copyright © 2016年 ios. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var startBtn: UIButton!
    @IBOutlet weak var pauseBtn: UIButton!
    
    //定义变量
    var Time = 0.0;
    var Timer = NSTimer()
    
    //设置状态栏style
    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return UIStatusBarStyle.Default
    }
    
    //view did load
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    //收到内存警告
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    //更新label
    func UpdateTimeLabel() {
        Time += 0.1;
        timeLabel.text = String(format: "%.1f", Time)
    }

    //点击开始
    @IBAction func startClick(sender: UIButton) {
        startBtn.enabled = false;
        pauseBtn.enabled = true;
        Timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: #selector(ViewController.UpdateTimeLabel), userInfo: nil, repeats: true);
        timeLabel.text = String(Time);
    }
    
    //点击暂停
    @IBAction func pauseClick(sender: UIButton) {
        Timer.invalidate();
        Time = 0.0
        startBtn.enabled = true;
        pauseBtn.enabled = false;
    }
}


相关文章

  • Day 1 - 计时器

    1、创建工程 2、布局UI 3、关联 4、添加方法 5、开始做事: 1、点击开始按钮,开始计时,并显示的到labe...

  • TCP的4个计时器

    大多数TCP至少实现使用4个计时器:重传计时器,持续计时器,保活计时器,TIME_WAIT计时器 1.重传计时器:...

  • 正面管教每日践行

    Day4:2020年10月1日 今日践行工具:练习 练习是“花时间训练”的重要部分。 1、让孩子们轮流设置计时器,...

  • swift-计时器

    iOS中计时器工具类如何设计呢? 1.需求 开启/关闭计时器。 设置计时器周期。 设置计时器是否周期触发。 回调计...

  • 10.12学习总结

    1 今天讲了计时器,包括基础,通用还有高级2 计时器的分频范围是1-65536 3 计时器还需要设置中断nvic...

  • 计时器

    1、系统为每个进程维护3个计时器:1)真实计时器: 程序运行的实际时间2)虚拟计时器:用户态消耗的时间3)实用计...

  • 轻松flutter 之 时间处理 | 动画 | 启动页

    一. 计时器 要使用计时器先要导入dart:async包,整个计时器体验和JS基本一致 1. Duration 间...

  • 计时器使用教程

    一、计时器使用教程 计时器图文教程-点击查看详情 二、防止计时器停止设置 1.华为手机设置教程-点击查看详情 2....

  • iOS NSTimer简单使用

    1 计时器添加到NSRunloop

  • 30DaysOfSwift - Day1 计时器

    前几天逛Github,偶然看到一个Swift的项目 —— 30DaysOfSwift,作者一共用30个小项目,来熟...

网友评论

    本文标题:Day 1 - 计时器

    本文链接:https://www.haomeiwen.com/subject/wcrrettx.html