Reputation: 227
Hello: I'm trying to create a collectionView with pages with at the top a segmentedControl that changes it's index depending on the current page of the collectionView
Here is the code I use to change the selector:
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offset = scrollView.contentOffset.x
self.selectorView.frame = CGRect(x: offset/3, y: 0, width: self.view.frame.width/3, height: 2)
}
However, this doesn't move selectorView
at all, so I tried moving the process to a background thread:
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offset = scrollView.contentOffset.x
DispatchQueue.main.async {
self.selectorView.frame = CGRect(x: offset/3, y: 0, width: self.view.frame.width/3, height: 2)
}
}
This works, but here's the laggy and buggy result: screen recording on YouTube
Does anyone know how I can fix this?
Upvotes: 1
Views: 202
Reputation: 116
I would set the segmentedControl selectedSegmentIndex based on the current index of the page. Here is a previous topic for how you can calculate the index: Click
The other thing you could do is avoid using a collection view and switch to a page view controller, which has an indicator for the current page, and the ability to scroll to different sections. They can be tricky to configure at first but they're worth it to be able to use, and the design is one users are very familiar with. I know it might not be the exact solution to the current problem, but it would be a way to circumvent the problem.
Upvotes: 1