How to make a button flash or blink?

前端 未结 12 1055
不思量自难忘°
不思量自难忘° 2021-02-01 06:17

I am trying to change a button\'s color (just a flash/blink) to green when a scan is correct and red when there\'s a problem. I am able to do this with a view like so

         


        
12条回答
  •  醉酒成梦
    2021-02-01 07:07

    //MARK : Usage 
    
    yourButton.flash()
    
    extension UIButton {
    
        func flash() {
    
            let flash = CABasicAnimation(keyPath: "opacity")
            flash.duration = 0.5
            flash.fromValue = 1
            flash.toValue = 0.1
            flash.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
            flash.autoreverses = true
            flash.repeatCount = 3
    
            layer.add(flash, forKey: nil)
        }
    }
    

提交回复
热议问题