How to make a button flash or blink?

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

    Another smoothly animating version for Swift 5:

     public extension UIView {
      func blink(duration: TimeInterval) {
        let initialAlpha: CGFloat = 1
        let finalAlpha: CGFloat = 0.2
        
        alpha = initialAlpha
        
        UIView.animateKeyframes(withDuration: duration, delay: 0, options: .beginFromCurrentState) {
          UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 0.5) {
            self.alpha = finalAlpha
          }
          
          UIView.addKeyframe(withRelativeStartTime: 0.5, relativeDuration: 0.5) {
            self.alpha = initialAlpha
          }
        }
      }
    }
    

提交回复
热议问题