iosuiviewswift4xcode9.4

iOS blink animation on UIView with finite number of times


I am trying to create a blinking effect on the UIView. Currently I am using a code which blinks the UIView with infinite number of times. the Code looks like this

WhatI have done so far:

 func startBlink() {
                  UIView.animate(withDuration: 0.8,//Time duration
                                delay:0.0,
                                options:[.allowUserInteraction, .curveEaseInOut, .autoreverse, .repeat],
                                animations: { self.alpha = 0 },
                                completion: nil)
        }

But this code blinks the ui view for infinite number of time. I used another code but that was blinking for one time only.

What I want:

So I am pretty close but I really want to blink the UIView for finite number of times i.e 30 times, and it must stop after 30th blink.

Please help me in this, I think I have clear in my question. Please help me out.


Solution

  • Use this function to animate View. I hope it can help

    extension UIView {  
            func flash(numberOfFlashes: Float) {
               let flash = CABasicAnimation(keyPath: "opacity")
               flash.duration = 0.2
               flash.fromValue = 1
               flash.toValue = 0.1
               flash.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
               flash.autoreverses = true
               flash.repeatCount = numberOfFlashes
               layer.add(flash, forKey: nil)
           }
     }