0

I want my collectionView to start from the first one and 3 seconds later automatically scroll to the second one and 3 seconds later to the 3rd one and so on... How can i do this? The code below works but for only 1 time. It scrolls from the first one to the second one but then it stops.

func scrollToNextCell(){

        let cellSize = CGSize(width: self.view.frame.width, height: self.view.frame.height)
        let contentOffset = myCollectionView.contentOffset;
        myCollectionView.scrollRectToVisible(CGRect(x: contentOffset.x + cellSize.width, y: contentOffset.y, width: cellSize.width, height: cellSize.height), animated: true)
        }

    func startTimer() {

        _ = Timer.scheduledTimer(timeInterval: 3.0,
        target: self,
        selector: #selector(scrollToNextCell),
        userInfo: nil,
        repeats: false)
    }
Volkan Elçi
  • 163
  • 2
  • 13
  • Have you try something ? If yes, share us your code – Jimmy James Dec 15 '16 at 15:44
  • I have just found this answer and i will try right now. http://stackoverflow.com/questions/36588933/collectionview-move-to-next-cell-automatically-swift – Volkan Elçi Dec 15 '16 at 15:49
  • 1
    This kind of "tell me how to implement my app idea" question is not a good fit for SO. You should take a stab at writing the code yourself, and if you have problems, post your code, tell us how it fails, and we'll help you debug it. – Duncan C Dec 15 '16 at 15:51
  • As a freebie, because you're new, your search term is the "Timer" class (You might need to search on "NSTimer", the Objective-C version of the class name.) UICollectionView also has a method `scrollToItem(at:at:animated:)` that you should look at. – Duncan C Dec 15 '16 at 15:53
  • Google for `NSTimer`. If you can not make it, post your code and show us the issue. – shallowThought Dec 15 '16 at 15:53
  • @DuncanC I have edited my question with my code. It scrolls from the 1st one to 2nd after 3 seconds but then it stops. How can i make it repeat until i go to last one? – Volkan Elçi Dec 15 '16 at 16:14
  • marks `repeats: true` instead of `false` – Jimmy James Dec 15 '16 at 16:16
  • Indeed, pass in `repeats: true` as @JimmyJames says, but you'll need logic to detect the end of the collection and stop once you reach the end. – Duncan C Dec 15 '16 at 16:19
  • Thank you very much – Volkan Elçi Dec 15 '16 at 16:25

2 Answers2

-1

I apologize in advance for any typo (I don't have access to my Xcode right now). But something like this could work:

let timerIntervalForResendingCode = TimeInterval(60)
    Timer.scheduledTimer(timeInterval: f3.0,
                     target: self,
                     selector: #selector(timerEndedUp),
                     userInfo: nil,
                     repeats: false)
}

@objc func timerEndedUp() {
     collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Right, animated: false)
}
Alex
  • 995
  • 12
  • 25
-1

Create a timer like this in your viewDidLoad:

var timer = NSTimer()
var currentItem: Int = 0

override func viewDidLoad() {
    self.timer = NSTimer.scheduledTimerWithTimeInterval(3, target:self, selector: Selector("updateCounter"), userInfo: nil, repeats: true)
}

func updateCounter() {
    if currentItem < self.collectionView.numberOfItemInSection(0) {
        self.collection.scrollToItemAtIndexPath(NSIndexPath(forItem: currentItem, inSection: 0), atScrollPosition: UICollectionViewScrollPosition.Top, animated: true) 
        self.currentItem += 1
    } else {
        self.timer.invalidate()
    }
}

I didn't try this code, but it's an example

Jimmy James
  • 825
  • 12
  • 28