根据UIScrollView的contentOffset在颜色之间切换

use*_*977 4 uiscrollview ios contentoffset

我的水平UIScrollView宽度960足以容纳3个UIViewController视图.

每个视图都有一个背景颜色.第一个是粉红色,第二个是蓝色,第三个是绿色.

我希望在用户滚动时将可见颜色混合/混合/淡化.

因此,如果您从第1页(粉红色)滚动到第2页(蓝色),当用户完全刷到第2页时,最终会落在最终的蓝色上之前会有某种类型的粉色和蓝色混合/混合/淡入淡出.

我发现了一个关于我正在尝试做什么的问题,我已经实现了答案,可以在这里找到:https://stackoverflow.com/a/26159561/3344977

这个答案确实有效,我唯一的问题是这个答案只是为了使用2个屏幕/颜色,我有3个屏幕/颜色.

我理解这个答案的基础知识,它取决于UIScrollView contentOffset.x来计算当前的颜色,但除此之外,我在数学方面很糟糕,这让我不知道如何修改它以使用第三种颜色.

use*_*977 6

Fogmeister发布的答案完美无缺!

以下是转换为Swift的相同答案:

// this just gets the percentage offset.
// 0,0 = no scroll
// 1,1 = maximum scroll

func scrollViewdidScrollToPercentageOffset(scrollView: UIScrollView, percentageOffset: CGPoint) {
    // get your colours to fade between
    var colors = [UIColor.redColor(), UIColor.blueColor(), UIColor.greenColor()]

    // choose the colours to fade between based on the percentage.
    if (percentageOffset.x < 0.5) {
        // multiply the offset by 2 because we want 0.5 to be 100%
        scrollView.backgroundColor = fadeFromColor(colors[0], colors[1], percentageOffset.x*2)
    } else {
        // minus 0.5 because we want 0.5 to be 0%
        scrollView.backgroundColor = fadeFromColor(colors[1], colors[2], (percentageOffset.x - 0.5)*2)
    }
}

func fadeFromColor(fromColor: UIColor, toColor: UIColor, withPercentage: CGFloat) -> UIColor {
    var fromRed: CGFloat = 0.0
    var fromGreen: CGFloat = 0.0
    var fromBlue: CGFloat = 0.0
    var fromAlpha: CGFloat = 0.0

    fromColor.getRed(&fromRed, green: &fromGreen, blue: &fromBlue, alpha: &fromAlpha)

    var toRed: CGFloat = 0.0
    var toGreen: CGFloat = 0.0
    var toBlue: CGFloat = 0.0
    var toAlpha: CGFloat = 0.0

    toColor.getRed(&toRed, green: &toGreen, blue: &toBlue, alpha: &toAlpha)

    //calculate the actual RGBA values of the fade colour
    var red = (toRed - fromRed) * withPercentage + fromRed
    var green = (toGreen - fromGreen) * withPercentage + fromGreen
    var blue = (toBlue - fromBlue) * withPercentage + fromBlue
    var alpha = (toAlpha - fromAlpha) * withPercentage + fromAlpha

    // return the fade colour
    return UIColor(red: red, green: green, blue: blue, alpha: alpha)
}
Run Code Online (Sandbox Code Playgroud)


Fog*_*ter 5

是的,您绝对可以将它用于三种颜色(或更多)。

对于三种颜色(例如红色、绿色、蓝色),红色为 0.0,绿色为 0.5,蓝色为 1.0。所以你只需要在两个淡入淡出之间拆分方法(红-绿和绿-蓝将分别计算)。

在你获得方法之前使用我的代码......

// this just gets the percentage offset.
// 0,0 = no scroll
// 1,1 = maximum scroll
- (void)scrollView:(UIScrollView *)scrollView didScrollToPercentageOffset:(CGPoint)percentageOffset
{
    // get your colours to fade between
    NSArray *colours = @[[UIColor redColor], [UIColor yellowColor], [UIColor purpleColor]];

    // choose the colours to fade between based on the percentage.
    if (percentageOffset.x < 0.5) {
        // multiply the offset by 2 because we want 0.5 to be 100%
        self.backgroundColor = [self fadeFromColor:colours[0] toColor:colours[1] withPercentage:percentageOffset.x*2];
    } else {
        // minus 0.5 because we want 0.5 to be 0%
        self.backgroundColor = [self fadeFromColor:colours[1] toColor:colours[2] withPercentage:(percentageOffset.x-0.5)*2];
    }
}

// this is a more generic method to fade between two colours
// it allows the colours to be passed in as parameters
- (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];
}
Run Code Online (Sandbox Code Playgroud)

这将在所有三种颜色之间很好地褪色。

您可以为此添加更多颜色并更改其拆分百分比的方式。