gif view to swift
This commit is contained in:
parent
33510a6484
commit
9b6df99126
67
MVMCoreUI/Atoms/Views/MFTransparentGIFView.swift
Normal file
67
MVMCoreUI/Atoms/Views/MFTransparentGIFView.swift
Normal file
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user