iosswiftimageviewblink

How to make UIImageView blink/flash animation infinite?


I'm trying to achieve a blinking/flashing effect with some duration for a UIImageView in my iOS app. I want the UIImageView to blink/flash with alpha, not with different images.

I have another code-snippet from an android app which sort of describe what I'm trying to achieve in XCode Swift. I post it here below:

This is what I have achieved so far, in XCode Swift:

        ellipses.alpha=0.8
        ellipses.animationDuration=1
        ellipses.animationRepeatCount=0
        ellipses.startAnimating()

And of course, it's not working! I'm not really sure on how to achieve this, and would be very happy to get some guidance.

This code below is for android, not for Swift/Xcode (But it describes what my goal is pretty clear)

        Animation animation = new AlphaAnimation(1, 0); //to change visibility from visible to invisible
        animation.setDuration(1000); //1 second duration for each animation cycle
        animation.setInterpolator(new LinearInterpolator());
        animation.setRepeatCount(Animation.INFINITE); //repeating indefinitely
        animation.setRepeatMode(Animation.REVERSE); //animation will start from end point once ended.
        imageButton.startAnimation(animation); //to start animation

Solution

  • Use the following code to get image animation like a blink.

    Image Blink (Using image view hide/show):-

    override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.alarmAlertActivate), userInfo: nil, repeats: true)
        }
    
        @objc func alarmAlertActivate(){
            myImage.isHidden = !myImage.isHidden
        }
    

    enter image description here

    Image Blink (Using image view alpha):-

    override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            Timer.scheduledTimer(timeInterval: 0.7, target: self, selector: #selector(self.alarmAlertActivate), userInfo: nil, repeats: true)
        }
    
    @objc func alarmAlertActivate(){
            UIView.animate(withDuration: 0.7) {
                self.alarmImage.alpha = self.alarmImage.alpha == 1.0 ? 0.0 : 1.0
            }
        }
    

    enter image description here