Skip to content

Instantly share code, notes, and snippets.

@farnscosnippet
Forked from anonymous/UIViewExtensions.swift
Last active January 8, 2018 21:21
Show Gist options
  • Save farnscosnippet/986b70b18de4d0a518fd80faa4ffae11 to your computer and use it in GitHub Desktop.
Save farnscosnippet/986b70b18de4d0a518fd80faa4ffae11 to your computer and use it in GitHub Desktop.
SWIFT: Extend UIView to add fadeIn() and fadeOut() functions
import Foundation
import UIKit
extension UIView {
func fadeIn(_ duration: TimeInterval = 1.0, delay: TimeInterval = 0.0, completion: @escaping ((Bool) -> Void) = {(finished: Bool) -> Void in}) {
UIView.animate(withDuration: duration, delay: delay, options: UIViewAnimationOptions.curveEaseIn, animations: {
self.alpha = 1.0
}, completion: completion) }
func fadeOut(_ duration: TimeInterval = 1.0, delay: TimeInterval = 0.0, completion: @escaping (Bool) -> Void = {(finished: Bool) -> Void in}) {
UIView.animate(withDuration: duration, delay: delay, options: UIViewAnimationOptions.curveEaseIn, animations: {
self.alpha = 0.0
}, completion: completion)
}
}
// Usage
// self.label.fadeOut(completion: {
// (finished: Bool) -> Void in
// self.label.text = "Replacement text"
// self.label.fadeIn()
// })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment