IOS Gradient Progress bar

Source: Internet
Author: User

#import <UIKit/UIKit.h>

@interface Jianbianview:uiview

To add a progress bar, you can use the Mask property to block part

@property (nonatomic, strong) Calayer *masklayer;

@property (nonatomic, assign) cgfloat progress;

Animation methods

-(void) performanimation;

-(void) Setprogress: (cgfloat) value;

@end

#import "JianBianView.h"

@implementation Jianbianview

@synthesize masklayer,progress;

+ (Class) layerclass

{

Set DEFAULT is Cagradientlayer

return [Cagradientlayer class];

}

-(ID) initWithFrame: (CGRect) frame

{

self = [super Initwithframe:frame];

if (self)

{

Masklayer = [Calayer layer];

[Masklayer setframe:cgrectmake (0, 0, 0, frame.size.height)];

[Masklayer Setbackgroundcolor:[uicolor Blackcolor]. Cgcolor];

Cagradientlayer *layer = (ID) [self layer];

[Layer Setstartpoint:cgpointmake (0.0, 0.5)];

[Layer Setendpoint:cgpointmake (1.0, 0.5)];

Nsmutablearray *colors = [[Nsmutablearray alloc] init];

for (Nsinteger hue = 0; hue <; hue + = 5)

{

Uicolor *color;

Hue hue saturation saturation brightness brightness

color = [Uicolor colorwithhue:1.0*hue/360.0 saturation:1.0 brightness:1.0 alpha:1.0];

[Colors AddObject: (ID) [Color Cgcolor]];

}

[Layer Setcolors:[nsarray arraywitharray:colors];

[Layer Setmask:masklayer];

}

return self;

}

It is not important to create a mask with a width of 0 covering the entire view,mask, and when we progress the property update we increase its width and add it to the initWithFrame:

/*

Masklayer = [Calayer layer];

[Masklayer setframe:cgrectmake (0, 0, 0, frame.size.height)];

[Masklayer Setbackgroundcolor:[uicolor Blackcolor]. Cgcolor];

*/

So rewrite setprogress:

-(void) Setprogress: (cgfloat) value

{

if (Progress! = value)

{

Progress = MIN (1.0, Fabs (value));

[Self setneedslayout];

}

}

-(void) layoutsubviews

{

CGRect maskrect = [Masklayer frame];

MaskRect.size.width = Cgrectgetwidth ([self bounds]) * progress;

[Masklayer Setframe:maskrect];

}

-(void) performanimation

{

Move the last color of the array to the front

Shifting all of the other colors.

Cagradientlayer *layer = (ID) [self layer];

Nsmutablearray *mutable = [[Layer colors] mutablecopy];

ID lastcolor = [mutable lastobject];

[Mutable Removelastobject];

[mutable insertobject:lastcolor atindex:0];

Nsarray *shiftcolors = [Nsarray arraywitharray:mutable];

[Layer setcolors:shiftcolors];

Cabasicanimation *animation;

Animation = [Cabasicanimation animationwithkeypath:@ "Colors"];

[Animation settovalue:shiftcolors];

[Animation setduration:0.08];

[Animation Setremovedoncompletion:yes];

[Animation setfillmode:kcafillmodeforwards];

[Animation setdelegate:self];

[Layer Addanimation:animation forkey:@ "];

}

-(void) Animationdidstop: (Caanimation *) Anim finished: (BOOL) flag

{

[Self performanimation];

}

@end

View Controller Invocation

-(void) viewdidload

{

[Super Viewdidload];

Additional setup after loading the view, typically from a nib.

Self.title = @ "gradient test";

Self.view.backgroundColor = [Uicolor Whitecolor];

[Self Jianbianmethord];

[Nstimer scheduledtimerwithtimeinterval:1.0 target:self selector: @selector (Timemethod) Userinfo:nil Repeats:yes];

[Self addjianbianview];

}

-(void) Timemethod

{

NSLog (@ "enter");

Progress + = 0.1;

[Self.jianbianview setprogress:progress];

}

-----------Add a Gradient view

-(void) Addjianbianview

{

if (Self.jianbianview = = nil)

{

Self.jianbianview = [[Jianbianview alloc] Initwithframe:cgrectmake (0, Self.view.frame.size.width, 10)];

[Self.view AddSubview:self.jianBianView];

[Self.jianbianview performanimation];

}

}

IOS Gradient Progress bar

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.