title: Update Segmented Progress Bar When CollectionView Index is Changed Swift
body: <p><a href="https://vimeo.com/674383395" rel="nofollow noreferrer">Working Example Video</a></p>
  <p>I am trying to create an onboarding view such as Instagram stories with a progress bar on top of the view.</p>
  <p>So far I was able to animate to the second or third index in the given time. This animation also changes stack view progress bar. But when I try to scroll to the next or previous collection view index I can not show this action inside the progress bar.</p>
  <p>I can read the current page index with page control but can not reflect this reading to progress bar.</p>
  <pre><code>class OnboardingViewController: UIViewController, SegmentedProgressBarDelegate {
      
      @IBOutlet weak var collectionView: UICollectionView!
      @IBOutlet weak var stackView: UIStackView!
      
      private var spb: SegmentedProgressBar!
      
      var currentPage = 0
      
      lazy var pageControl: UIPageControl = {
          let pageControl = UIPageControl()
          pageControl.numberOfPages = slides.count
          return pageControl
      }()
      
      //Burası Presenter'dan gelecek.
      var slides: [OnboardingSlide] = [
          OnboardingSlide(title: &quot;Delicious Dishes&quot;, image: #imageLiteral(resourceName: &quot;1&quot;)),
          OnboardingSlide(title: &quot;World-Class Chefs&quot;, image: #imageLiteral(resourceName: &quot;2&quot;)),
          OnboardingSlide(title: &quot;Instant World-Wide Delivery&quot;, image: #imageLiteral(resourceName: &quot;3&quot;))
      ]
      
      override func viewDidLoad() {
          super.viewDidLoad()
          spb = SegmentedProgressBar(numberOfSegments: slides.count, duration: 3)
          spb.frame = CGRect(x: 15, y: 56, width: collectionView.frame.width - 30, height: 4)
          spb.delegate = self
          spb.topColor = UIColor.white
          spb.bottomColor = UIColor.white.withAlphaComponent(0.25)
          spb.padding = 2
          collectionView.delegate = self
          collectionView.dataSource = self
          stackView.addArrangedSubview(spb)
          spb.startAnimation()
          collectionView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(tappedView)))
      }
      
      func segmentedProgressBarChangedIndex(index: Int) {
          updateView(index: index)
      }
      
      override var prefersStatusBarHidden: Bool {
          return true
      }
      
      func segmentedProgressBarFinished() {
          print(&quot;Finished!&quot;)
      }
      
      @objc private func tappedView() {
          spb.isPaused = !spb.isPaused
      }
      
      private func updateView(index: Int) {
          print(&quot;index: \(index)&quot;)
          let indexPath = IndexPath(row: index, section: 0)
          collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
      }
     
      @IBAction func loginButtonTapped(_ sender: UIButton) {
      }
      
      @IBAction func signupButtonTapped(_ sender: UIButton) {
      }
      
  }
  
  extension OnboardingViewController: UICollectionViewDelegate, UICollectionViewDataSource {
      
      func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -&gt; Int {
          return slides.count
      }
      func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
          let cell = collectionView.dequeueReusableCell(withReuseIdentifier: OnboardingCollectionViewCell.identifier, for: indexPath) as! OnboardingCollectionViewCell
          cell.setup(slides[indexPath.row])
          return cell
      }
      
      
  }
  
  extension OnboardingViewController: UICollectionViewDelegateFlowLayout {
      
      func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -&gt; CGSize {
          return CGSize(width: collectionView.frame.width, height: collectionView.frame.height)
      }
      
      func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
          let width = scrollView.frame.width
          currentPage = Int(scrollView.contentOffset.x / width)
          pageControl.currentPage = currentPage
          updateView(index: currentPage)
          print(&quot;current page: \(currentPage)&quot;)
      }
  }
  </code></pre>
  <p>I have used
  <a href="https://github.com/D-32/SegmentedProgressBar" rel="nofollow noreferrer">https://github.com/D-32/SegmentedProgressBar</a></p>
  <p>as the segmentedProgressBar</p>
  <pre><code>class OnboardingCollectionViewCell: UICollectionViewCell {
      
      static let identifier = String(describing: OnboardingCollectionViewCell.self)
      
      @IBOutlet weak var slideImageView: UIImageView!
      @IBOutlet weak var slideTitleLabel: UILabel!
      
      func setup(_ slide: OnboardingSlide) {
          slideImageView.image = slide.image
          slideTitleLabel.text = slide.title
      }
  }
  
  struct OnboardingSlide {
      let title: String
      let image: UIImage
  }
  </code></pre>