Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在滚动过程中更改背景颜色_Ios_Objective C_Swift_Uiscrollview_Uiscrollviewdelegate - Fatal编程技术网

Ios 在滚动过程中更改背景颜色

Ios 在滚动过程中更改背景颜色,ios,objective-c,swift,uiscrollview,uiscrollviewdelegate,Ios,Objective C,Swift,Uiscrollview,Uiscrollviewdelegate,我的应用程序有一个入职部分,有4个页面,用户可以水平滚动,了解如何使用该应用程序(标准)。我希望在用户从一页滚动到另一页时背景颜色发生变化 我有4个要使用的RGB值: 24170170 170201241 188170241 241199170 我知道我必须使用scroll view delegate+content offset来更改uicolor值,但我不确定如何使其转到我选择的特定颜色 任何帮助都将不胜感激。 任何实施都可以,swift或objective-c 谢谢设置滚动视图委托并使用下

我的应用程序有一个入职部分,有4个页面,用户可以水平滚动,了解如何使用该应用程序(标准)。我希望在用户从一页滚动到另一页时背景颜色发生变化

我有4个要使用的RGB值:

24170170

170201241

188170241

241199170

我知道我必须使用scroll view delegate+content offset来更改uicolor值,但我不确定如何使其转到我选择的特定颜色

任何帮助都将不胜感激。 任何实施都可以,swift或objective-c


谢谢

设置滚动视图委托并使用下面的方法

//This method is called when scroll view ends scrolling
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [self updateColor];
}

要在scrollview中启用分页,必须首先从属性检查器启用scrollview的分页属性

根据你的要求,你有4页。因此,您的scrollview内容大小将类似于scrollviewWidth*4

将此代码放入viewDidLoad

[self.scrollVw setContentSize:CGSizeMake(self.scrollVw.frame.size.width * 4,0)];
然后使用数组存储颜色值,如下所示

 arrColor = [[NSMutableArray alloc]init];

 [arrColor addObject:[UIColor colorWithRed:(241.0f/255.0f) green:(170.0f/255.0f) blue:(170.0f/255.0f) alpha:1.0f]];

 [arrColor addObject:[UIColor colorWithRed:(170.0f/255.0f) green:(201.0f/255.0f) blue:(241/255.0f) alpha:1.0f]];
 [arrColor addObject:[UIColor colorWithRed:(188.0f/255.0f) green:(170.0f/255.0f) blue:(241.0f/255.0f) alpha:1.0f]];
 [arrColor addObject:[UIColor colorWithRed:(241.0f/255.0f) green:(199.0f/255.0f) blue:(170.0f/255.0f) alpha:1.0f]];

 [self.scrollVw setBackgroundColor:[arrColor objectAtIndex:0]]; //this is for first time to display first color when scrollview is load. you can avoid this by directly setting color from UI
Scrollview委托方法代码

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
 [self.scrollVw setBackgroundColor:[arrColor objectAtIndex:currentPage]]; //currentPage overhere is simple int variable which i had incremented and decremented its values based on current offset of scrollview. Also currentPage value should not be > 4.
}

希望这能帮到你。快乐编码:)

您可以根据需要使用代理。如果要在滚动时同时更改背景色,应使用scrollViewDidScroll方法,动态获取内容偏移量并更改背景色。如果你在完成卷轴后想要它,你可以像@Chetan所说的那样为任何感兴趣的人使用。这就是解决办法。我结合了在堆栈上找到的一些答案,并将其调整为使用4种颜色

