Skip to content

Instantly share code, notes, and snippets.

@KrisYu
Created February 13, 2018 03:22
Show Gist options
  • Save KrisYu/fedc74abc07e303130a6a90965e32624 to your computer and use it in GitHub Desktop.
Save KrisYu/fedc74abc07e303130a6a90965e32624 to your computer and use it in GitHub Desktop.
// 1. 在 Storyboard 中设置RestorationID
// 2. AppDelegate.swift中告诉App我们想要save和restore
func application(_ application: UIApplication, shouldSaveApplicationState coder: NSCoder) -> Bool {
return true
}
func application(_ application: UIApplication, shouldRestoreApplicationState coder: NSCoder) -> Bool {
return true
}
// 3. ViewController.swift
static let IndexRestorationKey = "currentAlbumIndex"
override func encodeRestorableState(with coder: NSCoder) {
coder.encode(currentAlbumIndex, forKey: Constants.IndexRestorationKey)
super.encodeRestorableState(with: coder)
}
override func decodeRestorableState(with coder: NSCoder) {
super.decodeRestorableState(with: coder)
currentAlbumIndex = coder.decodeInteger(forKey: Constants.IndexRestorationKey)
showDataForAlbum(at: currentAlbumIndex)
horizontalScrollerView.reload()
}
// 4. 把 horizontalScrollerView 移动到合适的地方
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
horizontalScrollerView.scrollToView(at: currentAlbumIndex, animated: false)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment