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

Scrolling through code #3

Open
xMonty opened this issue Jun 18, 2017 · 3 comments
Open

Scrolling through code #3

xMonty opened this issue Jun 18, 2017 · 3 comments

Comments

@xMonty
Copy link

xMonty commented Jun 18, 2017

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

@Vishal-Singh-Panwar
Copy link
Owner

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.

@shaibow
Copy link

shaibow commented Jun 25, 2017

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

@Vishal-Singh-Panwar
Copy link
Owner

Vishal-Singh-Panwar commented Aug 12, 2017

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants