Created
February 13, 2018 03:22
-
-
Save KrisYu/fedc74abc07e303130a6a90965e32624 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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