-
Notifications
You must be signed in to change notification settings - Fork 44
Size animation
Evgenii Neumerzhitckii edited this page Aug 13, 2016
·
6 revisions
Auk uses Auto Layout to position the scroll view content. The following view controller code animates the scroll view during device orientation change.
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
guard let pageIndex = scrollView.auk.currentPageIndex else { return }
let newScrollViewWidth = size.width // Assuming scroll view occupies 100% of the screen width
coordinator.animate(alongsideTransition: { [weak self] _ in
self?.scrollView.auk.scrollToPage(atIndex: pageIndex, pageWidth: newScrollViewWidth, animated: false)
}, completion: nil)
}
override func willRotate(to toInterfaceOrientation: UIInterfaceOrientation,
duration: TimeInterval) {
super.willRotate(to: toInterfaceOrientation, duration: duration)
var screenWidth = UIScreen.main.bounds.height
if UIInterfaceOrientationIsPortrait(toInterfaceOrientation) {
screenWidth = UIScreen.main.bounds.width
}
guard let pageIndex = scrollView.auk.currentPageIndex else { return }
scrollView.auk.scrollToPage(atIndex: pageIndex, pageWidth: screenWidth, animated: false)
}
override func viewWillTransitionToSize(size: CGSize,
withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
let pageIndex = scrollView.auk.currentPageIndex
let newScrollViewWidth = size.width // Assuming scroll view occupies 100% of the screen width
coordinator.animateAlongsideTransition({ [weak self] _ in
self?.scrollView.auk.scrollTo(pageIndex, pageWidth: newScrollViewWidth, animated: false)
}, completion: nil)
}
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation,
duration: NSTimeInterval) {
super.willRotateToInterfaceOrientation(toInterfaceOrientation, duration: duration)
var screenWidth = UIScreen.mainScreen().bounds.height
if UIInterfaceOrientationIsPortrait(toInterfaceOrientation) {
screenWidth = UIScreen.mainScreen().bounds.width
}
let pageIndex = scrollView.auk.currentPageIndex
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(0.01 * Double(NSEC_PER_SEC)))
dispatch_after(time, dispatch_get_main_queue()) {
self.scrollView.auk.scrollTo(pageIndex, pageWidth: screenWidth, animated: false)
}
}