效果图:

代码部分:

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. hdu1405 第六周J题(质因数分解)

    J - 数论,质因数分解 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Desc ...

  2. DCI

    理论: 某个data,在一个特定的场景中,以某个角色role,来与该场景中的其他角色进行交互.这个过程要以代码的方式表达出来,他要求data本身不具备交互行为, 有交互行为的是角色,当一个data没有 ...

  3. springmvc参数类型转换三种方式

    SpringMVC绑定参数之类型转换有三种方式:     1. 实体类中加日期格式化注解      @DateTimeFormat(pattern="yyyy-MM-dd hh:MM&quo ...

  4. 安卓使用Dialog创建普通对话框

    Activity页面简单所以XML不再写出.下面给出核心代码: button1=(Button)findViewById(R.id.button1); //为按钮设置监听器  button1.setO ...

  5. winform拖动无边框窗体

    这个无边框拖动船体,代码很少,却总是记不住,于是就在网上搜了这段代码,记录一下,省的再忘 using System; using System.Collections.Generic; using S ...

  6. (未解决)android studio:com.android.support:appcompat-v7:22+ Could not found

    错误信息如下: Error:Could not +. Searched in the following locations: https://jcenter.bintray.com/com/andr ...

  7. android 自定义titlebar

    首先,修改标题栏的宽度和背景,在style.xml中添加: <style> <item name="android:background">@drawabl ...

  8. Linux下的库操作工具-nm、ar、ldd、ldconfig和ld.so

    Linux下的库操作工具-nm.ar.ldd.ldconfig和ld.so .nm [options] file 列出file中的所有符号 [option] -c 将符号转化为用户级的名字 -s 当用 ...

  9. BZOJ 1008 [HNOI2008]越狱

    1008: [HNOI2008]越狱 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 5166  Solved: 2242[Submit][Status] ...

  10. WordPress NextGEN Gallery ‘upload.php’任意文件上传漏洞

    漏洞名称: WordPress NextGEN Gallery ‘upload.php’任意文件上传漏洞 CNNVD编号: CNNVD-201306-259 发布时间: 2013-06-20 更新时间 ...