- (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];
}    
-(无效)scrollViewDidScroll:(UIScrollView*)scrollView{
CGFloat pageWidth=self.scrollView.frame.size.width;
float-partitionlpage=self.scrollView.contentOffset.x/pageWidth;
NSInteger page=lrund(分馏页);
self.pageControl.currentPage=page;
//水平的
CGFloat maximumHorizontalOffset=scrollView.contentSize.width-CGRectGetWidth(scrollView.frame);
CGFloat currentHorizontalOffset=scrollView.contentOffset.x;
//百分比
CGFloat Percentage HorizontalOffset=当前水平偏移/最大水平偏移;
NSLog(@“内容集:%f”,百分比水平偏移量);
如果(百分比水平偏移<0.333){
self.view.backgroundColor=[self-fadeFromColor:self.colorArray[0]到color:self.colorArray[1],带百分比:percentageHorizontalOffset*3];
}否则如果(percentageHorizontalOffset>=0.333333&&percentageHorizontalOffset<0.66666 7){
self.view.backgroundColor=[self-fadeFromColor:self.colorArray[1]到color:self.colorArray[2]以及百分比:(percentageHorizontalOffset-0.333333)*3];
}否则如果(百分比水平偏移量>=0.66666 7){
self.view.backgroundColor=[self-fadeFromColor:self.colorArray[2]到color:self.colorArray[3]以及百分比:(percentageHorizontalOffset-0.66666 7)*3];
}
}
-(UIColor*)fadeFromColor:(UIColor*)从颜色到颜色:(UIColor*)到带有百分比的颜色:(CGFloat)百分比
{
//从颜色中获取RGBA值
CGFloat来自红色、绿色、蓝色、阿尔法;
[fromColor getRed:&fromRed green:&fromGreen blue:&fromBlue alpha:&fromAlpha];
托雷德、托格林、托布鲁、托阿尔法;
[toColor getRed:&toRed green:&toGreen blue:&toBlue alpha:&toAlpha];
//计算褪色颜色的实际RGBA值
CGFloat red=(toRed-fromRed)*百分比+fromRed;
CGFloat绿色=(绿色-绿色)*百分比+绿色;
CGFloat蓝色=(toBlue-fromBlue)*百分比+fromBlue;
CGFloat alpha=(toAlpha-fromAlpha)*百分比+fromAlpha;
//返回淡入淡出的颜色
返回[UIColor COLOR WITHRED:红绿色:绿蓝色:蓝色阿尔法:阿尔法];
}    
var currPage=0
让颜色:[UIColor]=[红色、蓝色、绿色、黄色、紫色、橙色]
func scrollViewDidScroll(scrollView:UIScrollView){
//用于水平滚动
让progress=scrollView.contentOffset.x/scrollView.bounds.width
让page=Int(进度)
如果currPage!=第{currPage=page}
让nextPage=Int(进度+1)
设prog=1-(CGFloat(Int(progress+1))-progress)
打印(“\(当前页)\(下一页)\(进度)”)
如果currPage>=0&&currPage=0&&nextPageUIColor{
设c1=CIColor(颜色:col1)
设c2=CIColor(颜色:col2)
设α=(c2.alpha-c1.alpha)*百分比+c1.alpha
设红色=(c2.red-c1.red)*百分比+c1.red
设蓝色=(c2.blue-c1.blue)*百分比+c1.blue
设绿色=(c2.green-c1.green)*百分比+c1.green
返回UIColor(红色:红色,绿色:绿色,蓝色:蓝色,alpha:alpha)
}

将它们放入数组,滚动时选择随机颜色…感谢您的回复,但这只会在我进入下一页时改变颜色。我期待着从一种颜色过渡到另一种颜色。(就像动画一样,淡入下一种颜色)。当用户滚动时,而不是当他进入下一页时。这正是我想要做的,我正在做类似的事情:你能帮忙吗?当我尝试你的解决方案时,我每次都得到相同的颜色(在Swift中)
    var currPage = 0
    let colors: [UIColor] = [.red, .blue, .green, .yellow, .purple, .orange]

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        // for horizontal scrolling
        let progress = scrollView.contentOffset.x / scrollView.bounds.width
        let page = Int(progress)
        if currPage != page { currPage = page }
        let nextPage = Int(progress + 1)
        let prog = 1 - (CGFloat(Int(progress + 1)) - progress)
        print("\(currPage) \(nextPage) \(prog)")
        if currPage >= 0 && currPage < colors.count && nextPage >= 0 && nextPage < colors.count {
            let interColor = colorBetween(col1: colors[currPage], col2: colors[nextPage], percent: prog)
            scrollView.backgroundColor = interColor.withAlphaComponent(0.5)
        }
    }

    // calculates intermediate color, percent should in between 0.0 - 1.0
    func colorBetween(col1: UIColor, col2: UIColor, percent: CGFloat) -> UIColor {
        let c1 = CIColor(color: col1)
        let c2 = CIColor(color: col2)

        let alpha = (c2.alpha - c1.alpha) * percent + c1.alpha
        let red = (c2.red - c1.red) * percent + c1.red
        let blue = (c2.blue - c1.blue) * percent + c1.blue
        let green = (c2.green - c1.green) * percent + c1.green

        return UIColor(red: red, green: green, blue: blue, alpha: alpha)
    }