Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved user experience while scrolling #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions UPCarouselFlowLayoutDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ class ViewController: UIViewController, UICollectionViewDelegate, UICollectionVi

fileprivate var currentPage: Int = 0 {
didSet {
let character = self.items[self.currentPage]
self.infoLabel.text = character.name.uppercased()
self.detailLabel.text = character.movie.uppercased()
if items.indices.contains(currentPage) {
let character = self.items[self.currentPage]
self.infoLabel.text = character.name.uppercased()
self.detailLabel.text = character.movie.uppercased()
}
}
}

Expand Down Expand Up @@ -108,11 +110,12 @@ class ViewController: UIViewController, UICollectionViewDelegate, UICollectionVi

// MARK: - UIScrollViewDelegate

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let layout = self.collectionView.collectionViewLayout as! UPCarouselFlowLayout
let pageSide = (layout.scrollDirection == .horizontal) ? self.pageSize.width : self.pageSize.height
let offset = (layout.scrollDirection == .horizontal) ? scrollView.contentOffset.x : scrollView.contentOffset.y
currentPage = Int(floor((offset - pageSide / 2) / pageSide) + 1)
let currentPage = Int(floor((offset - pageSide / 2) / pageSide) + 1)
self.currentPage = currentPage
}

}
Expand Down