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滑动 改变顶部视图颜色 - 简书

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 接下来我们看Base文件夹下的UIKIt文件夹的内容。 1.UIColor+YYAdd 这里看了这个类,里面有许多...
    充满活力的早晨阅读 6,854评论 0 1
  • 前言: 以下内容是作者在实际开发中所总结的,主要列举了一些实用小技巧,也希望在实际开发中能够帮到你。 设置控件的圆...
    暗香有独阅读 5,196评论 6 33
  • 1、禁止手机睡眠 [UIApplication sharedApplication].idleTimerDisab...
    小小夕舞阅读 5,377评论 1 1
  • 7月,从一开始就阴雨连绵, 难道是要我一起多愁善感吗? 以前品红楼中的黛玉,她那病殃殃的身子,终日不见好转,实在无...
    细思笃行阅读 1,326评论 0 3
  • JavaScript: Window Location window.location 对象用于获得当前页面的地址...
    明明德撩码阅读 1,241评论 0 0