Gifu/Source/AnimatableImageView.swift

72 lines
2.3 KiB
Swift
Raw Normal View History

2015-01-22 10:54:27 +00:00
import ImageIO
import Runes
2015-01-23 00:02:08 +00:00
import UIKit
2015-01-22 10:54:27 +00:00
2015-01-23 00:02:08 +00:00
/// A subclass of `UIImageView` that can be animated using an image name string or raw data.
2015-01-22 10:54:27 +00:00
public class AnimatableImageView: UIImageView, Animatable {
2015-01-23 00:02:08 +00:00
/// An `Animator` instance that holds the frames of a specific image in memory.
2015-01-22 10:54:27 +00:00
var animator: Animator?
deinit {
println("deinit animatable view")
}
2015-01-23 00:02:08 +00:00
/// A computed property that returns whether the image view is animating.
2015-01-22 10:54:27 +00:00
public var isAnimatingGIF: Bool {
return animator?.isAnimating ?? isAnimating()
}
2015-01-23 00:02:08 +00:00
/// Prepares the frames using a GIF image file name, without starting the animation.
/// The file name should include the `.gif` extension.
///
/// :param: imageName The name of the GIF file. The method looks for the file in the app bundle.
2015-01-22 10:54:27 +00:00
public func prepareForAnimation(imageNamed imageName: String) {
let path = NSBundle.mainBundle().bundlePath.stringByAppendingPathComponent(imageName)
prepareForAnimation <^> NSData(contentsOfFile: path)
}
2015-01-23 00:02:08 +00:00
/// Prepares the frames using raw GIF image data, without starting the animation.
///
/// :param: data GIF image data.
2015-01-22 10:54:27 +00:00
public func prepareForAnimation(imageData data: NSData) {
image = UIImage(data: data)
animator = Animator(data: data, delegate: self)
}
2015-01-23 00:02:08 +00:00
/// Prepares the frames using a GIF image file name and starts animating the image view.
///
/// :param: imageName The name of the GIF file. The method looks for the file in the app bundle.
2015-01-22 10:54:27 +00:00
public func animateWithImage(named imageName: String) {
prepareForAnimation(imageNamed: imageName)
startAnimatingGIF()
}
2015-01-23 00:02:08 +00:00
/// Prepares the frames using raw GIF image data and starts animating the image view.
///
/// :param: data GIF image data.
2015-01-22 10:54:27 +00:00
public func animateWithImageData(#data: NSData) {
prepareForAnimation(imageData: data)
startAnimatingGIF()
}
2015-01-23 00:02:08 +00:00
/// Updates the `UIImage` property of the image view if necessary. This method should not be called manually.
2015-01-22 10:54:27 +00:00
override public func displayLayer(layer: CALayer!) {
2015-02-10 19:07:54 +00:00
image = animator?.currentFrame
2015-01-22 10:54:27 +00:00
}
2015-01-23 00:02:08 +00:00
/// Starts the image view animation.
2015-01-22 10:54:27 +00:00
public func startAnimatingGIF() {
animator?.resumeAnimation() ?? startAnimating()
}
2015-01-23 00:02:08 +00:00
/// Stops the image view animation.
2015-01-22 10:54:27 +00:00
public func stopAnimatingGIF() {
animator?.pauseAnimation() ?? stopAnimating()
}
public func cleanup() {
animator = .None
}
2015-01-22 10:54:27 +00:00
}