     scrollView.decelerationRate = UIScrollViewDecelerationRateFast
 
     addViews()
+  }
+  
+  /// Animate scroll view on orientation change
+  override func viewWillTransitionToSize(size: CGSize,
+    withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
+      
+    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)
+    
+    // Preserve the current page during orientation change
+      
+    let pageIndexBeforeRotation = currentPageIndex
+    
+    coordinator.animateAlongsideTransition({ [weak self] _ in
+      self?.scrollToPage(pageIndexBeforeRotation)
+    }, completion: nil)
+  }
+  
+  private func scrollToPage(page: Int) {
+    let newContentOffsetX = calculateOffsetBasedOnPage(page)
+    let newContentOffset: CGPoint = CGPoint(x: newContentOffsetX, y: 0)
+    scrollView.setContentOffset(newContentOffset, animated: false)
   }
   
   /// Add all the subviews to the scroll view

  func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint,
    targetContentOffset: UnsafeMutablePointer<CGPoint>) {
      
    let page = calculatePageBasedOnOffset(targetContentOffset.memory.x)
    var correctedOffset = calculateOffsetBasedOnPage(Int(round(page)))
      
    if isLastPage(Int(round(page))) {
      // Workaround a bug that prevents scrolling to the last page from the second to last page
      correctedOffset -= 2
    }

    return aSpacer.bounds.width
  }
  
  /// Returns the index of the current page
  var currentPageIndex: Int {
    return calculatePageIndexBasedOnOffset(scrollView.contentOffset.x)
  }
  
  /// Returns the page index for a given scroll view offset.
  func calculatePageIndexBasedOnOffset(var offset: CGFloat) -> Int {
    offset -= spacerWidth
    return Int(round(offset / subviewWidth))
  }
  
  /// Returns the offset needed to display the given page
