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,7 +51,6 @@ 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.
@ -116,8 +115,8 @@ public class Animator {
/// - 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,6 +23,21 @@ extension GIFAnimatable {
return image?.size ?? CGSize.zero return image?.size ?? CGSize.zero
} }
/// Returns the active frame if available.
public var activeFrame: UIImage? {
return animator?.activeFrame()
}
/// Total frame count of the GIF.
public var frameCount: Int {
return animator?.frameCount ?? 0
}
/// Introspect whether the instance is animating.
public var isAnimatingGIF: Bool {
return animator?.isAnimating ?? false
}
/// 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.
@ -30,9 +45,11 @@ extension GIFAnimatable {
animator?.animate(withGIFNamed: imageName, size: frame.size, contentMode: contentMode) animator?.animate(withGIFNamed: imageName, size: frame.size, contentMode: contentMode)
} }
/// Introspect whether the instance is animating. /// Prepare for animation and start animating immediately.
public var isAnimatingGIF: Bool { ///
return animator?.isAnimating ?? false /// - 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.
@ -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()
} }
} }