美文网首页
iOS 滚动过程中逐渐切换颜色值

iOS 滚动过程中逐渐切换颜色值

作者: 天上飞的狒狒 | 来源:发表于2023-03-06 17:24 被阅读0次

//fromColor开始滚动切换前的颜色(十六进制)
//滚动切换结束后的颜色toColor(十六进制)
//ratio滚动比例(向左滚动逐渐变大)

+ (UIColor *) loadFromColor:(NSString *)fromColor toColor:(NSString *)toColor andRatio:(CGFloat)ratio
{
     if (fromColor == nil && toColor == nil) {
          return nil;
     }
     //滚动切换前颜色
    if (![fromColor containsString:@"#"]) {
         fromColor = [NSString stringWithFormat:@"#%@", fromColor];
    }
    unsigned int fromRed, fromGreen, fromBlue;
    NSRange fromRange;
     fromRange.length = 2;
     fromRange.location = 1;
    [[NSScanner scannerWithString:[fromColor substringWithRange:fromRange]] scanHexInt:&fromRed];
     fromRange.location = 3;
    [[NSScanner scannerWithString:[fromColor substringWithRange:fromRange]] scanHexInt:&fromGreen];
     fromRange.location = 5;
    [[NSScanner scannerWithString:[fromColor substringWithRange:fromRange]] scanHexInt:&fromBlue];

     //滚动切换后颜色
     if (toColor == nil) {
         return nil;
     }
     if (![toColor containsString:@"#"]) {
          toColor = [NSString stringWithFormat:@"#%@", toColor];
     }
     unsigned int toRed, toGreen, toBlue;
     NSRange toRange;
     toRange.length = 2;
     toRange.location = 1;
     [[NSScanner scannerWithString:[toColor substringWithRange:toRange]] scanHexInt:&toRed];
     toRange.location = 3;
     [[NSScanner scannerWithString:[toColor substringWithRange:toRange]] scanHexInt:&toGreen];
     toRange.location = 5;
     [[NSScanner scannerWithString:[toColor substringWithRange:toRange]] scanHexInt:&toBlue];
     
     //渐变颜色
     CGFloat lastRed, lastGreen, lastBlue;
     //处理红色
     //100 ----> 30
     if(fromRed >= toRed){
          lastRed = fromRed - (fromRed - toRed)*ratio;
     } else {
          lastRed = fromRed + (toRed - fromRed)*ratio;
     }
     //处理绿色
     if(fromGreen >= toGreen){
          lastGreen = fromGreen - (fromGreen - toGreen)*ratio;
     } else {
          lastGreen = fromGreen + (toGreen - fromGreen)*ratio;
     }
     //处理蓝色
     if(fromBlue >= toBlue){
          lastBlue = fromBlue - (fromBlue - toBlue)*ratio;
     } else {
          lastBlue = fromBlue + (toBlue - fromBlue)*ratio;
     }
     return [UIColor colorWithRed:(float)(lastRed/255.0f) green:(float)(lastGreen/255.0f) blue:(float)(lastBlue/255.0f) alpha:1.0f];
}

相关文章

  • IOS15之UINavigationBar背景颜色不全

    IOS15之UINavigationBar背景颜色不全 系统升级到ios 15之后,背景颜色,或者背景图片必须滚动...

  • Flutter沉浸式Banner + 滚动修改状态栏颜色

    Flutter沉浸式Banner + 滚动修改状态栏颜色 PS:最近项目有个非人类需求,顶部放轮播广告,滚动切换状...

  • iOS颜色值

    美工一般会给我们出16进制的颜色值,就需要一个方法来实现了 1.我是这么实现的, 给UIColor写个分类, 然后...

  • Android int颜色值和rgba颜色互转

    有的App后台网络颜色值使用的Android的int类型的颜色值。iOS使用RGBA的颜色值。怎么进行转换呢? 注...

  • iOS主题颜色切换

    效果 demo在这里:https://github.com/fengfengaima/ColorChangesFo...

  • 通过反射改变toolbar

    1、改变toolbar的颜色 当滚动条再顶部的时候,toobar透明,再发生滚动的时候,toobar逐渐变成白色,...

  • Android滑动开关控件

    先看效果 点击按钮时切换状态,也可以拖动切换。在滑动过程中,颜色透明度有渐变(本想做颜色渐变,但是对颜色计算不太懂...

  • UIScrollViewDelegate 详解

    //scrollView滚动时,就调用该方法。任何offset值改变都调用该方法。即滚动过程中,调用多次 // 当...

  • 监测ScrollView和TableView是处在开始拖动,上移

    1、 //scrollView滚动时,就调用该方法。任何offset值改变都调用该方法。即滚动过程中,调用多次 -...

  • UIScrollViewDelegate

    //scrollView滚动时,就调用该方法。任何offset值改变都调用该方法。即滚动过程中,调用多次- (vo...

网友评论

      本文标题:iOS 滚动过程中逐渐切换颜色值

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