Show UIImageView for 1 second and hide again

Hello. I am trying to show an image for a short time, in an animated way, and hidden again. This is the function:

@IBOutlet var statusField: UIImageView!
@IBAction func debugbutton(_ sender: UIButton) {
        showIcon()
}
override func viewDidLoad() {
        super.viewDidLoad()
        statusField.isHidden = true

func showIcon() {
        statusField.isHidden = false
        UIView.animate(withDuration: 1, delay: 0.5, options: UIView.AnimationOptions.transitionFlipFromTop, animations: {
            self.statusField.alpha = 0
        }, completion: { finished in
            self.statusField.isHidden = true
        })
    }

When I run the function for the first time it works fine but then it stops doing it. The icon is no longer displayed. Any advice on what's going on? Thanks!

  • Hello, just to clarify, you wanted to make the UIImageView flicker at 1 second interval, or just show it for 1 second and hide once?

  • Just show it for 1 second and hide once! Thanks!

Add a Comment

Accepted Reply

In your animation, you set alpha to 0, but there's no code to reset it to 1.

    func showIcon() {
        statusField.isHidden = false
        UIView.animate(withDuration: 1, delay: 0.5, options: UIView.AnimationOptions.transitionFlipFromTop, animations: {
            self.statusField.alpha = 0
        }, completion: { finished in
            self.statusField.isHidden = true
            self.statusField.alpha = 1 //<-
        })
    }

In your shown code, showIcon() looks like it is nested inside viewDidLoad(), but it is odd and I assumed it is not nested.

Replies

In your animation, you set alpha to 0, but there's no code to reset it to 1.

    func showIcon() {
        statusField.isHidden = false
        UIView.animate(withDuration: 1, delay: 0.5, options: UIView.AnimationOptions.transitionFlipFromTop, animations: {
            self.statusField.alpha = 0
        }, completion: { finished in
            self.statusField.isHidden = true
            self.statusField.alpha = 1 //<-
        })
    }

In your shown code, showIcon() looks like it is nested inside viewDidLoad(), but it is odd and I assumed it is not nested.

It worked perfect, thank you very much. You are correct, the function was not nested, I just forgot to close viewDidLoad() when I was copying the code in the forum.

Besides knowing that it works, I would like to understand well how the code works. When I execute the function, the button appears (hidden = false) and then I have an animation that in a certain time (1s) varies the alpha from 1 to 0, upon completion it returns the value to 1 (thanks to you) and maintains I hide the image ( hidden = true ). It is right?

When I execute the function, the button appears (hidden = false) and then I have an animation that in a certain time (1s) varies the alpha from 1 to 0, upon completion it returns the value to 1 (thanks to you) and maintains I hide the image ( hidden = true ). It is right?

Before you call, image is hidden and alpha is 1 (by default as defined in IB probably)

Before animation, you unhide : image is visible Then animation is to change progressively alpha from 1 to 0 ; hidden remains false At the end of animation, you hide the image again. and you need to reset alpha to 1, otherwise next time you play you start at 0

You could also have written it at start:

    func showIcon() {
        statusField.isHidden = false
        statusField.alpha = 1 //<-
  • Thanks for the explanation Claude!

Add a Comment