#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 = ; Hue <= ; Hue += ) {
UIColor *color = [UIColor colorWithHue:1.0*Hue/.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(, , , 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:];
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(, 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() % ) / 10.0f + 0.1;
CGFloat progress = progressView.progressValue + increment;
[progressView setProgressValue:progress];
if (progress < 1.0) { [self simulateProgress];
}
});
} @end

最新文章

  1. Hibernate批量处理数据
  2. DAY6 使用ping钥匙临时开启SSH:22端口,实现远程安全SSH登录管理就这么简单
  3. RabbitMQ学习总结 第三篇:工作队列Work Queue
  4. Uiautomator ——API详解
  5. encache学习教程
  6. html 关于块级元素和行内元素
  7. NPOI 2.0导出word(docx格式)
  8. Android Weekly Notes Issue #249
  9. APP品牌具体有哪几个要素?又是如何操作的?
  10. xfire调用webservice接口的实现方式
  11. 【Salvation】——关卡功能&amp;数据库基础实现
  12. sql server把一个库表的某个字段更新到另一张表的相同字段
  13. 60秒的快速巡检Linux服务器性能
  14. C#.GetHashCode方法简单比较
  15. linux dns 工具包 -- bind-utils
  16. Hive之变量和属性
  17. JS库汇总[重要]
  18. java重载和重载的区别
  19. ini_set的权限大于error_reporting
  20. PL/SQL开发中动态SQL的使用方法

热门文章

  1. [C#] DataTable 操作汇总(持续更新)
  2. [luogu2148 SDOI2009] E&amp;D (博弈论)
  3. 腾讯云,搭建nginx静态网站服务器
  4. Cookie的Python爬虫应用
  5. 洛谷 1373 dp 小a和uim之大逃离 良心题解
  6. 清北学堂模拟赛d2t2 位运算2(bit)
  7. ACM的你伤不起
  8. BZOJ——T 2097: [Usaco2010 Dec]Exercise 奶牛健美操
  9. android程序在调试时出现了套接字异常“java.net.SocketException: Permission denied”该如何解决
  10. Python开发工具安装