iOS 根据滑动值实现颜色渐变效果

网上搜了有关于iOS根据scrollview滑动值实现导航栏颜色渐变效果但没有搜出相关结果,所以参照了安卓的实现,希望本篇对大家的开发有所帮助。

详细代码:iOS 根据Banner滑动 改变顶部视图颜色 -

代码:

```Objective-C

+ (NSArray*)getRGBWithColor:(NSString*)color {

    // 从六位数值中找到RGB对应的位数并转换

    NSRange range;

    range.location=0;

    range.length=2;

    //R、G、B

    NSString*rString = [colorsubstringWithRange:range];

    range.location=2;

    NSString*gString = [colorsubstringWithRange:range];

    range.location=4;

    NSString*bString = [colorsubstringWithRange:range];

    // Scan values

    unsignedintr, g, b;

    [[NSScanner scannerWithString:rString] scanHexInt:&r];

    [[NSScanner scannerWithString:gString] scanHexInt:&g];

    [[NSScanner scannerWithString:bString] scanHexInt:&b];

    return@[@(r),@(g),@(b)];

}

+ (NSArray*)getRGBWithFromColor:(NSString*)fromColor toColor:(NSString*)toColor shade:(CGFloat)mShade {

    NSArray*fromeRgb = [SPUtils getRGBWithColor:fromColor];

    NSArray*toRgb = [SPUtils getRGBWithColor:toColor];

    intfromR = [fromeRgb[0]intValue];

    intfromG = [fromeRgb[1]intValue];

    intfromB = [fromeRgb[2]intValue];


    inttoR = [toRgb[0]intValue];

    inttoG = [toRgb[1]intValue];

    inttoB = [toRgb[2]intValue];


    intdiffR = toR - fromR;

    intdiffG = toG - fromG;

    intdiffB = toB - fromB;


    intred = fromR + (int) ((diffR * mShade));

    intgreen = fromG + (int) ((diffG * mShade));

    intblue = fromB + (int) ((diffB * mShade));


    return@[@(red /255.0f),@(green /255.0f),@(blue /255.0f)];

}

```


mShade是你的scrollview的滑动值,+ (NSArray*)getRGBWithFromColor:(NSString*)fromColor toColor:(NSString*)toColor shade:(CGFloat)mShade; 方法得到的是NSNumber类型,需要转成CGFloat类型


        CGFloat red = [rgb[0] floatValue];

        CGFloat green = [rgb[1] floatValue];

        CGFloat blue = [rgb[2] floatValue];

        UIColor *bgColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];


iOS 根据Banner滑动 改变顶部视图颜色 -

你可能感兴趣的:(iOS 根据滑动值实现颜色渐变效果)