Gifu/Source/UIImageView+Gifu.swift

58 lines
1.2 KiB
Swift
Raw Normal View History

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
}
}
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
func startAnimatingGIF() {
2014-09-06 14:12:09 +00:00
if animatable {
2014-09-11 21:06:16 +00:00
animatableImage!.resumeAnimation()
} else {
startAnimating()
2014-09-06 14:12:09 +00:00
}
}
func stopAnimatingGIF() {
2014-09-06 14:12:09 +00:00
if animatable {
2014-09-11 21:06:16 +00:00
animatableImage!.pauseAnimation()
} else {
stopAnimating()
2014-09-06 14:12:09 +00:00
}
}
}