Skip to content

Instantly share code, notes, and snippets.

@Th3nion
Created September 13, 2017 12:14
Show Gist options
  • Save Th3nion/d1d569e3b51f3203b1da601c9a2968c0 to your computer and use it in GitHub Desktop.
Save Th3nion/d1d569e3b51f3203b1da601c9a2968c0 to your computer and use it in GitHub Desktop.
//
// UIImage+replaceImage.swift
//
// Created by mtournier on 26/10/16.
// NO Copyright !
//
import Foundation
import UIKit
extension UIImageView {
/// Change the image inside ImageView with a fade animation
///
/// - Parameters:
/// - image: the new image to display
/// - animated: activate fade or not
func replaceImage(_ image:UIImage, animated:Bool) {
if animated {
let animationDuration:TimeInterval = 0.25
CATransaction.begin()
CATransaction.setAnimationDuration(animationDuration)
let transition = CATransition()
transition.type = kCATransitionFade
self.layer.add(transition, forKey: kCATransition)
self.image = image
CATransaction.commit()
} else {
self.image = image
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment