#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),@(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),@(1)]; } -(CGFloat)percent { return _percent; } /** * @brief 设置颜色渐变方向 * @param direction 重写setter方法 */ -(void)setDirection:(EcolorDirectionType)direction { _direction=direction; switch (direction) { case EcolorDirectionUp: { _gradientLayer.startPoint = CGPointMake(0, 0); _gradientLayer.endPoint = CGPointMake(0, 1); } break; case EcolorDirectionDown: { _gradientLayer.startPoint = CGPointMake(0, 1); _gradientLayer.endPoint = CGPointMake(0, 0); } break; case EcolorDirectionLeft: { _gradientLayer.startPoint = CGPointMake(0, 0); _gradientLayer.endPoint = CGPointMake(1, 0); } break; case EcolorDirectionRight: { _gradientLayer.startPoint = CGPointMake(1, 0); _gradientLayer.endPoint = CGPointMake(0, 0); } break; default: { _gradientLayer.startPoint = CGPointMake(0, 0); _gradientLayer.endPoint = CGPointMake(0, 1); } 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(0, 0, 220, 185)]; _imvColor.image = [UIImage imageNamed:@"1"]; _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()%100/100.f; } @end
时间: 2024-11-08 20:39:48