iOS 渐变进度条

#import <UIKit/UIKit.h>

@interface JianBianView : UIView

//为了增加一个表示进度条的进行,可们可以使用mask属性来屏蔽一部分

@property (nonatomic, strong) CALayer *maskLayer;

@property (nonatomic, assign) CGFloat progress;

//动画方法

-(void)performAnimation;

-(void)setProgress:(CGFloat)value;

@end

#import "JianBianView.h"

@implementation JianBianView

@synthesize maskLayer,progress;

+(Class)layerClass

{

    //设置默认是 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 < 360; hue += 5)

        {

            UIColor *color;

            //hue 色调 saturation 饱和度 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;

}

//创建一个宽度为0的mask覆盖整个View,mask的颜色不重要,当我们progress属性更新的时候我们会增加它的宽度 然后在initWithFrame:里面添加:

/*

 maskLayer = [CALayer layer];

 [maskLayer setFrame:CGRectMake(0, 0, 0, frame.size.height)];

 [maskLayer setBackgroundColor:[UIColor blackColor].CGColor];

 */

//所以重写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 in the array to the front

    // shifting all 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

 视图控制器 调用

- (void)viewDidLoad

{

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    self.title = @"渐变测试";

    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(@"进入");

    progress += 0.1;

    [self.jianBianView setProgress:progress];

}

//-----------添加渐变view

-(void)addJianBianView

{

    if (self.jianBianView == nil)

    {

        self.jianBianView = [[JianBianView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 10)];

        [self.view addSubview:self.jianBianView];

        [self.jianBianView performAnimation];

  

    }

}

原文地址:https://www.cnblogs.com/camillezlh/p/4801413.html