效果图:
代码部分:
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