Gifu/README.md

172 lines
6.0 KiB
Markdown
Raw Normal View History

2016-10-06 22:17:41 +00:00
# ![Logo](https://github.com/kaishin/Gifu/raw/master/header.gif)
2014-09-07 21:48:09 +00:00
2018-01-01 21:28:21 +00:00
[![GitHub release](https://img.shields.io/github/release/kaishin/Gifu.svg)](https://github.com/kaishin/Gifu/releases/latest) ![Bitrise](https://www.bitrise.io/app/a0eae2c9b6aa754c.svg?token=dDlRyPi2aOlZnvbl4kL8Kw&branch=master) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage) ![Swift 4.0](https://img.shields.io/badge/Swift-4.0-orange.svg) ![platforms](https://img.shields.io/badge/platforms-iOS-lightgrey.svg)
2016-01-31 12:00:48 +00:00
Gifu adds protocol-based, performance-aware animated GIF support to UIKit. (It's also a [prefecture in Japan](https://goo.gl/maps/CCeAc)).
2016-09-25 18:12:56 +00:00
## Install
2016-09-25 18:12:56 +00:00
### [Carthage](https://github.com/Carthage/Carthage)
2014-12-08 22:22:46 +00:00
2015-12-02 22:47:47 +00:00
- Add the following to your Cartfile: `github "kaishin/Gifu"`
- Then run `carthage update`
- Follow the current instructions in [Carthage's README][carthage-installation]
for up to date installation instructions.
2014-09-07 21:45:51 +00:00
2015-12-02 22:47:47 +00:00
[carthage-installation]: https://github.com/Carthage/Carthage#adding-frameworks-to-an-application
### [CocoaPods](http://cocoapods.org)
2015-12-02 22:47:47 +00:00
- Add the following to your [Podfile](http://guides.cocoapods.org/using/the-podfile.html): `pod 'Gifu'`
- You will also need to make sure you're opting into using frameworks: `use_frameworks!`
- Then run `pod install` with CocoaPods 0.36 or newer.
2014-12-08 22:22:46 +00:00
## How It Works
2017-01-05 00:19:12 +00:00
`Gifu` does not require using the built-in `GIFImageView` subclass. The `Animator` class does the heavy-lifting, while the `GIFAnimatable` protocol exposes the functionality to the view classes that conform to it, using protocol extensions.
The `Animator` has a `FrameStore` that only keeps a limited number of frames in-memory, effectively creating a buffer for the animation without consuming all the available memory. This approach makes loading large GIFs a lot more resource-friendly.
The figure below summarizes how this works in practice. Given an image
containing 10 frames, Gifu will load the current frame (red), buffer the next two frames in this example (orange), and empty up all the other frames to free up memory (gray):
2017-07-02 22:31:20 +00:00
<img src="https://github.com/kaishin/Gifu/raw/master/gifu-figure.gif" width="300" />
## Usage
2014-09-07 21:45:51 +00:00
There are two options that should cover any situation:
2014-09-07 21:45:51 +00:00
2016-10-08 18:34:32 +00:00
- Use the built-in `GIFImageView` subclass if you don't need to combine GIF support with another image library.
2017-01-05 00:19:12 +00:00
- If you need more flexibility and composability, make your class conform to `GIFAnimatable`. In practice, any `UIView` subclass would do, since you get most of the required properties for free. For best results, make your `UIImageView` subclass conform to `GIFAnimatable` to get access to other features such as intrinsic content size.
2015-12-02 22:32:02 +00:00
### GIFAnimatable
2016-10-08 18:34:32 +00:00
The bread and butter of Gifu. Through protocol extensions, `GIFAnimatable` exposes all the APIs of the library, and with very little boilerplate, any class can conform to it.
~~~swift
class MyImageView: UIImageView, GIFAnimatable {
public lazy var animator: Animator? = {
return Animator(withDelegate: self)
}()
override public func display(_ layer: CALayer) {
updateImageIfNeeded()
}
}
~~~
2016-10-08 18:34:32 +00:00
That's it. Now `MyImageView` has access to all these methods and properties:
- `prepareForAnimation(withGIFNamed:)` and `prepareForAnimation(withGIFData:)` to prepare the animator property for animation.
- `startAnimatingGIF()` and `stopAnimatingGIF()` to control the animation.
- `animate(withGIFNamed:)` and `animate(withGIFData:)` to prepare for animation and start animating immediately.
- `frameCount`, `isAnimatingGIF`, and `activeFrame` to inspect the GIF view.
- `prepareForReuse()` to free up resources.
- `updateImageIfNeeded()` to update the image property if necessary.
2016-10-08 18:34:32 +00:00
Furthermore, you can make any class GIF-animatable, starting with `UIView` subclasses:
~~~swift
2016-10-08 18:34:32 +00:00
class CustomAnimatedView: UIView, GIFAnimatable {
public lazy var animator: Animator? = {
return Animator(withDelegate: self)
}()
override public func display(_ layer: CALayer) {
updateImageIfNeeded()
}
}
~~~
2017-01-05 00:19:12 +00:00
You can also make `UIKit` classes conform using associated objects may you wish:
~~~swift
import UIKit
import Gifu
extension UIImageView: GIFAnimatable {
private struct AssociatedKeys {
static var AnimatorKey = "gifu.animator.key"
}
override open func display(_ layer: CALayer) {
updateImageIfNeeded()
}
public var animator: Animator? {
get {
guard let animator = objc_getAssociatedObject(self, &AssociatedKeys.AnimatorKey) as? Animator else {
let animator = Animator(withDelegate: self)
self.animator = animator
return animator
}
return animator
}
set {
objc_setAssociatedObject(self, &AssociatedKeys.AnimatorKey, newValue as Animator?, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
}
~~~
### Examples
The simplest way to get started is initializing a `GIFAnimatable` class in code or in a storyboard, then calling `animate(:)` on it.
~~~swift
let imageView = GIFImageView(frame: CGRect(x: 0, y: 0, width: 200, height: 100))
2018-01-01 21:28:21 +00:00
imageView.animate(withGIFNamed: "mugen") {
print("It's animating!")
}
~~~
You can also prepare for the animation when the view loads and only start animating after a user interaction.
~~~swift
// In your view controller..
override func viewDidLoad() {
super.viewDidLoad()
2018-01-01 21:28:21 +00:00
imageView.prepareForAnimation(withGIFNamed: "mugen") {
print("Ready to animate!")
}
}
@IBAction func toggleAnimation(_ sender: AnyObject) {
if imageView.isAnimatingGIF {
imageView.stopAnimatingGIF()
} else {
imageView.startAnimatingGIF()
}
}
~~~
If you are using a `GIFAnimatable` class in a table or collection view, you can call the `prepareForReuse()` method in your cell subclass:
~~~swift
override func prepareForReuse() {
super.prepareForReuse()
imageView.prepareForReuse()
}
~~~
### Demo App
2014-11-04 10:28:43 +00:00
Clone or download the repository and open `Gifu.xcworkspace` to check out the demo app.
2014-09-07 22:48:47 +00:00
## Documentation
See the [full API documentation](http://kaishin.github.io/Gifu/).
2014-09-07 22:48:47 +00:00
## Compatibility
2014-09-07 21:45:51 +00:00
- iOS 9.0+
2018-01-01 21:28:21 +00:00
- Swift 4.0
- Xcode 9.0
2014-09-10 19:44:28 +00:00
## License
2014-09-07 21:45:51 +00:00
See LICENSE.