I am trying to incorporate a page system in my app. With these pages, I want buttons to appear or disappear depending on what page the user is currently on. With the method I am currently using, only the very first if statement works properly. If I were to press the next button slowly all the way to the end, the condition fails. However, If I were to rapidly press the next button, it works just fine. Same thing if I wanted to go back to current page == 0. If I slowly press, it fails. If I were to rapidly press it success.
How my logic looks is as follows.
struct Pages {
let imageName: String
let headerText: String
let bodyText: String
}
let pages1 = [
Pages(imageName: "image", headerText: "header text", bodyText: "body text"),
Pages(imageName: "image", headerText: "header text", bodyText: "body text"),
Pages(imageName: "image", headerText: "header text", bodyText: "body text"),
Pages(imageName: "image", headerText: "header text", bodyText: "body text"),
Pages(imageName: "image", headerText: "header text", bodyText: "body text"),
Pages(imageName: "image", headerText: "header text", bodyText: "body text.")
]
func setUpPageControlShouldNotShow() {
if self.PageControl.currentPage == 0 {
previousButton.setTitleColor(.clear, for: .normal)
previousButton.isUserInteractionEnabled = false
} else if PageControl.currentPage < 5 {
TakeALookButton.setTitleColor(.clear, for: .normal)
TakeALookButton.isUserInteractionEnabled = false
} else if PageControl.currentPage == 5 {
nextButton.setTitleColor(.clear, for: .normal)
nextButton.isUserInteractionEnabled = false
}
}
And I am calling this function inside of cellForItemAt which looks like.
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
setUpPageControlShouldNotShow()
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! Cell
let page = pages1[indexPath.item]
cell.page1 = page
return cell
}
lazy var PageControl: UIPageControl = {
let pc = UIPageControl()
pc.currentPage = 0
pc.numberOfPages = pages1.count
pc.currentPageIndicatorTintColor = GREEN_Theme
pc.pageIndicatorTintColor = UIColor(red: 249/255, green: 207/255, blue: 224/255, alpha: 1)
return pc
}()
// updated this is how my current page is being changed.
@objc private func handleNext() {
let nextIndex = min(PageControl.currentPage + 1, pages1.count - 1)
let indexPath = IndexPath(item: nextIndex, section: 0)
PageControl.currentPage = nextIndex
collectionView?.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
}
am I calling in the right place? Ive tried to use less logic, but it does not function properly from the start. This was the closest I've gotten to function properly.