Skip to content

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.

iOS 8+

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)
}

iOS 7

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)
}

Swift 2.2 version

iOS 8+

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)
}

iOS 7

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)
  }
}
Clone this wiki locally