效果图:

代码部分:

RPGradientAnimationView.h

#import <UIKit/UIKit.h>

typedef enum : NSUInteger {
RPGradientAnimationViewColorDirectionUpToDown = , // 从上到下
RPGradientAnimationViewColorDirectionLeftToRight = , // 从左到右
RPGradientAnimationViewColorDirectionDownToUp = , // 从下到上
RPGradientAnimationViewColorDirectionRightToLeft = , // 从右到左
RPGradientAnimationViewColorDirectionObliqueLeftToRigth = , // 对角线:左上到右下
RPGradientAnimationViewColorDirectionObliqueRightToLeft = // 对角线:右上到左下 } RPGradientAnimationViewColorDirection; @interface RPGradientAnimationView : UIImageView /** 渐变色方向 */
@property (nonatomic, assign) RPGradientAnimationViewColorDirection colorDirection;
/** 颜色 */
@property (nonatomic, strong) UIColor *color;
/** 颜色百分比(非透明部分) 取值范围:0~1 */
@property (nonatomic, assign) CGFloat percent; @end

RPGradientAnimationView.m

#import "RPGradientAnimationView.h"

@interface RPGradientAnimationView ()

// 渐变层
@property (nonatomic, strong) CAGradientLayer *gradientLayer;
// 背景图片
@property (nonatomic, weak) UIImageView *bgImageView; @end @implementation RPGradientAnimationView - (instancetype)init
{
self = [super init];
if (self) { // 初始化渐变层
self.gradientLayer = [CAGradientLayer layer]; // 颜色组
self.gradientLayer.colors = @[
(id)[UIColor clearColor].CGColor,
(id)[UIColor redColor].CGColor
]; // 设置颜色分隔点
self.gradientLayer.locations = @[@(.f), @(.f)]; // 设置渐变方向
self.gradientLayer.startPoint = CGPointMake(, );
self.gradientLayer.endPoint = CGPointMake(, ); // 添加渐变层
[self.layer addSublayer:self.gradientLayer]; }
return self;
} - (void)layoutSubviews
{
[super layoutSubviews];
self.gradientLayer.frame = self.bounds;
} - (void)setColor:(UIColor *)color
{
_color = color;
self.gradientLayer.colors = @[
(id)[UIColor clearColor].CGColor,
(id)color.CGColor
];
} - (void)setColorDirection:(RPGradientAnimationViewColorDirection)colorDirection
{
_colorDirection = colorDirection;
CGPoint startPoint;
CGPoint endPoint;
switch (colorDirection) {
case RPGradientAnimationViewColorDirectionUpToDown:
startPoint = CGPointMake(, );
endPoint = CGPointMake(, );
break;
case RPGradientAnimationViewColorDirectionLeftToRight:
startPoint = CGPointMake(, );
endPoint = CGPointMake(, );
break;
case RPGradientAnimationViewColorDirectionDownToUp:
startPoint = CGPointMake(, );
endPoint = CGPointMake(, );
break;
case RPGradientAnimationViewColorDirectionRightToLeft:
startPoint = CGPointMake(, );
endPoint = CGPointMake(, );
break;
case RPGradientAnimationViewColorDirectionObliqueLeftToRigth:
startPoint = CGPointMake(, );
endPoint = CGPointMake(, );
break;
case RPGradientAnimationViewColorDirectionObliqueRightToLeft:
startPoint = CGPointMake(, );
endPoint = CGPointMake(, );
break;
default:
startPoint = CGPointMake(, );
endPoint = CGPointMake(, );
break;
}
self.gradientLayer.startPoint = startPoint;
self.gradientLayer.endPoint = endPoint;
} - (void)setPercent:(CGFloat)percent
{
if (!(percent < || percent > )) {
_percent = percent;
self.gradientLayer.locations = @[@(percent), @(.f)];
}
} @end

ViewController.m

#import "ViewController.h"
#import "RPGradientAnimationView.h" #define RPRandomColor [UIColor colorWithRed:(arc4random_uniform(255))/255.0 green:(arc4random_uniform(255))/255.0 blue:(arc4random_uniform(255))/255.0 alpha:1.0] @interface ViewController () @property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, weak) RPGradientAnimationView *gradientAnimationView; @end @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad]; RPGradientAnimationView *gradientAnimationView = [[RPGradientAnimationView alloc] init];
gradientAnimationView.frame = CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height);
gradientAnimationView.colorDirection = RPGradientAnimationViewColorDirectionUpToDown;
gradientAnimationView.color = [UIColor redColor];
gradientAnimationView.image = [UIImage imageNamed:@"开始图片"];
gradientAnimationView.contentMode = UIViewContentModeScaleAspectFill;
[self.view addSubview:gradientAnimationView];
self.gradientAnimationView = gradientAnimationView; self.timer = [NSTimer scheduledTimerWithTimeInterval:.f target:self selector:@selector(startAnimation) userInfo:nil repeats:YES];
} - (void)startAnimation
{
self.gradientAnimationView.percent = arc4random() % / 100.0;
self.gradientAnimationView.color = RPRandomColor;
} @end

