Gifu/source/UIImageView+Gifu.swift

54 lines
1.1 KiB
Swift
Raw Normal View History

2014-09-06 14:12:09 +00:00
import UIKit
extension UIImageView {
// MARK: - Computed Properties
var animatableImage: AnimatedImage? {
if image is AnimatedImage {
return image as? AnimatedImage
} else {
return nil
}
}
var isAnimating: Bool {
return animatableImage?.isAnimating() ?? false
}
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 startAnimating() {
if animatable {
2014-09-11 21:06:16 +00:00
animatableImage!.resumeAnimation()
2014-09-06 14:12:09 +00:00
}
}
func stopAnimating() {
if animatable {
2014-09-11 21:06:16 +00:00
animatableImage!.pauseAnimation()
2014-09-06 14:12:09 +00:00
}
}
}