Reputation: 17591
I have a scrollView with 4 page; I want to change it's background color at every page; then I want to know index page value. For example, if I have page 1, I want to know that I have index 1 then background must be red or if I have page 3, I want to know that I have index 3 and background color must be blue.
then I want to do an "if" where I control if my current page have index 1, or index 2, or index 3.....
Upvotes: 0
Views: 2298
Reputation: 12590
More fully, you can really only get it like this
let pageCGFloat = scrollView.contentOffset.x / scrollView.bounds.width
let pageIndexInt = pageCGFloat.rounded(.toNearestOrAwayFromZero)
you THEN, to avoid crashes, must ensure
That's about it.
Upvotes: 1
Reputation: 11920
The page index is found by: scrollview.contentOffset.x / scrollview.frame.size.width
.
To set the background color you can either:
There is problem with option one. When transitioning between pages the background colour will be incorrect until the scrolling ends.
Upvotes: 2