//
//  UIButton.swift
//  Astro
//
//  Created by Jeremy Wiebe on 2015-06-11.
//  Copyright (c) 2015 Mobify Research & Development Inc. All rights reserved.
//

import Foundation

extension UIButton {
    @objc func crossfadeToNewImage(_ image: UIImage, duration: TimeInterval = 0.4) {
        if let imageView = self.imageView, let oldImage = imageView.image {
            let crossFade = CABasicAnimation(keyPath: "contents")
            crossFade.duration = 0.4
            crossFade.fromValue = oldImage.cgImage
            crossFade.toValue = image.cgImage
            crossFade.fillMode = CAMediaTimingFillMode.forwards
            imageView.layer.add(crossFade, forKey: "animateContents")
        }

        self.setImage(image, for: .normal)
        self.sizeToFit()
    }
}
