CollectionView move to next cell automatically swift

后端 未结 11 2195
耶瑟儿~
耶瑟儿~ 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:41

    For Swift4

    override func viewDidLoad() {
        super.viewDidLoad()
    
        startTimer()
    }
    
    
    
    func startTimer() {
    
        let timer =  Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.scrollAutomatically), userInfo: nil, repeats: true)
    }
    
    
    @objc func scrollAutomatically(_ timer1: Timer) {
    
        if let coll  = topMenuCollection {
            for cell in coll.visibleCells {
                let indexPath: IndexPath? = coll.indexPath(for: cell)
                if ((indexPath?.row)! < banner.count - 1){
                    let indexPath1: IndexPath?
                    indexPath1 = IndexPath.init(row: (indexPath?.row)! + 1, section: (indexPath?.section)!)
    
                    coll.scrollToItem(at: indexPath1!, at: .right, animated: true)
                }
                else{
                    let indexPath1: IndexPath?
                    indexPath1 = IndexPath.init(row: 0, section: (indexPath?.section)!)
                    coll.scrollToItem(at: indexPath1!, at: .left, animated: true)
                }
    
            }
        }
    }
    

    topMenuCollection :- your collection view

    banner.Count:- a number of cells containing a collection view

提交回复
热议问题