github:https://github.com/RinpeChen/RPGradientAnimationViewDemo

CAGradientLayer实现色差动画的更多相关文章

  1. CAGradientLayer渐变颜色动画

    CAGradientLayer渐变颜色动画 或许你用过CAGradientLayer,你知道他是用于渐变颜色的,但你是否直到,CAGradientLayer的渐变颜色是可以动画的哦. 源码: // / ...

  2. 用CAGradientLayer实现渐变色动画

    效果图: github:https://github.com/RinpeChen/CAGradientLayerBasicDemo

  3. CAGradientLayer

    参考: CAShapeLayer和CAGradientLayer 一 简介 1,CAGradientLayer,处理颜色渐变: 2,CAGradientLayer的渐变色可以做隐式动画: 3,大部分情 ...

  4. OCiOS开发:CAGradientLayer 渐变色

    OCiOS开发:CAGradientLayer 渐变色 CAGradientLayer 简介 CAGradientLayer是CALayer图层类的子类,用于处理渐变色的层结构. CAGradient ...

  5. CAGradientLayer功能

    一.CAGradientLayer介绍 .CAGradientLayer是用于处理渐变色的层结构 .CAGradientLayer的渐变色能够做隐式动画 .大部分情况下.CAGradientLayer ...

  6. 通过cagradientLayer类封装uiimageview动画色度差

    #import <UIKit/UIKit.h> typedef NS_ENUM(NSInteger, EcolorDirectionType) { EcolorDirectionUp, / ...

  7. 通过CAGradientLayer类实现色度差动画

    #import "ViewController.h" @interface ViewController () { CAGradientLayer *_gradientLayer; ...

  8. iOS 动画绘制线条颜色渐变的折线图

    效果图 .................... 概述 现状 折线图的应用比较广泛,为了增强用户体验,很多应用中都嵌入了折线图.折线图可以更加直观的表示数据的变化.网络上有很多绘制折线图的demo,有 ...

  9. 通过CAGradientLayer制作渐变色效果(转)

    转载自:http://blog.it985.com/7986.html 看了极客学院的视频之后写的一篇博客,觉得不错,还是作为笔记使用. 简单介绍一下CAGradientLayer吧. Gradien ...

随机推荐

  1. [解决]UserLibrary中的jar包不会自动发布Tomcat的lib目录下(基于MyEclipse2014)

    1.在工程名称上单击[右键] —— 单击[Properties]选项,点击后会弹出属性窗口: 2.选择[Properties]后在左侧树中找到[MyEclipse] —— [Deployment As ...

  2. The underlying provider failed on Open. EF

    本地测试是可以的:但是放到服务器上就不行了: 报错:"The underlying provider failed on Open." 这一情况和我以前遇上的一次错误有点相似啊:都 ...

  3. 转:Sharethrough使用Spark Streaming优化实时竞价

    文章来自于:http://www.infoq.com/cn/news/2014/04/spark-streaming-bidding 来自于Sharethrough的数据基础设施工程师Russell ...

  4. 【编程实践】连续正整数之和(华东师范大学OJ-3025)

    题目描述:一个正整数有可能可以被表示为 n(n>=2) 个连续正整数之和,如: 15=1+2+3+4+5 15=4+5+6 15=7+8 请编写程序,根据输入的任何一个正整数,找出符合这种要求的 ...

  5. Github上最受关注的前端大牛,快来膜拜吧!

    1. Paul Irish Github主页: https://github.com/paulirish 个人主页: http://paulirish.com 维基百科: http://en.wiki ...

  6. cf C. Counting Kangaroos is Fun

    http://codeforces.com/contest/373/problem/C 贪心,先排序,然后从n/2位置倒着找每一个可不可以放到别的袋鼠内. #include <cstdio> ...

  7. (转载)Python 列表(list)操作

    (转载)http://blog.csdn.net/facevoid/article/details/5338048 创建列表sample_list = ['a',1,('a','b')] Python ...

  8. 【日语】アップデート(update)一吻定情OP

    ねぇ~気づいてる? nee ~ kizu iteru ?呐~注意到吗? 私はアップデートしているよwatashi ha appude^to shiteiruyo我期待更新 いつか届きますようにitsu ...

  9. cppunit学习笔记

    下载cppunit 链接:http://www.cnblogs.com/duxiuxing/p/4303809.html cppunit官方文档浅析 链接:http://www.cnblogs.com ...

  10. UVAlive3713 Astronauts(2-SAT)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18511 [思路] 2-SAT. 设分得A或B类任务为1 C类任务为 ...