1.

forEach

給兩個button添加事件

 [button1,button2].forEach{
            ($0.addTarget(self, action: #selector(buttonTapped(sender:)), for: .touchUpInside))
        }

sort

        bumbers.sort{(first,second) in 
            return first > second
         }
     等價
       bumbers.sort{$0 > $1}

optional類型

待編輯

幾個Xcode標記

// MARK: - 操作
// TODO: - 記得做
// FIXME: - 提醒

好像沒見到宏, 反正寫個常量哪里都可以訪問

<li><h1>學習代碼:<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("結束", 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
         }
         等價
         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.
    }


}
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容