forEach
給兩個(gè)button添加事件
[button1,button2].forEach{
($0.addTarget(self, action: #selector(buttonTapped(sender:)), for: .touchUpInside))
}
sort
bumbers.sort{(first,second) in
return first > second
}
等價(jià)
bumbers.sort{$0 > $1}
optional類型
待編輯
幾個(gè)Xcode標(biāo)記
// MARK: - 操作
// TODO: - 記得做
// FIXME: - 提醒
好像沒見到宏, 反正寫個(gè)常量哪里都可以訪問
<li><h1>學(xué)習(xí)代碼:<h1></li>
ViewController.swift
import UIKit
class ViewController: UIViewController {
let showLable = UILabel(frame: CGRect(x: 0.0, y: 0.0, width: YHScreenWidth, height: YHScreenHeight/2))
let beign = UIButton(frame: CGRect(x: 0.0, y: YHScreenHeight/2, width: YHScreenWidth/2, height: YHScreenHeight/2))
let pause = UIButton(frame: CGRect(x: YHScreenWidth/2, y: YHScreenHeight/2, width: YHScreenWidth/2, height: YHScreenHeight/2))
var time : Timer?
var n = 0
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white//背景色
//添加界面
setupView()
}
func setupView(){
showLable.backgroundColor = .yellow
beign.backgroundColor = .orange
pause.backgroundColor = .blue
beign.setTitle("開始", for: .normal)
beign.setTitle("結(jié)束", for: .selected)
pause.setTitle("暫停", for: .normal)
pause.setTitle("繼續(xù)", for: .selected)
[beign,pause].forEach{
($0.addTarget(self, action: #selector(buttonTapped(sender:)), for: .touchUpInside))
}
/*
bumbers.sort{(first,second) in
return first > second
}
等價(jià)
bumbers.sort{$0 > $1}
*/
showLable.text = "0"
showLable.textColor = .white
showLable.font = UIFont.systemFont(ofSize: YHScreenHeight/4, weight: YHScreenWidth/4)
showLable.textAlignment = .center
view.addSubview(showLable)
view.addSubview(beign)
view.addSubview(pause)
}
func buttonTapped(sender: UIButton) {
switch sender {
case beign:
beign.isSelected = !beign.isSelected
beign.isSelected ? beginSC() : stopSC()
case pause:
pause.isSelected = !pause.isSelected
pause.isSelected ? pauseSC() : continSC()
default:
break
}
}
// MARK: - 操作
// TODO: - 記得做
// FIXME: - 提醒
func beginSC() {
time = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(changeLabel), userInfo: nil, repeats: true)
}
func stopSC() {
showLable.text = "0"
time?.invalidate()
time = nil
}
func pauseSC() {
if !beign.isSelected {
return
}
time?.invalidate()
time = nil
}
func continSC() {
if !beign.isSelected {
return
}
beginSC()
}
func changeLabel() {
n += 1
showLable.text = String(n)
//s = "\(x)" | toString(x) | x.description
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}