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

时间:2023-03-09 03:34:05
通过cagradientLayer类封装uiimageview动画色度差
#import <UIKit/UIKit.h>
typedef NS_ENUM(NSInteger, EcolorDirectionType)
{
EcolorDirectionUp, //上
EcolorDirectionDown, //下
EcolorDirectionRight, //右
EcolorDirectionLeft, //左
};
@interface ColorImageView : UIImageView /**
* @brief 确定方向
*/
@property(nonatomic,assign) EcolorDirectionType direction; /**
* @brief 颜色
*/
@property(nonatomic,strong) UIColor *color; /**
* @brief 百分比
*/
@property(nonatomic,assign) CGFloat percent; @end
#import "ColorImageView.h"
@interface ColorImageView ()
{
CAGradientLayer *_gradientLayer;
}
@end
@implementation ColorImageView
@synthesize color = _color;
@synthesize percent = _percent;
@synthesize direction = _direction; - (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
_gradientLayer = [CAGradientLayer layer];
_gradientLayer.frame = self.bounds;
_gradientLayer.borderWidth = 1.0f;
_gradientLayer.colors = @[ (__bridge id)[UIColor clearColor].CGColor,
(__bridge id)[UIColor redColor].CGColor
];
_gradientLayer.locations = @[@(0.1),@()]; [self.layer addSublayer:_gradientLayer];
}
return self;
}
/**
* @brief 设置颜色
* @param color 重写setter方法
*/
- (void)setColor:(UIColor *)color
{
_color = color;
_gradientLayer.colors = @[ (__bridge id)[UIColor clearColor].CGColor,
(__bridge id)color.CGColor
];
}
- (UIColor *)color
{
return _color;
}
/**
* @brief 设置颜色分割点
* @param percent 重写setter方法
*/
- (void)setPercent:(CGFloat)percent
{
_percent = percent;
_gradientLayer.locations= @[@(percent),@()];
} -(CGFloat)percent
{
return _percent;
} /**
* @brief 设置颜色渐变方向
* @param direction 重写setter方法
*/
-(void)setDirection:(EcolorDirectionType)direction
{
_direction=direction;
switch (direction) {
case EcolorDirectionUp:
{
_gradientLayer.startPoint = CGPointMake(, );
_gradientLayer.endPoint = CGPointMake(, );
}
break;
case EcolorDirectionDown:
{
_gradientLayer.startPoint = CGPointMake(, );
_gradientLayer.endPoint = CGPointMake(, ); }
break;
case EcolorDirectionLeft:
{
_gradientLayer.startPoint = CGPointMake(, );
_gradientLayer.endPoint = CGPointMake(, ); }
break;
case EcolorDirectionRight:
{
_gradientLayer.startPoint = CGPointMake(, );
_gradientLayer.endPoint = CGPointMake(, );
}
break;
default:
{
_gradientLayer.startPoint = CGPointMake(, );
_gradientLayer.endPoint = CGPointMake(, );
}
break;
}
}
-(EcolorDirectionType)direction
{
return _direction;
}
@end
#import "ViewController.h"
#import "ColorImageView.h"
@interface ViewController ()
{ ColorImageView *_imvColor;
}
@end @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad]; _imvColor=[[ColorImageView alloc]initWithFrame:CGRectMake(, , , )];
_imvColor.image = [UIImage imageNamed:@""];
_imvColor.center = self.view.center; [self.view addSubview:_imvColor];
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(event) userInfo:nil repeats:YES];
// [self performSelector:@selector(event) withObject:nil afterDelay:2.0f]; }
- (void)event
{
_imvColor.direction=EcolorDirectionUp;
_imvColor.color=[UIColor cyanColor];
_imvColor.percent=arc4random()%/.f;
}
@end

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