使按钮在swift中改变颜色

时间:2018-05-10 23:03:13

标签: ios swift uibutton

有谁知道如何让按钮在swift中改变颜色?我有一个矩形的按钮,里面有文字和空心的背景,我希望空心空间每3秒左右改变颜色。任何人都可以提供帮助,我将不胜感激!

2 个答案:

答案 0 :(得分:0)

你能试试吗

 var counter = 0
 var pageChangeTimer: Timer?
 let colors = [UIColor.red,UIColor.green,,,,,,,,,] //,,,, set more

 pageChangeTimer = Timer.scheduledTimer( withTimeInterval: 3.0 , repeats: true) { [weak self] timer in 

    if counter == colors.count {
       counter = 0
    }
     self.btn.backgroundColor = colors[counter]  
    counter += 1      

}

答案 1 :(得分:0)

试试这个

var net = Bool()

override func viewDidLoad() {
    super.viewDidLoad()
    net = true
    var timer = Timer.scheduledTimer(timeInterval: 0.3, target: self, selector: #selector(self.update), userInfo: nil, repeats: true)

}

//更新方法

@objc func update() {
// Something cool
if net == true {
    // changebtn is the outlet of UIButton
    changebtn.backgroundColor = UIColor.blue
    net = false
}else{
    changebtn.backgroundColor = UIColor.black
    net = true
}
}