2015年5月6日 星期三

練習計時器Swopwatch

今天練習一個計時器的app,可加入Navigation,以及Toolbar,以及
Flexible Space Bar Button的拉寬間距用的空項目,並用自動排版來定位上下區塊。

其中,設定了一個func,不太理解,要爬文一下><!

#why

參考:
http://blog.kumaya.co/2014/11/17/nstimer/
NSTimer 是 Foundation framework 的成員,可以用來處理定期週期性的行為或是拿來做倒數計時器。這裡做個簡單的筆記如何使用它。


練習之程式碼(僅練習計算時間的方式,真正的程式在下方Github Gist):
import UIKit

class ViewController: UIViewController {
    
    var timer = NSTimer()
    
    var count = 0
    
    func updateTime() {
    
        count++
        
        time.text = "\(count)"
    
    }
    
    @IBOutlet var time: UILabel!
    
    
    @IBAction func pause(sender: AnyObject) {
        
        timer.invalidate()
        
    }
    
    @IBAction func stop(sender: AnyObject) {
        
        timer.invalidate()
        
        count = 0
        
        time.text = "0"
    }
    
    @IBAction func play(sender: AnyObject) {
        
        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true) 
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

參考:自動佈局
http://blog.csdn.net/thelittleboy/article/details/13170675

參考:
http://bbs.bjflexedu.com/upload/forum.php?mod=viewthread&tid=279

NSTimer叫做“定时器”它的作用如下在指定的时间
执行指定的任务
每隔一段时间执行指定的任务调用下面的方法
就会开启一个定时任务

class func scheduledTimerWithTimeInterval(ti: NSTimeInterval,
target aTarget: AnyObject, selector aSelector: Selector, userInfo: AnyObject?,
 repeats yesOrNo: Bool) -> NSTimer
每隔ti秒,调用一次aTarget的aSelector方法,
yesOrNo决定了是否重复执行这个任务

func invalidate()通过invalidate方法可以停止定时器的工作,一旦定时器被停止了,
就不能再次执行任务。只能再创建一个新的定时器才能执行新的任务。


參考:
[Objective-C] NSTimer - 基本用法
http://aiur3908.blogspot.tw/2014/11/objective-c-nstimer.html

參考:
IOS NSTimer 定时器用法总结http://my.oschina.net/u/2340880/blog/398598

參考:
IOS UIPageControl & NSTimer定时器 & 图片轮播实例http://my.oschina.net/u/1032974/blog/364520#OSC_h4_4

參考:
NSTimer计时器,作用是每隔多少时间执行相应的方法
1,创建方法一
Internal:后面跟的是每隔多少秒执行方法
userInfo:一般后面跟nil
repeats:是否循环执行
http://blog.csdn.net/qq_24954629/article/details/42804159

參考教程:
从零开始学Swift计时器App开发http://swiftist.org/topics/96?page=4#77

對應:
Rob Swift 41


沒有留言:

張貼留言

歡迎網友的交流與分享,謝謝。