John Smith Optional
John Smith Optional

Reputation: 491

UIButton animation sequence in Swift

I have this UIButton extension (I'm using it to animate my buttons):

func pulsate() {

        let pulse = CASpringAnimation(keyPath: "transform.scale")
        pulse.duration = 0.3
        pulse.fromValue = 0.95
        pulse.toValue = 1.05
        pulse.autoreverses = true
        pulse.repeatCount = 3
        pulse.initialVelocity = 0.5
        pulse.damping = 1.0

        layer.add(pulse, forKey: "pulse")
    }

I'm trying to use it to make animation sequence with this func... :

private func onInactivityDetected() {
       for i buttonsOutlet.indices {
            buttonsOutlet[i].flash()
        }
     }

... and it works but instead to make a sequence all of the buttons are flashing in the same time.

So, i'm trying to figure out a way to makes them flash in a sequence, like button1, button2, button3 etc. and am not sure if i have to use something like completion handler or some other method? Can anyone help me with this, please?

Upvotes: 0

Views: 70

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

Can you try

private func onInactivityDetected() {

        for i buttonsOutlet.indices {

           DispatchQueue.main.asyncAfter(deadline: .now() + 0.3 * Double(i) ) {

            self.buttonsOutlet[i].flash()
         }
      }
 }

Upvotes: 1

Related Questions