How to make a button flash or blink?

前端 未结 12 1179
不思量自难忘°
不思量自难忘° 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:06

    You can try something like this:

    extension UIView {
        func blink() {
            UIView.animateWithDuration(0.5, //Time duration you want,
                delay: 0.0,
                options: [.CurveEaseInOut, .Autoreverse, .Repeat],
                animations: { [weak self] in self?.alpha = 0.0 },
                completion: { [weak self] _ in self?.alpha = 1.0 })
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW,Int64(2 * NSEC_PER_SEC)),dispatch_get_main_queue()){
                [weak self] in
                self?.layer.removeAllAnimations()
            }
        }
    }
    

提交回复
热议问题