Logo Questions Linux Laravel Mysql Ubuntu Git Menu

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
                                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.

like image 553
Abdul Salam Ali Avatar asked Nov 06 '18 12:11

Abdul Salam Ali

People also ask

What is animation speed in iOS?

Before we start off with topics related to iOS, let us take a brief look at animation speed. Generally in videos, each frame is represented by an image and the frame rate determines the number of images flipped in the sequence. This is termed as ‘frames per second’ or FPS.

What is a UIView and how do I use it?

We will do a quick walk through each of them, explaining their feature set with a relevant example. UIView is the base class for any view that displays content in iOS apps. UIKit, the framework that gives us UIView, already provides us some basic animation functions which make it convenient for developers to achieve more by doing less.

What are some examples of interactive and interruptible animations?

The classic ‘Slide to Unlock’ gesture and the player view dismiss/ expand animation (in the Music app) are examples of interactive and interruptible animations. You can start moving a view with your finger, then release it and the view will go back to its original position.

What is the difference between UIKit and Core Animation?

Any UIKit based animation is internally translated into core animations. Thus, the Core Animation framework acts as a backing layer or backbone for any UIKit animation. Hence, all UIKit animation APIs are nothing but encapsulated layers of the core animation APIs in an easily consumable or convenient fashion.

Video Answer

2 Answers

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)
like image 107
Muneeb Ali Avatar answered Nov 14 '22 23:11

Muneeb Ali

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
                            options:[.allowUserInteraction, .curveEaseInOut,    .autoreverse, .repeat],
                            animations: { 

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

     self.alpha = 0 
                            completion: nil)
like image 33
E.Coms Avatar answered Nov 15 '22 00:11