kean / Preheat
- понедельник, 27 июня 2016 г. в 03:13:03
Swift
Automates prefetching of content in UITableView and UICollectionView
Automates preheating (prefetching) of content in UITableView and UICollectionView.
This library is similar to
UITableViewDataSourcePrefetchingandUICollectionViewDataSourcePrefetchingadded in iOS 10
One of the ways to use Preheat is to improve user experience in applications that display collections of images. Preheat allows you to detect which cells are soon going to appear on the display, so that you can precache images for those cells. You can use Preheat with any image loading library, including Nuke which it was designed for.
The idea of automating preheating was inspired by Apple’s Photos framework example app.
Here is an example of how you might implement preheating in your application using Preheat and Nuke:
class PreheatDemoViewController: UICollectionViewController, PreheatControllerDelegate {
var preheatController: PreheatController<UICollectionView>!
override func viewDidLoad() {
super.viewDidLoad()
preheatController = PreheatController(view: collectionView!)
preheatController.handler = { [weak self] in
self?.preheatWindowChanged(addedIndexPaths: $0, removedIndexPaths: $1)
}
}
func preheatWindowChanged(addedIndexPaths added: [NSIndexPath], removedIndexPaths removed: [NSIndexPath]) {
func requestsForIndexPaths(indexPaths: [NSIndexPath]) -> [ImageRequest] {
return indexPaths.map { ImageRequest(photos[$0.row].URL) }
}
Nuke.startPreheatingImages(requestsForIndexPaths(added))
Nuke.stopPreheatingImages(requestsForIndexPaths(removed))
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
preheatController.enabled = true
}
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(animated)
// When you disable preheat controller it removes all preheating
// index paths and calls its handler
preheatController.enabled = false
}
}To install Preheat add a dependency to your Podfile:
# source 'https://github.com/CocoaPods/Specs.git'
# use_frameworks!
# platform :ios, "8.0"
pod "Preheat"To install Preheat add a dependency to your Cartfile:
github "kean/Preheat"
Import installed modules in your source files
import PreheatPreheat is available under the MIT license. See the LICENSE file for more info.