2014-09-06 14:12:09 +00:00
|
|
|
import UIKit
|
|
|
|
|
2014-12-08 22:11:24 +00:00
|
|
|
public extension UIImageView {
|
2014-09-06 14:12:09 +00:00
|
|
|
// MARK: - Computed Properties
|
|
|
|
var animatableImage: AnimatedImage? {
|
|
|
|
if image is AnimatedImage {
|
|
|
|
return image as? AnimatedImage
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-10 21:57:37 +00:00
|
|
|
var isAnimatingGIF: Bool {
|
|
|
|
return animatableImage?.isAnimating() ?? isAnimating()
|
2014-09-06 14:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var animatable: Bool {
|
|
|
|
return animatableImage != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Method Overrides
|
|
|
|
override public func displayLayer(layer: CALayer!) {
|
|
|
|
if let image = animatableImage {
|
|
|
|
if let frame = image.currentFrame {
|
|
|
|
layer.contents = frame.CGImage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-10 19:12:54 +00:00
|
|
|
// MARK: - Setter Methods
|
2014-09-07 20:35:11 +00:00
|
|
|
func setAnimatableImage(named name: String) {
|
2014-09-06 14:12:09 +00:00
|
|
|
image = AnimatedImage.imageWithName(name, delegate: self)
|
2014-09-11 21:06:16 +00:00
|
|
|
layer.setNeedsDisplay()
|
2014-09-06 14:12:09 +00:00
|
|
|
}
|
|
|
|
|
2014-09-07 20:35:11 +00:00
|
|
|
func setAnimatableImage(#data: NSData) {
|
2014-09-06 14:12:09 +00:00
|
|
|
image = AnimatedImage.imageWithData(data, delegate: self)
|
2014-09-11 21:06:16 +00:00
|
|
|
layer.setNeedsDisplay()
|
2014-09-06 14:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Animation
|
2014-11-10 21:57:37 +00:00
|
|
|
func startAnimatingGIF() {
|
2014-09-06 14:12:09 +00:00
|
|
|
if animatable {
|
2014-09-11 21:06:16 +00:00
|
|
|
animatableImage!.resumeAnimation()
|
2014-11-10 21:57:37 +00:00
|
|
|
} else {
|
|
|
|
startAnimating()
|
2014-09-06 14:12:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-10 21:57:37 +00:00
|
|
|
func stopAnimatingGIF() {
|
2014-09-06 14:12:09 +00:00
|
|
|
if animatable {
|
2014-09-11 21:06:16 +00:00
|
|
|
animatableImage!.pauseAnimation()
|
2014-11-10 21:57:37 +00:00
|
|
|
} else {
|
|
|
|
stopAnimating()
|
2014-09-06 14:12:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|