Use computed variables when possible

This commit is contained in:
Reda Lemeden 2016-10-06 22:49:31 +02:00
parent d3bc2e0c27
commit 22397e9474
2 changed files with 33 additions and 26 deletions

View File

@ -51,11 +51,10 @@ public class Animator {
} }
} }
/// Prepares the animator instance for animation. /// Prepares the animator instance for animation.
/// ///
/// - parameter imageName: The file name of the GIF in the main bundle. /// - parameter imageName: The file name of the GIF in the main bundle.
/// - parameter size: The target size of the individual frames. /// - parameter size: The target size of the individual frames.
/// - parameter contentMode: The view content mode to use for the individual frames. /// - parameter contentMode: The view content mode to use for the individual frames.
func prepareForAnimation(withGIFNamed imageName: String, size: CGSize, contentMode: UIViewContentMode) { func prepareForAnimation(withGIFNamed imageName: String, size: CGSize, contentMode: UIViewContentMode) {
guard let extensionRemoved = imageName.components(separatedBy: ".")[safe: 0], guard let extensionRemoved = imageName.components(separatedBy: ".")[safe: 0],
@ -67,8 +66,8 @@ public class Animator {
/// Prepares the animator instance for animation. /// Prepares the animator instance for animation.
/// ///
/// - parameter imageData: GIF image data. /// - parameter imageData: GIF image data.
/// - parameter size: The target size of the individual frames. /// - parameter size: The target size of the individual frames.
/// - parameter contentMode: The view content mode to use for the individual frames. /// - parameter contentMode: The view content mode to use for the individual frames.
func prepareForAnimation(withGIFData imageData: Data, size: CGSize, contentMode: UIViewContentMode) { func prepareForAnimation(withGIFData imageData: Data, size: CGSize, contentMode: UIViewContentMode) {
frameStore = FrameStore(data: imageData, size: size, contentMode: contentMode, framePreloadCount: frameBufferCount) frameStore = FrameStore(data: imageData, size: size, contentMode: contentMode, framePreloadCount: frameBufferCount)
@ -103,8 +102,8 @@ public class Animator {
/// Prepare for animation and start animating immediately. /// Prepare for animation and start animating immediately.
/// ///
/// - parameter imageName: The file name of the GIF in the main bundle. /// - parameter imageName: The file name of the GIF in the main bundle.
/// - parameter size: The target size of the individual frames. /// - parameter size: The target size of the individual frames.
/// - parameter contentMode: The view content mode to use for the individual frames. /// - parameter contentMode: The view content mode to use for the individual frames.
func animate(withGIFNamed imageName: String, size: CGSize, contentMode: UIViewContentMode) { func animate(withGIFNamed imageName: String, size: CGSize, contentMode: UIViewContentMode) {
prepareForAnimation(withGIFNamed: imageName, size: size, contentMode: contentMode) prepareForAnimation(withGIFNamed: imageName, size: size, contentMode: contentMode)
@ -113,11 +112,11 @@ public class Animator {
/// Prepare for animation and start animating immediately. /// Prepare for animation and start animating immediately.
/// ///
/// - parameter imageData: GIF image data. /// - parameter imageData: GIF image data.
/// - parameter size: The target size of the individual frames. /// - parameter size: The target size of the individual frames.
/// - parameter contentMode: The view content mode to use for the individual frames. /// - parameter contentMode: The view content mode to use for the individual frames.
func animate(withGIFData data: Data, size: CGSize, contentMode: UIViewContentMode) { func animate(withGIFData imageData: Data, size: CGSize, contentMode: UIViewContentMode) {
prepareForAnimation(withGIFData: data, size: size, contentMode: contentMode) prepareForAnimation(withGIFData: imageData, size: size, contentMode: contentMode)
startAnimating() startAnimating()
} }

View File

@ -23,11 +23,14 @@ extension GIFAnimatable {
return image?.size ?? CGSize.zero return image?.size ?? CGSize.zero
} }
/// Prepare for animation and start animating immediately. /// Returns the active frame if available.
/// public var activeFrame: UIImage? {
/// - parameter imageName: The file name of the GIF in the main bundle. return animator?.activeFrame()
public func animate(withGIFNamed imageName: String) { }
animator?.animate(withGIFNamed: imageName, size: frame.size, contentMode: contentMode)
/// Total frame count of the GIF.
public var frameCount: Int {
return animator?.frameCount ?? 0
} }
/// Introspect whether the instance is animating. /// Introspect whether the instance is animating.
@ -35,6 +38,20 @@ extension GIFAnimatable {
return animator?.isAnimating ?? false return animator?.isAnimating ?? false
} }
/// Prepare for animation and start animating immediately.
///
/// - parameter imageName: The file name of the GIF in the main bundle.
public func animate(withGIFNamed imageName: String) {
animator?.animate(withGIFNamed: imageName, size: frame.size, contentMode: contentMode)
}
/// Prepare for animation and start animating immediately.
///
/// - parameter imageData: GIF image data.
public func animate(withGIFData imageData: Data) {
animator?.animate(withGIFData: imageData, size: frame.size, contentMode: contentMode)
}
/// Prepares the animator instance for animation. /// Prepares the animator instance for animation.
/// ///
/// - parameter imageName: The file name of the GIF in the main bundle. /// - parameter imageName: The file name of the GIF in the main bundle.
@ -50,10 +67,6 @@ extension GIFAnimatable {
animator?.prepareForAnimation(withGIFData: imageData, size: frame.size, contentMode: contentMode) animator?.prepareForAnimation(withGIFData: imageData, size: frame.size, contentMode: contentMode)
} }
/// Total frame count of the GIF.
public var frameCount: Int {
return animator?.frameCount ?? 0
}
/// Stop animating and free up GIF data from memory. /// Stop animating and free up GIF data from memory.
public func prepareForReuse() { public func prepareForReuse() {
@ -74,16 +87,11 @@ extension GIFAnimatable {
public func updateImageIfNeeded() { public func updateImageIfNeeded() {
image = animator?.activeFrame() ?? image image = animator?.activeFrame() ?? image
} }
/// Returns the active frame if available.
public func activeFrame() -> UIImage? {
return animator?.activeFrame()
}
} }
extension GIFAnimatable { extension GIFAnimatable {
/// Calls setNeedsDisplay on the layer whenever the animator has a new frame. Should *not* be called directly. /// Calls setNeedsDisplay on the layer whenever the animator has a new frame. Should *not* be called directly.
public func animatorHasNewFrame() { func animatorHasNewFrame() {
layer.setNeedsDisplay() layer.setNeedsDisplay()
} }
} }