From 9b6df99126b2d0531ae07c8c0aa16fc7f93dc904 Mon Sep 17 00:00:00 2001 From: "Pfeil, Scott Robert" Date: Tue, 19 Mar 2019 13:12:09 -0400 Subject: [PATCH] gif view to swift --- .../Atoms/Views/MFTransparentGIFView.swift | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 MVMCoreUI/Atoms/Views/MFTransparentGIFView.swift diff --git a/MVMCoreUI/Atoms/Views/MFTransparentGIFView.swift b/MVMCoreUI/Atoms/Views/MFTransparentGIFView.swift new file mode 100644 index 00000000..3556dcb4 --- /dev/null +++ b/MVMCoreUI/Atoms/Views/MFTransparentGIFView.swift @@ -0,0 +1,67 @@ +// +// MFTransparentGifView.swift +// MVMCoreUI +// +// Created by Scott Pfeil on 3/19/19. +// Copyright © 2019 Verizon Wireless. All rights reserved. +// + +import UIKit + +@objcMembers public class MFTransparentGifView: FLAnimatedImageView { + var imageData: Data? + + /** Creates the GIF display view with the passed in frame. + frame: frame to set the view to. + ImageName: name of the .gif to load. Should not contain the extension. + StartImmediately: should the gif immeidately begin playing. If YES, it will start. If NO, call [performAnimations] to start it. + Duration: how long the animation takes to loop. Pass a negative value to use the default. + LoopCompletionBlock: a block called whenever the gif finishes a loop. + animatedImage : set as nil when use this view in reusable cell + */ + init(withFrame frame: CGRect, imageName: String, startImmediately: Bool, duration: TimeInterval, loopCompletionBlock: ((UInt) -> Void)?) { + super.init(frame: frame) + loadImage(imageName: imageName, startImmediately: startImmediately, duration: duration, loopCompletionBlock: loopCompletionBlock) + } + + required init?(coder aDecoder: NSCoder) { + super.init(coder: aDecoder) + } + + func loadImage(imageName: String, startImmediately: Bool, duration: TimeInterval, loopCompletionBlock: ((UInt) -> Void)?) { + contentMode = UIView.ContentMode.scaleAspectFill + clipsToBounds = true + + if duration >= 0 { + animationDuration = duration + } + self.loopCompletionBlock = loopCompletionBlock + backgroundColor = .clear + + var url: URL? + if imageName.contains("http") { + url = URL(string: imageName) + } else { + url = MVMCoreUIUtility.bundleForMVMCoreUI()?.url(forResource: imageName, withExtension: "gif") + } + imageData = MFFreebeeHandler.shared()?.freebee_data(withContentsOf: url) + + runLoopMode = RunLoop.Mode.common.rawValue + if startImmediately { + performAnimations() + } + } + + func loadGifWithData(imageData: Data) { + contentMode = UIView.ContentMode.scaleAspectFill + clipsToBounds = true + backgroundColor = .clear + self.imageData = imageData + runLoopMode = RunLoop.Mode.common.rawValue + performAnimations() + } + + func performAnimations() { + animatedImage = FLAnimatedImage(animatedGIFData: imageData, optimalFrameCacheSize: 250, predrawingEnabled: true) + } +}