30DaysOfSwift - Day1 計時器

前幾天逛Github,偶然看到一個Swift的項目 —— 30DaysOfSwift,作者一共用30個小項目,來熟悉Swift語言,而我正好也學習了一段時間的Swift語言,準備仿照這樣的模式,來更加深入的了解UI部分

今天做的是一個計時器項目,大概效果如下 :

作者在這個項目中,使用AutoLayout來完成自動布局,使用StoryBoard完成UI創(chuàng)建。

而我一直都是喜歡用純代碼布局,UI的搭建也是使用代碼完成。所以我在寫這個小Demo之前在我的項目里集成了SnapKit,使用類似Objective-C中常用的masonry框架來完成自動布局。

這里我還發(fā)現(xiàn)一個Swift中的小問題,使用cocoadPods集成第三方庫,引用不到頭文件的解決方法和Objective-C不一樣。

這是第一個Swift小Demo,很簡單,也很好的幫助熟悉UI.


import UIKit
import SnapKit

let SCREEN_WIDTH = UIScreen.mainScreen().bounds.size.width
let SCREEN_HEIGHT = UIScreen.mainScreen().bounds.size.height

let kTopViewHeight = SCREEN_HEIGHT * 0.4 //倒計時試圖高度
let kButtonHeight = SCREEN_HEIGHT * 0.6  //開始暫停按鈕高度
let kPauseButtonWidth = SCREEN_WIDTH * 0.4 //暫停按鈕寬度
let kStartButtonWidth = SCREEN_WIDTH * 0.6 //開始按鈕高度

var counter = 0.0
var timer = NSTimer()
var isPlaying = false


class ViewController: UIViewController {

    //MARK: - 懶加載
    
    
    //倒計時Label
    private lazy var showLabel: UILabel = {
        let label = UILabel(frame: CGRect.zero)
        label.text = String(counter)
        label.textColor = UIColor.yellowColor()
        label.font = UIFont.systemFontOfSize(100)
        label.textAlignment = NSTextAlignment.Center
        return label
    }()
    
    //頂部背景試圖
    private lazy var topBackgroundView: UIView = {
        let view = UIView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: kTopViewHeight))
        view.backgroundColor = UIColor.blackColor()
        return view
    }()
    
    //Reset按鈕
    private lazy var resetButton: UIButton = {
        let button = UIButton(type: (UIButtonType.Custom))
        button.frame = CGRectZero
        button.setTitle("Reset", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Highlighted)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.backgroundColor = UIColor.clearColor()
        button.addTarget(self, action: "buttonDidClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.tag = 101
        return button
    }()
    
    //暫停按鈕
    private lazy var pauseButton: UIButton = {
        let button = UIButton(type: (UIButtonType.Custom))
        button.frame = CGRectZero
        button.setImage(UIImage(named: "pause"), forState: UIControlState.Normal)
        button.backgroundColor = UIColor.greenColor()
        button.addTarget(self, action: "buttonDidClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.tag = 102
        return button
    }()
    
    //開始按鈕
    private lazy var startButton: UIButton = {
        let button = UIButton(type: (UIButtonType.Custom))
        button.frame = CGRectZero
        button.setImage(UIImage(named: "play"), forState: UIControlState.Normal)
        button.backgroundColor = UIColor.blueColor()
        button.addTarget(self, action: "buttonDidClick:", forControlEvents: UIControlEvents.TouchUpInside)
        button.tag = 103
        return button
    }()
    
    //MARK: - 創(chuàng)建UI界面
    
    func setupUI() {
        //頂部的背景試圖
        self.view.addSubview(self.topBackgroundView)
        // 顯示倒計時的Label
        self.topBackgroundView.addSubview(self.showLabel)
        self.showLabel.snp_makeConstraints { make in
            make.width.equalTo(SCREEN_WIDTH)
            make.height.equalTo(137)
            make.centerX.equalTo(self.topBackgroundView.snp_centerX)
            make.centerY.equalTo(self.topBackgroundView.snp_centerY)
        }
        //Reset按鈕
        self.topBackgroundView.addSubview(self.resetButton)
        self.resetButton.snp_makeConstraints { make in
            make.top.equalTo(self.topBackgroundView.snp_top).offset(20)
            make.right.equalTo(self.topBackgroundView.snp_right).offset(-20)
            make.height.equalTo(20)
            make.width.equalTo(60)
        }
        // 暫停按鈕
        self.view.addSubview(self.pauseButton)
        self.pauseButton.snp_makeConstraints { make in
            make.top.equalTo(self.topBackgroundView.snp_bottom).offset(0)
            make.left.equalTo(self.view).offset(0)
            make.height.equalTo(kButtonHeight)
            make.width.equalTo(kPauseButtonWidth)
        }
        
        //開始按鈕
        self.view .addSubview(self.startButton)
        self.startButton.snp_makeConstraints { make in
            make.top.equalTo(self.topBackgroundView.snp_bottom).offset(0)
            make.left.equalTo(self.pauseButton.snp_right).offset(0)
            make.height.equalTo(kButtonHeight)
            make.width.equalTo(kStartButtonWidth)
        }
    }
    //MARK: - 設置狀態(tài)欄
    override func preferredStatusBarStyle() -> UIStatusBarStyle {
        return UIStatusBarStyle.LightContent
    }
    
    //MARK: - ButtonClick
    func buttonDidClick(sender: AnyObject) {
        switch sender.tag {
            //reset
        case 101 :
            timer.invalidate()
            isPlaying = false
            counter = 0.0
            self.showLabel.text = String(counter)
            self.startButton.enabled = true
            self.pauseButton.enabled = false
            
            //暫停
        case 102 :
            self.startButton.enabled = true
            self.pauseButton.enabled = false
            isPlaying = false
            timer.invalidate()
            
            //開始
        case 103 :
            if (isPlaying) {
                return
            }
            self.startButton.enabled = false
            self.pauseButton.enabled = true
            timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("updateTimer"), userInfo: nil, repeats: true)
            isPlaying = true
            
        default :
            break
        }
    }
    
    //MARK: - 計時器方法
    func updateTimer() {
        counter = counter + 0.1
        self.showLabel.text = String(format: "%.1f", counter)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.whiteColor()
        setupUI()
    }
    
    

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


}

代碼已經(jīng)上傳到GitHub上

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,146評論 4 61
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,872評論 25 709
  • 有成長型思維模式的學生,他們的學習動機完全是為了自己。他們享受學習的過程,而不是一味地追求好成績。即使他們覺得教材...
    快樂的石頭閱讀 213評論 0 0
  • ljc@ubuntu:~$ perf 程序“perf”尚未安裝。 您可以使用以下命令安裝: sudo apt in...
    闖爺閱讀 21,683評論 0 4
  • 目錄的'x'權(quán)限 目錄的執(zhí)行權(quán)限代表是否可以切換到一個目錄中去。要讀寫目錄下的某個文件,就必須對從根到文件所在目錄...
    chandarlee閱讀 1,386評論 0 51

友情鏈接更多精彩內(nèi)容