前幾天逛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.
}
}