您的位置:首页 > 其它

CAGradientLayer 颜色渐变实现进度条

2015-06-17 15:11 323 查看
#import <UIKit/UIKit.h>

@interface TJGradientProgressView : UIView
/**
*  进度值
*/
@property(nonatomic,assign) CGFloat progressValue;
/**
*  开始动画
*/
- (void)startAnimating;
/**
*  停止动画
*/
- (void)stopAnimating;

@end
//
//  TJGradientProgressView.m
//  TJGradientProgressViewDemo
//
//  Created by SNWF on 15/6/17.
//  Copyright (c) 2015年 SNWFMJ. All rights reserved.
//

#import "TJGradientProgressView.h"
@interface TJGradientProgressView ()
{
CALayer *progressMaskLayer; //进度条蒙版layer图层

}
/**
*  是否开始动画
*/
@property(nonatomic,assign) BOOL ISAnimating;
@end
@implementation TJGradientProgressView
@synthesize ISAnimating = _ISAnimating;
@synthesize progressValue = _progressValue;

- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
/**设置颜色值*/
CAGradientLayer *gradientLayer = (id)[self layer];
gradientLayer.startPoint = CGPointMake(0.0, 0.5);
gradientLayer.endPoint = CGPointMake(1.0, 0.5);

NSMutableArray *colorArray = [NSMutableArray array];
for (NSInteger Hue = 0; Hue <= 360; Hue += 5) {
UIColor *color = [UIColor colorWithHue:1.0*Hue/360.f
saturation:1.0
brightness:1.0
alpha:1.0];
[colorArray addObject:(id)color.CGColor];
}
gradientLayer.colors = [NSArray arrayWithArray:colorArray];

progressMaskLayer = [CALayer layer];
progressMaskLayer.frame = CGRectMake(0, 0, 0, frame.size.height);
progressMaskLayer.backgroundColor = [UIColor blackColor].CGColor;
gradientLayer.mask = progressMaskLayer;

}
return self;
}

- (void)setProgressValue:(CGFloat)progressValue
{
_progressValue = MIN(1.0, fabs(progressValue));
[self setNeedsLayout];
}

- (void)layoutSubviews
{
CGRect maskRect = progressMaskLayer.frame;
maskRect.size.width = CGRectGetWidth(self.bounds)*_progressValue;
progressMaskLayer.frame = maskRect;

}
+ (Class)layerClass
{
return [CAGradientLayer class];
}
- (NSArray *)shiftColors:(NSArray *)colors {

NSMutableArray *mutable = [colors mutableCopy];
id last = [mutable lastObject];
[mutable removeLastObject];
[mutable insertObject:last atIndex:0];
return [NSArray arrayWithArray:mutable];
}

- (void)performAnimation
{
CAGradientLayer *layer = (id)[self layer];
NSArray *fromColors = [layer colors];
NSArray *toColors = [self shiftColors:fromColors];
[layer setColors:toColors];

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"colors"];
[animation setFromValue:fromColors];
[animation setToValue:toColors];
[animation setDuration:0.08];
[animation setRemovedOnCompletion:YES];
[animation setFillMode:kCAFillModeForwards];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
[animation setDelegate:self];
[layer addAnimation:animation forKey:@"animationGradient"];
}
- (void)animationDidStop:(CAAnimation *)animation finished:(BOOL)flag {

if (self.ISAnimating) {

[self performAnimation];
}
}

- (void)startAnimating {

if (!self.ISAnimating)
{

_ISAnimating = YES;

[self performAnimation];
}
}

- (void)stopAnimating {

if (self.ISAnimating) {

_ISAnimating = NO;
}

}

@end
//
//  ViewController.m
//  TJGradientProgressViewDemo
//
//  Created by SNWF on 15/6/17.
//  Copyright (c) 2015年 SNWFMJ. All rights reserved.
//

#import "ViewController.h"
#import "TJGradientProgressView.h"
@interface ViewController ()
{
TJGradientProgressView *progressView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
CGRect frame = CGRectMake(0, 22.0f, CGRectGetWidth(self.view.bounds), 1.0f);
progressView = [[TJGradientProgressView alloc] initWithFrame:frame];

[self.view addSubview:progressView];
[progressView startAnimating];
[self simulateProgress];
//[NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(testAnimation) userInfo:nil repeats:YES];

}

- (void)testAnimation
{
progressView.progressValue +=0.1;
if (progressView.progressValue >=1.0f) {
[progressView setProgressValue:0.1];
}

}
- (void)simulateProgress {

double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

CGFloat increment = (arc4random() % 5) / 10.0f + 0.1;
CGFloat progress  = progressView.progressValue + increment;
[progressView setProgressValue:progress];
if (progress < 1.0) {

[self simulateProgress];
}
});
}

@end




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: