• Collection view auto scroll Data Like (Slider)
  • Collection view scroll automatically next page every 3.0 seconds
  1. First Outlet Of CollectionView 

@IBOutlet weak var cvWeeklyDeals: UICollectionView!

2. creating Timer to call every 3.0 seconds call to method

func startTimer() {

        let timer =  Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(self.scrollNextPage), userInfo: nil, repeats: true)

    }

3. Called startTimer() to viewDidLoad()

override func viewDidLoad() {

        super.viewDidLoad()

                startTimer()

    }

4. Every 3.0 Second called scrollNextPage() Methods

@objc func scrollNextPage(_ timer1: Timer) {

           if let cvView  = cvWeeklyDeals {

            for cell in cvView.visibleCells {

                let indexPath: IndexPath? = cvView.indexPath(for: cell)

                if ((indexPath?.row)!  <  arrWeeklyDeal.count – 1){

                    let indexPath1: IndexPath?

                    indexPath1 = IndexPath.init(row: (indexPath?.row)! + 1, section: (indexPath?.section)!)

                    cvView.scrollToItem(at: indexPath1!, at: .right, animated: true)

                }

                else{

                    let indexPath1: IndexPath?

                    indexPath1 = IndexPath.init(row: 0, section: (indexPath?.section)!)

                    cvView.scrollToItem(at: indexPath1!, at: .left, animated: false)

                }

            }

        }

    }

You may also like

Leave a Reply