6

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.

2 Answers2

11

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)
       }
 }
Muneeb Ali
  • 472
  • 6
  • 14
  • Suppose I wanted to remove the flash before the numberOfFlashes completes. Would that be, `layer.removeAllAnimations()`? Or `layer.removeAnimation(forKey:"opacity")`? – Victor Engel Aug 24 '20 at 15:18
  • Trying both out, only the first worked. I noticed that there is no forKeyPath: option like in the way the animation was set up and using forKey: as in my comment, did not work. – Victor Engel Aug 26 '20 at 15:42
  • There is an option of `forKeyPath:` it is not working because somehow animation with that key is not in the list. You can check active animations from layer.animationKeys() – Muneeb Ali Aug 27 '20 at 05:52
  • What I'm saying is that the compiler stated there was no signature with `forKeyPath:`. I'll try again when I next have a chance. Maybe it's one of those things that restarting Xcode fixes. :) – Victor Engel Aug 27 '20 at 15:37
  • I don't see an option to remove an animation with a `keyPath` in the documentation here. https://developer.apple.com/documentation/quartzcore/calayer – Victor Engel Aug 27 '20 at 15:44
  • This seems to be what is required. Instead of `layer.add(flash, forKey: nil)` use `layer.add(flash, forKey: "myKey")`. Then to remove the animation, don't try to find the opacity keyPath. Instead, `layer.removeAnimation(forKey:"myKey")`. BTW, active animations with nil keys are apparently not listed despite being active, so it's probably a good idea to always set a key. – Victor Engel Aug 27 '20 at 16:39
1

There is a builtin in class function for the count and call it in the block.

class func setAnimationRepeatCount(_ repeatCount: Float)

  func startBlink() {
              UIView.animate(withDuration: 0.8,//Time duration
                            delay:0.0,
                            options:[.allowUserInteraction, .curveEaseInOut,    .autoreverse, .repeat],
                            animations: { 

       UIView.setAnimationRepeatCount(30) // repeat 30 times.

     self.alpha = 0 
       },
                            completion: nil)
    }
E.Coms
  • 11,065
  • 2
  • 23
  • 35