自学 iOS - 三十天三十个 Swift 项目 第一天

最近公司项目不是很忙,偶然间看到编程语言排行榜,看到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 / 255, green: green / 255, blue: blue / 255, alpha: 1);
}

var RGBAColor: (CGFloat, CGFloat, CGFloat, CGFloat) -> UIColor = {red, green, blue, alpha in
    return UIColor(red: red / 255, green: green / 255, blue: blue / 255, 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(8, 1, 34)
        bottomLeft.backgroundColor = RGBColor(82, 91, 252)
        bottomRight.backgroundColor = RGBColor(102, 189, 9)
        
        topBox.snp.makeConstraints { (make) in
            make.width.equalTo(SCREEN_WIDTH)
            make.height.equalTo(SCREEN_HEIGHT * 0.4)
            make.left.equalTo(self.view).offset(0)
            make.top.equalTo(self.view).offset(0)
        }
        
        resertBtn.setTitle("Reset", for: UIControlState.normal)
        resertBtn.setTitleColor(RGBColor(255, 255, 255), 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: 100)
        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(100)
        }
        
        resertBtn.snp.makeConstraints { (make) in
            make.width.equalTo(120)
            make.top.equalTo(self.topBox).offset(20)
            make.height.equalTo(20)
            make.right.equalTo(self.topBox.snp.right).offset(-20)
        }
        
        bottomLeft.snp.makeConstraints { (make) in
            make.width.equalTo(SCREEN_WIDTH * 0.5)
            make.top.equalTo(topBox.snp.bottom).offset(0)
            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(0)
            make.top.equalTo(bottomLeft).offset(0)
        }
        
        bottomRight.snp.makeConstraints { (make) in
            make.left.equalTo(bottomLeft.snp.right).offset(0)
            make.width.equalTo(bottomLeft)
            make.height.equalTo(bottomLeft)
            make.top.equalTo(topBox.snp.bottom).offset(0)
        }
        
        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(0)
            make.top.equalTo(bottomRight).offset(0)
        }
        
        
        
    }
    // 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()
    }

}

 

 

posted @ 2017-03-14 23:52  徒步天涯  阅读(1185)  评论(1编辑  收藏  举报