I have a horizontal UIScrollView
that has a width of 960
, enough to hold 3 UIViewController
view's.
Each view simply has a background color. The first is pink, the second is blue, and the third is green.
I want to blend/mix/fade the visible colors together as the user scrolls.
So if you were scrolling from page 1 (pink) to page 2 (blue), there would be some type of mix/blend/fade of pink and blue before finally landing on the final blue color when the user fully swiped to the 2nd page.
I found a question about exactly what I'm trying to do, and I have implemented the answer which can be found here: https://stackoverflow.com/a/26159561/3344977
This answer does work, my only problem is this answer was only created to use 2 screens/colors, and I have 3 screens/colors.
I understand the basics of this answer and that it depends on the UIScrollView's contentOffset.x
to calculate the current color, but other than that I am terrible at math which is holding me back from figuring out how to modify this to use a third color.
Here's the answer for any quantity of colors (or pages in UICollectionView in my case) with Swift 4.2:
The answer Fogmeister posted works perfectly!
Here's the same answer translated to Swift:
Yes you can definitely use this for three colours (or more).
For three colours (say red, green, blue) you will have red at 0.0, green at 0.5 and blue at 1.0. So you just have to split off the method between the two fades (red-green and green-blue will be calculated separately).
Using my code from before you get the method...
This will fade nicely between all three colours.
You can add more colours to this and change how it splits up the percentages.