Here is some code I struggle with for a while.
If you start the fade in animation, the label text fades in. If I start the fade out animation the the label text fades ou
Generic answer : You can use this method to apply animation to any UIView object . First create an extension of UIView class . Create a separate swift file and write the code like this
import Foundation
import UIKit
extension UIView {
func fadeIn() {
//Swift 2
UIView.animateWithDuration(1.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseIn, animations: {
self.alpha = 1.0
}, completion: nil)
//Swift 3, 4, 5
UIView.animate(withDuration: 1.0, delay: 0.0, options: UIView.AnimationOptions.curveEaseIn, animations: {
self.alpha = 1.0
}, completion: nil)
}
func fadeOut() {
//Swift 2
UIView.animateWithDuration(1.0, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.alpha = 0.0
}, completion: nil)
//Swift 3, 4, 5
UIView.animate(withDuration: 1.0, delay: 0.0, options: UIView.AnimationOptions.curveEaseOut, animations: {
self.alpha = 0.0
}, completion: nil)
}
}
Here self refers to any UIView you refer to . You can use buttons, labels etc to call these 2 methods .
Then in any other swift class you can call fadeIn() and fadeOut() like this :
self.yourUIObject.fadeIn()
self.yourUIObject.fadeOut()
This gives the desired effect of animation to any UIObject .