Learn iOS and ios
Recently, the company's projects were not very busy. By chance, I saw the top 10 Programming Language rankings and swift had ranked first 10. However, the OC rankings were getting better and better, and I felt like I was about to get on the bus, although the projects are all written in OC, swift is a trend. I saw the blog "Learn iOS-30 Swift projects in 30 days" on the Internet. I also want to learn swift in my spare time. After reading the swift syntax for 2 days, when you start to do this, the syntax is not very familiar, some parts. Do it yourself
Not much nonsense
First Effect
This is a simple result.
1. First, I went online to find the swift Automatic Layout Framework "SnapKit", which is almost as quick as Massory.
Then, the macro definition of some necessary things in swift is similar to the PCH file in OC. In swift, it is easier to create a new swift file.
The Code is as follows:
import UIKitimport SnapKitlet SCREEN_WIDTH = UIScreen.main.bounds.size.widthlet SCREEN_HEIGHT = UIScreen.main.bounds.size.heightvar 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);}
Then the code in the master controller (because the code is relatively simple, I did not write comments)
Import UIKitclass 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. failed to (SCREEN_WIDTH) make. height. similar to (SCREEN_HEIGHT * 0.4) make. left. similar to (self. view ). offset (0) make. top. similar to (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. similar to (topBox) make. width. similar to (topBox) make. height. failed to (100)} resertBtn. snp. makeConstraints {(make) in make. width. failed to (120) make. top. similar to (self. topBox ). offset (20) Make. height. failed to (20) make. right. similar to (self. topBox. snp. right ). offset (-20)} bottomLeft. snp. makeConstraints {(make) in make. width. failed to (SCREEN_WIDTH * 0.5) make. top. reply to (topBox. snp. bottom ). offset (0) make. left. similar to (self. view) make. bottom. similar to (self. view)} startBtn. setTitle ("START", :. normal) startBtn. setTitleColor (UIColor. white, :. normal) startBtn. addTarget (self, action: # selec Tor (start), :. touchUpInside) bottomLeft. addSubview (startBtn) startBtn. snp. makeConstraints {(make) in make. width. failed to (bottomLeft) make. height. failed to (bottomLeft) make. left. failed to (bottomLeft ). offset (0) make. top. failed to (bottomLeft ). offset (0)} bottomRight. snp. makeConstraints {(make) in make. left. similar to (bottomLeft. snp. right ). offset (0) make. width. failed to (bottomLeft) make. height. botto MLeft) make. top. reply to (topBox. snp. bottom ). offset (0)} pauseBtn. setTitle ("stop", :. normal) pauseBtn. setTitleColor (UIColor. white, :. normal) pauseBtn. addTarget (self, action: # selector (pause), :. touchUpInside) bottomRight. addSubview (pauseBtn) pauseBtn. snp. makeConstraints {(make) in make. width. failed to (bottomRight) make. height. failed to (bottomRight) make. left. failed to (bottomRight ). offset (0) mak E. top. failed to (bottomRight ). offset (0) }// MARK: Click Event func resert () {startBtn. isUserInteractionEnabled = true pauseBtn. isUserInteractionEnabled = true numberLabel. text = "0.0" timer. invalidate ()} // MARK: start event func start () {startBtn. isUserInteractionEnabled = false pauseBtn. isUserInteractionEnabled = true // create and start the timer Timer = timer. scheduledTimer (timeInterval: 0.1, target: self, selector: # sele Ctor (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: pause func pause () {pauseBtn. isUserInteractionEnabled = false startBtn. isUserInteractionEnabled = true timer. invalidate ()} override func didReceiveMemoryWarning () {super. didReceiveMemoryWarning ()}}