CollectionView move to next cell automatically swift

后端 未结 11 2186
耶瑟儿~
耶瑟儿~ 2021-01-31 21:05

I am trying to create horizontal slider like Flipkart. I am using collectionView with Horizontal scrolling and paging. Cell contains imageView. I am succeed in scrolling items h

11条回答
  •  时光说笑
    2021-01-31 21:29

    Swift 3

    This tested code scrolls to the next cell and returns to first item after the last one.

    func setTimer() {
         let _ = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(MainVC.autoScroll), userInfo: nil, repeats: true)
    }
    var x = 1
    @objc func autoScroll() {
        if self.x < self.storiesForCollectionView.count {
          let indexPath = IndexPath(item: x, section: 0)
          self.collectionViewUp.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
          self.x = self.x + 1
        }else{
          self.x = 0
          self.collectionViewUp.scrollToItem(at: IndexPath(item: 0, section: 0), at: .centeredHorizontally, animated: true)
        }
    }
    
    override func viewDidLoad() {
            super.viewDidLoad()
            setTimer()
        }
    

提交回复
热议问题