如何实现一个平滑的颜色过渡
来源:互联网 发布:什么是云计算 通俗 编辑:程序博客网 时间:2024/05/18 00:34
如何从A颜色平滑的过渡到B颜色
同一个视图,随着进度或者其他过程的变化,从A颜色过渡到B颜色。
所有的颜色都是是由RGB三原色组成,在iOS开发中,通过RGB值的组合来显示不同的颜色。一次A-B其实就是RGB值的改变,如何平滑的过渡呢?那就是等比例的改变RGB值!
那么,如何获取颜色的RGB值呢?
- (NSArray *)getRGBDictionaryByColor:(UIColor *)originColor{ CGFloat r=0,g=0,b=0,a=0; if ([self respondsToSelector:@selector(getRed:green:blue:alpha:)]) { [originColor getRed:&r green:&g blue:&b alpha:&a]; } else { const CGFloat *components = CGColorGetComponents(originColor.CGColor); r = components[0]; g = components[1]; b = components[2]; a = components[3]; } return @[@(r),@(g),@(b)];}
有了上面的方法,下面就可以计算出连个颜色之间的色差
- (NSArray *)transColorBeginColor:(UIColor *)beginColor andEndColor:(UIColor *)endColor { NSArray<NSNumber *> *beginColorArr = [self getRGBDictionaryByColor:beginColor];// NSArray<NSNumber *> *endColorArr = [self getRGBDictionaryByColor:endColor]; NSArray<NSNumber *> *endColorArr = @[@(1.0),@(1.0),@(1.0)]; return @[@([endColorArr[0] doubleValue] - [beginColorArr[0] doubleValue]),@([endColorArr[1] doubleValue] - [beginColorArr[1] doubleValue]),@([endColorArr[2] doubleValue] - [beginColorArr[2] doubleValue])];}
最后通过过渡系数来返回当前的颜色
- (UIColor *)getColorWithColor:(UIColor *)beginColor andCoe:(double)coe andMarginArray:(NSArray<NSNumber *> *)marginArray { NSArray *beginColorArr = [self getRGBDictionaryByColor:beginColor]; double red = [beginColorArr[0] doubleValue] + coe * [marginArray[0] doubleValue]; double green = [beginColorArr[1] doubleValue]+ coe * [marginArray[1] doubleValue]; double blue = [beginColorArr[2] doubleValue] + coe * [marginArray[2] doubleValue]; return RGBNUM(red, green, blue);}
把获取到的颜色赋值给相应的控件,这样便完成了一个颜色过渡的效果。
0 0
- 如何实现一个平滑的颜色过渡
- Android 如何实现一个平滑过渡的ViewPager广告条
- Android 如何实现一个平滑过渡的ViewPager广告条
- Android buttom textview 颜色平滑过渡的动画效果
- viewpager实现banner轮播的平滑过渡
- Unity3d摄像机的平滑过渡
- 使 用 TextSwitcher实现平滑过渡
- css3实现网页平滑过渡效果
- 使用TextSwitcher和ImageSwitcher实现平滑过渡
- TextSwitcher为TextView实现平滑过渡
- Hack5-1 TextSwitcher实现平滑过渡
- 使用TextSwitcher和ImageSwitcher实现平滑过渡
- 使用TextSwitcher 和ImageSwitcher实现平滑过渡
- 纯css3实现页面平滑过渡
- 如何构造一个平滑的最大值函数
- 平滑的 CSS3 和 jquery 过渡动画
- android平滑过渡的动画效果
- 平滑过渡的战争迷雾(二) 实现:真正的迷雾来了
- 数据结构——顺序表的基本操作
- SwipeRefreshLayout详解和自定义上拉加载更多
- 变态跳台阶
- 调整数组顺序使奇数位于偶数之前
- Linux nohup command &后台运行程序+前后台切换命令
- 如何实现一个平滑的颜色过渡
- 浮点型数据存储方式浅析
- 用两个栈实现队列
- JQuery 中 AJAX 如何实现 Excel 文件 下载
- jdbc将图片(二进制文件如电影,音乐等)插入数据库
- python多线程操作报错:No handlers could be found for logger "websocket"
- POCR-project-out cascaded regression for face alignment
- 杭电4499 Cannon DFS 搜索
- 算法提高 连接乘积