Login required for access
In order to watch this lesson, you'll need to login to the website.
How to Implement Skip and Next Buttons
Audible Login Walkthrough
Onto one of our last episodes for the Audible Login series. Today, let's go over implementing the behavior that handles the Skip and Next buttons at the top of the screen. We will be using UICollectionView's scrollToIndexPath method to handle the transition from page to page. I'll also go over how you can refactor the code to avoid redundancy which will definitely help you in the long run. The last step is to use our nextPage function to also implement the skipping behavior.
!codebreak <div class='filename'>ViewController.swift</div> !codebreak !syntax-highlight class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout { //... func skip() { // we only need to lines to do this pageControl.currentPage = pages.count - 1 nextPage() } lazy var nextButton: UIButton = { let button = UIButton(type: .system) button.setTitle("Next", for: .normal) button.setTitleColor(UIColor(red: 247/255, green: 154/255, blue: 27/255, alpha: 1), for: .normal) button.addTarget(self, action: #selector(nextPage), for: .touchUpInside) return button }() func nextPage() { //we are on the last page if pageControl.currentPage == pages.count { return } //second last page if pageControl.currentPage == pages.count - 1 { moveControlConstraintsOffScreen() UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: .curveEaseOut, animations: { self.view.layoutIfNeeded() }, completion: nil) } let indexPath = IndexPath(item: pageControl.currentPage + 1, section: 0) collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true) pageControl.currentPage += 1 } //... func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) { //... //we are on the last page if pageNumber == pages.count { moveControlConstraintsOffScreen() } else { //... } //... } fileprivate func moveControlConstraintsOffScreen() { pageControlBottomAnchor?.constant = 40 skipButtonTopAnchor?.constant = -40 nextButtonTopAnchor?.constant = -40 } //... }

Comments (0)
HELP & SUPPORT