#import<UIKit/UIKit.h>typedef ns_enum (Nsinteger, Ecolordirectiontype) {ecolordirectionup,//onEcolordirectiondown,//underEcolordirectionright,//RightEcolordirectionleft,//left};@interfaceColorimageview:uiimageview/** * @brief to determine the direction*/@property (nonatomic,assign) ecolordirectiontype direction;/** * @brief color*/@property (nonatomic,strong) Uicolor*color;/** * @brief percent*/@property (nonatomic,assign) cgfloat percent;@end#import "ColorImageView.h"@interfaceColorimageview () {Cagradientlayer*_gradientlayer;}@end@implementationColorimageview@synthesizecolor =_color;@synthesizePercent =_percent;@synthesizeDirection =_direction;-(Instancetype) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {_gradientlayer=[Cagradientlayer layer]; _gradientlayer.frame=Self.bounds; _gradientlayer.borderwidth=1.0f; _gradientlayer.colors= @[(__bridgeID) [Uicolor Clearcolor]. Cgcolor, (__bridgeID) [Uicolor Redcolor]. Cgcolor]; _gradientlayer.locations= @[@(0.1),@(1)]; [Self.layer Addsublayer:_gradientlayer]; } returnSelf ;}/** * @brief Set color * @param color override setter Method*/- (void) SetColor: (Uicolor *) color{_color=color; _gradientlayer.colors= @[(__bridgeID) [Uicolor Clearcolor]. Cgcolor, (__bridgeID) color. Cgcolor];}-(Uicolor *) color{return_color;}/** * @brief set color split point * @param percent override setter method*/- (void) Setpercent: (cgfloat) percent{_percent=percent; _gradientlayer.locations= @[@ (percent), @ (1)];}-(cgfloat) percent{return_percent;}/** * @brief Set color Gradient direction * @param direction override setter Method*/-(void) Setdirection: (ecolordirectiontype) direction{_direction=direction; Switch(direction) { Caseecolordirectionup: {_gradientlayer.startpoint= Cgpointmake (0,0); _gradientlayer.endpoint= Cgpointmake (0,1); } Break; Caseecolordirectiondown: {_gradientlayer.startpoint= Cgpointmake (0,1); _gradientlayer.endpoint= Cgpointmake (0,0); } Break; Caseecolordirectionleft: {_gradientlayer.startpoint= Cgpointmake (0,0); _gradientlayer.endpoint= Cgpointmake (1,0); } Break; Caseecolordirectionright: {_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"@interfaceViewcontroller () {Colorimageview*_imvcolor;}@end@implementationViewcontroller- (void) viewdidload{[Super Viewdidload]; _imvcolor=[[colorimageview Alloc]initwithframe:cgrectmake (0,0, -,185)]; _imvcolor.image= [UIImage imagenamed:@"1"]; _imvcolor.center=Self.view.center; [Self.view Addsubview:_imvcolor]; [Nstimer scheduledtimerwithtimeinterval:1.0fTarget: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
Uiimageview animation Chroma Difference by Cagradientlayer class encapsulation