效果图:

代码部分:

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

最新文章

  1. 深入浅析JAVA注解
  2. 使用CallableStatement的用法
  3. DataTable转化为Model
  4. BZOJ 4005 [JLOI 2015] 骗我呢
  5. C#链接远程SQL 服务器方法
  6. MRP工作台任务下达之x组织屏蔽全部发放功能
  7. Sping IOC 理解(转)
  8. Linux CentOS7下安装python3
  9. Python中什么是变量Python中定义字符串
  10. 【代码管理】GitHub超详细图文攻略 - Git客户端下载安装 GitHub提交修改源码工作流程 Git分支 标签 过滤 Git版本工作流
  11. excel日期拾取插件(支持Excel 2007 - 2016)
  12. php优秀框架codeigniter学习系列——CI_Output类的学习
  13. Django 查询时间段 时间搜索 过滤
  14. 前端入门CSS(3)
  15. HDU 4529 郑厂长系列故事——N骑士问题 状压dp
  16. Java 利用缓冲字节流来实现视频、音频、图片的复制粘贴
  17. NPOI设置水平、垂直居中
  18. MySQL学习【第五篇SQL语句上】
  19. time函数
  20. KVM虚拟机克隆及快照管理

热门文章

  1. PHP之路——PHPStudy虚拟主机
  2. android 休眠唤醒机制分析(三) — suspend
  3. ubuntu后台运行命令行
  4. 使用getGenericSuperclass()和getActualTypeArguments()将DAO做成泛型
  5. 【HDOJ】2604 Queuing
  6. loadrunner11 安装及破解教程来自百度文库
  7. 多线程爬虫Java调用wget下载文件,独立线程读取输出缓冲区
  8. 微软 Microsoft
  9. 使用libevent进行多线程socket编程demo
  10. Flask 安装 Ubuntu 14.04