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