I am looking to make a label fade in, in viewDidLoad()
, and then after a timer is at 3 fade out. I am not familiar with the fadein
or fadeout
functions.
How would I go about doing this?
I am looking to make a label fade in, in viewDidLoad()
, and then after a timer is at 3 fade out. I am not familiar with the fadein
or fadeout
functions.
How would I go about doing this?
Even though the view has loaded, it may not be visible to the user when
viewDidLoad
is called. This means you might find your animations appears to have already started when you witness it. To overcome this issue, you'll want to start your animation inviewDidAppear
instead.As for the
fadeIn
andfadeOut
functions - they don't exist. You'll need to write them yourself. Thankfully, it's very easy to do this. Something like the below might be good enough.SWIFT 4.2
Answer updated for Swift 3 - Using extension
Usage:
My suggestion is to leverage Swift extensions to make the code a bit more modular and easy to use. For example, if you want to make multiple labels fadein/out or one label to fade in/out on multiple views, you would have to pass the animateWithDuration methods everywhere, which can be a hassle. A cleaner alternative is to create a file called UIView.swift (our UIView extension). Add the following code to this file:
Now you can add fadeIn/fadeout functionality to any child of UIView (e.g., UILabel, UIImage, etc). Inside of your viewDidLoad() function, you can add :
Now, you can use this sample code for Image views, labels, text views, scroll views, or any child of UIView as well. I hope this helps.
Swift 5
This worked well for me. I placed the label inside the "cardHeaderView".
Place this inside the "viewDidAppear". I went with a delay of zero so the animation would start right away.
Here is the function.
Update Swift 3.1 - on @Andy code