美文网首页
iOS 根据滑动值实现颜色渐变效果

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

作者: 蛋上有皱纹 | 来源:发表于2019-10-18 14:37 被阅读0次

    网上搜了有关于iOS根据scrollview滑动值实现颜色渐变效果但没有搜出相关资料,希望本篇对大家的开发有所帮助。

    这个是参考网易考拉banner的滑动效果模仿的,看了安卓的实现跟着写的,其实很简单。

    代码:

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

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

        NSRangerange;

        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 根据滑动值实现颜色渐变效果

          本文链接:https://www.haomeiwen.com/subject/orpimctx.html