Vishal-Singh-Panwar / InfiniteScrolling

Add infinite scrolling to collection view.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Scrolling through code

xMonty opened this issue · comments

commented

Is it possible to scroll the collection view using code, for a long time just like casino slots?

I will try to find out a way to create effect that you are looking for. Meanwhile, have you tried using iCarousel ? It has a property autoscroll which could solve your purpose.

Same Issue, I took a day long to try to add such a thing ! 😢

Hi guys,
So I was checking about this over this weekend. I think this is something that can roughly be achieved without help of this component, so I think I should not make this feature as part of this component until I have a concrete solution. Meanwhile, you can try below code on you UICollectionView:

//speed of 10 would mean 10pts/second
    @objc public func autoScroll(withSpeed speed: CGFloat) {
        self.speed = speed
        startScrolling()
    }

    @objc private func startScrolling() {
        if (speed > 0) {
            collectionView.layer.removeAllAnimations()
            UIView.animate(withDuration: TimeInterval(1/speed), delay: 0, options: [.beginFromCurrentState, .allowUserInteraction, .curveLinear]
                , animations: {
                    // Check for scrolling direction.
                    if (scrollingDirection == .horizontal) {
                        self.collectionView.setContentOffset(CGPoint(x: self.collectionView.contentOffset.x + 1, y: self.collectionView.contentOffset.y), animated: false)
                    } else if (scrollingDirection == .vertical) {
                        self.collectionView.setContentOffset(CGPoint(x: self.collectionView.contentOffset.x, y: self.collectionView.contentOffset.y + 1), animated: false)
                    }
            })
           // Calling this method again at 70% completion of the animation so the next animation 
           // appear in continuation.
            perform(#selector(ViewController.startScrolling), with: nil, afterDelay: TimeInterval((1/speed) * 0.7))
        }
    }

It may not work very well for all values of speed. You might see some fluctuations with higher value of speed. In the sample included, I started seeing fluctuations at speed above 800. Try it out and let me know if it works.