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

Give 33 some context #7

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
15 changes: 11 additions & 4 deletions Glideshow/Glideshow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@

import UIKit

/*
Magic number used to prevent the users from reaching the end of the slideshow. This was all for the purpose
of giving the user the "feeling" of an infinitely swiping slideshow to each side, where in reality they aren't.
The slideshow will reset to the initial position when they stop swiping and when the animations stop.
*/
fileprivate let kResetToOrigionalPositionMultiplier = 33

@objc
/// The delegate protocol that notifies slideshow state changes
public protocol GlideshowProtocol : AnyObject {
Expand Down Expand Up @@ -316,7 +323,7 @@ public class Glideshow: UIView {
/// - animated: if `true` it animates the process of jumping
public func jumpToSlide(_ slide : Int, _ animated : Bool = true){
invalidateTimer()
let pageToJump = (33 * items!.count) + slide - 1
let pageToJump = (kResetToOrigionalPositionMultiplier * items!.count) + slide - 1
if slide > items?.count ?? 0 {return}
collectionView?.scrollToItem(at: IndexPath(item: pageToJump, section: 0), at: .right, animated: animated)
setTimerIfNeeded()
Expand Down Expand Up @@ -384,7 +391,7 @@ public class Glideshow: UIView {
func initializeCVPositionIfNeeded(){
collectionView?.layoutIfNeeded()
if items?.count != 0 && items != nil && isCircular{
collectionView?.scrollToItem(at: IndexPath(item: 33 * items!.count, section: 0), at: .right, animated: false)
collectionView?.scrollToItem(at: IndexPath(item: kResetToOrigionalPositionMultiplier * items!.count, section: 0), at: .right, animated: false)
}
}

Expand Down Expand Up @@ -446,10 +453,10 @@ public class Glideshow: UIView {
currentIndex = Int((currentMidSectionCount).rounded())

let realPage = (currentIndex % items!.count)
if (currentIndex < (33 * items!.count) - 5) || (currentIndex > (33 * items!.count) + 5) {
if (currentIndex < (kResetToOrigionalPositionMultiplier * items!.count) - 5) || (currentIndex > (kResetToOrigionalPositionMultiplier * items!.count) + 5) {
collectionView?.scrollToItem(
at: IndexPath(
item: (33 * items!.count ) + realPage,
item: (kResetToOrigionalPositionMultiplier * items!.count ) + realPage,
section: 0
),
at: .centeredVertically,
Expand Down