Uiimageview animation Chroma Difference by Cagradientlayer class encapsulation

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.