iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 27
0
Mobile Development

從零開始-30日練習開發iOS APP系列 第 27

從零開始-30日練習開發iOS APP-DispatchQueue + UIButton Day-27

  • 分享至 

  • xImage
  •  

正文:
多線程調色

此畫面會用到多執行緒,將下方程式碼打在viewDidLoad()內,就可以執行
程式碼:

DispatchQueue.global().async {
    while(true){
        //usleep(1000000)為1秒
        usleep(20000)
        DispatchQueue.main.async {
            //寫要作用的func
            //儘量不要放 usleep 或 sleep
            //放入要改變的UI內容就好,否則執行時會有時間差的問題
        }
    }
}

ViewController3 程式碼:

import UIKit

class ViewController3: UIViewController{
    
    @IBOutlet weak var numColor: UIButton!
    @IBOutlet weak var TableView: UIButton!
    @IBOutlet weak var Alert: UIButton!
    @IBOutlet weak var num3: UIButton!
    @IBOutlet weak var RealmButton: UIButton!
    @IBOutlet weak var num5: UIButton!
    public var r1:Float = 0
    public var g1:Float = 0
    public var b1:Float = 0
    public var r3:Float = 0
    public var g3:Float = 0
    public var b3:Float = 0
    public var r5:Float = 0
    public var g5:Float = 0
    public var b5:Float = 0
    public var r17:Float = 0
    public var g17:Float = 0
    public var b17:Float = 0
    public var r100:Float = 0
    public var g100:Float = 0
    public var b100:Float = 0
    public var run1=0
    public var run3=0
    public var run5=0
    public var run17=0
    public var run255=0
    override func viewDidLoad() {
        super.viewDidLoad()
        DispatchQueue.global().async {
            while(true){
                usleep(20000)
                self.colorRGBi1(i:1)
                self.colorRGBi3(i:3)
                self.colorRGBi5(i:5)
                self.colorRGBi17(i:17)
                self.colorRGBi100(i:100)
                self.colorch()
                
            }
        }
        

    }
    func colorRGBi1(i:Float){
        if (run1 == 0)
        {
            if (r1 >= 1.0){
                run1 = 1
            }
            else {
                if (g1 != 0){
                g1 -= (i/255)
                }
                r1 += (i/255)
            }
        }
        if (run1 == 1)
        {
            if (g1 >= 1.0){
                
                run1 = 2
            }else {
                if (b1 != 0){
                b1 -= (i/255)
                }
                g1 += (i/255)
            }
        }
        if (run1 == 2)
        {
            if (b1 >= 1.0){
                
                run1 = 0
            }else {
                if (r1 != 0){
                r1 -= (i/255)
                }
                b1 += (i/255)
            }
        }
    }
    func colorRGBi3(i:Float){
        if (run3 == 0)
        {
            if (r3 >= 1.0){
                run3 = 1
            }
            else {
                if (g3 != 0){
                g3 -= (i/255)
                }
                r3 += (i/255)
            }
        }
        if (run3 == 1)
        {
            if (g3 >= 1.0){
                
                run3 = 2
            }else {
                if (b3 != 0){
                b3 -= (i/255)
                }
                g3 += (i/255)
            }
        }
        if (run3 == 2)
        {
            if (b3 >= 1.0){
                
                run3 = 0
            }else {
                if (r3 != 0){
                r3 -= (i/255)
                }
                b3 += (i/255)
            }
        }
    }
    func colorRGBi5(i:Float){
        if (run5 == 0)
        {
            if (r5 >= 1.0){
                run5 = 1
            }
            else {
                if (g5 != 0){
                g5 -= (i/255)
                }
                r5 += (i/255)
            }
        }
        if (run5 == 1)
        {
            if (g5 >= 1.0){
                
                run5 = 2
            }else {
                if (b5 != 0){
                b5 -= (i/255)
                }
                g5 += (i/255)
            }
        }
        if (run5 == 2)
        {
            if (b5 >= 1.0){
                
                run5 = 0
            }else {
                if (r5 != 0){
                r5 -= (i/255)
                }
                b5 += (i/255)
            }
        }
    }
    func colorRGBi17(i:Float){
        if (run17 == 0)
        {
            if (r17 >= 1.0){
                run17 = 1
            }
            else {
                if (g17 != 0){
                g17 -= (i/255)
                }
                r17 += (i/255)
            }
        }
        if (run17 == 1)
        {
            if (g17 >= 1.0){
                
                run17 = 2
            }else {
                if (b17 != 0){
                b17 -= (i/255)
                }
                g17 += (i/255)
            }
        }
        if (run17 == 2)
        {
            if (b17 >= 1.0){
                
                run17 = 0
            }else {
                if (r17 != 0){
                r17 -= (i/255)
                }
                b17 += (i/255)
            }
        }
    }
    func colorRGBi100(i:Float){
        if (run255 == 0)
        {
            if (r100 >= 1.0){
                run255 = 1
            }
            else {
                if (g100 != 0){
                g100 -= (i/255)
                }
                r100 += (i/255)
            }
        }
        if (run255 == 1)
        {
            if (g100 >= 1.0){
                
                run255 = 2
            }else {
                if (b100 != 0){
                b100 -= (i/255)
                }
                g100 += (i/255)
            }
        }
        if (run255 == 2)
        {
            if (b100 >= 1.0){
                
                run255 = 0
            }else {
                if (r100 != 0){
                r100 -= (i/255)
                }
                b100 += (i/255)
            }
        }
    }
    func colorch(){
        DispatchQueue.main.async {
            self.numColor.backgroundColor = UIColor(red: CGFloat(self.r1), green: CGFloat(self.g1), blue: CGFloat(self.b1), alpha: 1)
            self.num3.backgroundColor = UIColor(red: CGFloat(self.r3), green: CGFloat(self.g3), blue: CGFloat(self.b3), alpha: 1)
            self.RealmButton.backgroundColor = UIColor(red: CGFloat(self.r5), green: CGFloat(self.g5), blue: CGFloat(self.b5), alpha: 1)
            self.num5.backgroundColor = UIColor(red: CGFloat(self.r17), green: CGFloat(self.g17), blue: CGFloat(self.b17), alpha: 1)
            self.Alert.backgroundColor = UIColor(red: CGFloat(self.r100), green: CGFloat(self.g100), blue: CGFloat(self.b100), alpha: 1)
        }
           
    }

要注意的是,UI元件只能在主要執行緒上改變,否則會報錯


上一篇
從零開始-30日練習開發iOS APP-CAShapeLayer + UILabel 進度圈(三) Day-26
下一篇
從零開始-30日練習開發iOS APP-UserDefault Day-28
系列文
從零開始-30日練習開發iOS APP30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言