最近公司项目不是很忙,偶然间看到编程语言排行榜,看到swift 已经排到前10了,然OC排名也越来越后了,感觉要上车了,虽然现在项目都是用OC写的,但是swift是一种趋势。在网上看到“自学 iOS - 三十天三十个 Swift 项目” 这篇博客,我也想自己在闲暇之余学习下swift,在看了2天的swift 的语法过后,才开始做这个,语法看的也不是很懂,有些部分。还是要自己动手
废话不多说
先上效果
这是这个简单的效果
1.首先 我去网上找了一下 swift自动布局的框架 “SnapKit” 用起来和Massory 差不多 上手很快
然后 对swift的一些必要东西 进行了宏定义 类似于OC 的PCH文件 swift 里面就比较简单 新建立个swift文件就可以了
代码如下
import UIKit import SnapKit let SCREEN_WIDTH = UIScreen.main.bounds.size.width
let SCREEN_HEIGHT = UIScreen.main.bounds.size.height var RGBColor: (CGFloat, CGFloat, CGFloat) -> UIColor = {red, green, blue in
return UIColor(red: red / , green: green / , blue: blue / , alpha: );
} var RGBAColor: (CGFloat, CGFloat, CGFloat, CGFloat) -> UIColor = {red, green, blue, alpha in
return UIColor(red: red / , green: green / , blue: blue / , alpha: alpha);
}
然后主控制器里面的代码(由于代码比较简单 我就没写注释了 )
import UIKit class ViewController: UIViewController { lazy var topBox = UIView()
lazy var bottomLeft = UIView()
lazy var bottomRight = UIView()
lazy var resertBtn = UIButton()
lazy var startBtn = UIButton()
lazy var pauseBtn = UIButton()
lazy var numberLabel = UILabel()
var timer: Timer! override func viewDidLoad() {
super.viewDidLoad() self.view.addSubview(topBox)
self.view.addSubview(bottomLeft)
self.view.addSubview(bottomRight) topBox.backgroundColor = RGBColor(, , )
bottomLeft.backgroundColor = RGBColor(, , )
bottomRight.backgroundColor = RGBColor(, , ) topBox.snp.makeConstraints { (make) in
make.width.equalTo(SCREEN_WIDTH)
make.height.equalTo(SCREEN_HEIGHT * 0.4)
make.left.equalTo(self.view).offset()
make.top.equalTo(self.view).offset()
} resertBtn.setTitle("Reset", for: UIControlState.normal)
resertBtn.setTitleColor(RGBColor(, , ), for: UIControlState.normal)
// resertBtn.backgroundColor = UIColor.red
resertBtn.addTarget(self, action: #selector(resert) , for: UIControlEvents.touchUpInside)
self.topBox.addSubview(resertBtn) numberLabel.text = "0.0"
numberLabel.font = UIFont.boldSystemFont(ofSize: )
numberLabel.textColor = UIColor.white
numberLabel.textAlignment = .center
topBox.addSubview(numberLabel) numberLabel.snp.makeConstraints { (make) in
make.center.equalTo(topBox)
make.width.equalTo(topBox)
make.height.equalTo()
} resertBtn.snp.makeConstraints { (make) in
make.width.equalTo()
make.top.equalTo(self.topBox).offset()
make.height.equalTo()
make.right.equalTo(self.topBox.snp.right).offset(-)
} bottomLeft.snp.makeConstraints { (make) in
make.width.equalTo(SCREEN_WIDTH * 0.5)
make.top.equalTo(topBox.snp.bottom).offset()
make.left.equalTo(self.view)
make.bottom.equalTo(self.view)
} startBtn.setTitle("开始", for: .normal)
startBtn.setTitleColor(UIColor.white, for: .normal)
startBtn.addTarget(self, action: #selector(start), for: .touchUpInside)
bottomLeft.addSubview(startBtn) startBtn.snp.makeConstraints { (make) in
make.width.equalTo(bottomLeft)
make.height.equalTo(bottomLeft)
make.left.equalTo(bottomLeft).offset()
make.top.equalTo(bottomLeft).offset()
} bottomRight.snp.makeConstraints { (make) in
make.left.equalTo(bottomLeft.snp.right).offset()
make.width.equalTo(bottomLeft)
make.height.equalTo(bottomLeft)
make.top.equalTo(topBox.snp.bottom).offset()
} pauseBtn.setTitle("停止", for: .normal)
pauseBtn.setTitleColor(UIColor.white, for: .normal)
pauseBtn.addTarget(self, action: #selector(pause), for: .touchUpInside)
bottomRight.addSubview(pauseBtn) pauseBtn.snp.makeConstraints { (make) in
make.width.equalTo(bottomRight)
make.height.equalTo(bottomRight)
make.left.equalTo(bottomRight).offset()
make.top.equalTo(bottomRight).offset()
} }
// MARK:清零的点击事件
func resert() {
startBtn.isUserInteractionEnabled = true
pauseBtn.isUserInteractionEnabled = true
numberLabel.text = "0.0"
timer.invalidate() } // MARK:开始事件
func start(){
startBtn.isUserInteractionEnabled = false
pauseBtn.isUserInteractionEnabled = true // 创建并启动定时器
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(numberChange), userInfo: self, repeats: true)
timer.fire()
} func numberChange() {
let number = NSString(string: numberLabel.text!).doubleValue
let changeNumber = number + 0.1
numberLabel.text = "\(changeNumber)"
} // MARK:暂停
func pause() {
pauseBtn.isUserInteractionEnabled = false
startBtn.isUserInteractionEnabled = true
timer.invalidate()
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
} }