問題描述
我的應(yīng)用有一個入門部分,其中包含 4 個頁面,用戶可以水平滾動以了解如何使用該應(yīng)用(標(biāo)準(zhǔn)).我希望當(dāng)用戶從一個頁面滾動到另一個頁面時背景顏色會發(fā)生變化.
I have an onboarding section of my app with 4 pages the users scrolls through horizontally to get an idea of how to use the app (standard). I want the background color to transition as the user scrolls from page to page.
我想要使用 4 個 RGB 值:
I have the 4 RGB values I want to use:
241,170,170
241,170,170
170,201,241
170,201,241
188,170,241
188,170,241
241,199,170
241,199,170
我知道我必須使用滾動視圖委托 + 內(nèi)容偏移來更改 uicolor 值,但我不確定如何讓它轉(zhuǎn)到我選擇的特定顏色.
I know I must use the scroll view delegate + content offset to change the uicolor values, but im not sure how I get it to go to the specific colors ive selected.
任何幫助將不勝感激.任何實(shí)現(xiàn)都可以,快速或客觀-c
Any help would be greatly appreciated. Any implementation would do, swift or objective-c
謝謝
推薦答案
對于任何有興趣的人.這就是解決方案.我結(jié)合了我在堆棧上找到的一些答案并將其調(diào)整為使用 4 種顏色
For anyone interested. This is the solution. I combined some answers I found on stack and adapted it to use 4 colors
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat pageWidth = self.scrollView.frame.size.width;
float fractionalPage = self.scrollView.contentOffset.x / pageWidth;
NSInteger page = lround(fractionalPage);
self.pageControl.currentPage = page;
// horizontal
CGFloat maximumHorizontalOffset = scrollView.contentSize.width - CGRectGetWidth(scrollView.frame);
CGFloat currentHorizontalOffset = scrollView.contentOffset.x;
// percentages
CGFloat percentageHorizontalOffset = currentHorizontalOffset / maximumHorizontalOffset;
NSLog(@"content offfset: %f", percentageHorizontalOffset);
if (percentageHorizontalOffset < 0.333333) {
self.view.backgroundColor = [self fadeFromColor:self.colorArray[0] toColor:self.colorArray[1] withPercentage:percentageHorizontalOffset*3];
} else if (percentageHorizontalOffset >= 0.333333 && percentageHorizontalOffset < 0.666667) {
self.view.backgroundColor = [self fadeFromColor:self.colorArray[1] toColor:self.colorArray[2] withPercentage:(percentageHorizontalOffset-0.333333)*3];
} else if (percentageHorizontalOffset >= 0.666667) {
self.view.backgroundColor = [self fadeFromColor:self.colorArray[2] toColor:self.colorArray[3] withPercentage:(percentageHorizontalOffset-0.666667)*3];
}
}
- (UIColor *)fadeFromColor:(UIColor *)fromColor toColor:(UIColor *)toColor withPercentage:(CGFloat)percentage
{
// get the RGBA values from the colours
CGFloat fromRed, fromGreen, fromBlue, fromAlpha;
[fromColor getRed:&fromRed green:&fromGreen blue:&fromBlue alpha:&fromAlpha];
CGFloat toRed, toGreen, toBlue, toAlpha;
[toColor getRed:&toRed green:&toGreen blue:&toBlue alpha:&toAlpha];
//calculate the actual RGBA values of the fade colour
CGFloat red = (toRed - fromRed) * percentage + fromRed;
CGFloat green = (toGreen - fromGreen) * percentage + fromGreen;
CGFloat blue = (toBlue - fromBlue) * percentage + fromBlue;
CGFloat alpha = (toAlpha - fromAlpha) * percentage + fromAlpha;
// return the fade colour
return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
這篇關(guān)于在滾動期間更改背景顏色的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!