CAGradientLayer 颜色渐变实现进度条
#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
CAGradientLayer 颜色渐变实现进度条的更多相关文章
- WPF 背景颜色渐变的滑动条实现
原文:WPF 背景颜色渐变的滑动条实现 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/BYH371256/article/details/83507 ...
- CAGradientLayer颜色渐变器
使用CAGradientLayer可以实现颜色的渐变, 我们先看下头文件 @interface CAGradientLayer : CALayer @property(nullable, copy) ...
- 来看看Python炫酷的颜色输出与进度条打印
英语单词优化 上篇文章写到了Python开发英语单词记忆工具,其中依赖了bootstrap.css jQuery.js 基础html模块以及片段的css样式.有些朋友问,怎么能将这个练习题打包成单独的 ...
- shell实现带颜色输出的进度条
1.基础版 #!/bin/bash b='' ;$i<=;i+=)) do printf "执行进度 :[%-50s]%d%%\r" $b $i sleep 0.001 b= ...
- 自定义控件之圆形颜色渐变进度条--SweepGradient
前几天在群里面有人找圆形可颜色渐变进度条,其中主要的知识点是SweepGradient: mSweepGradient = new SweepGradient(240, 360, new int[] ...
- iOS 开发技巧-制作环形进度条
有几篇博客写到了怎么实现环形进度条,大多是使用Core Graph来实现,实现比较麻烦且效率略低,只是一个小小的进度条而已,我们当然是用最简单而且效率高的方式来实现. 先看一下这篇博客,博客地址:ht ...
- iOS一分钟学会环形进度条
有几篇博客写到了怎么实现环形进度条,大多是使用Core Graph来实现,实现比较麻烦且效率略低,只是一个小小的进度条而已,我们当然是用最简单而且效率高的方式来实现.先看一下这篇博客,博客地址:htt ...
- 【iOS实现一个颜色渐变的弧形进度条】
在Github上看到一些进度条的功能,都是通过Core Graph来实现.无所谓正确与否,但是开发效率明显就差很多了,而且运行效率还是值得考究的.其实使用苹果提供的Core Animation能够非常 ...
- iOS 之使用CAShapeLayer中的CAGradientLayer实现圆环的颜色渐变
本文转载自:http://blog.csdn.net/zhoutao198712/article/details/20864143 在 Github上看到一些进度条的功能,都是通过Core Graph ...
随机推荐
- Nginx +tomcat 实现负载均衡集群
一. 工具 nginx-1.8.0 apache-tomcat-6.0.33 二. 目标 实现高性能负载均衡的Tomcat集群: 三. 步骤 1.首先下载Nginx ...
- ie 浏览器下ajax请求来自缓存的解决方法
如上图所示,在ie浏览器下发出的请求,如何缓存中已经出现过这条请求记录,则不会请求服务端数据,解决方法是在请求后增加一个随机数,使每次请求都不同*可以添加当前时间戳 url+'?t='+Date.no ...
- Vector 和 Array 区别
1:array 定义的时候必须定义数组的元素个数;而vector 不需要:且只能包含整型字面值常量,枚举常量或者用常量表达式初始化的整型const对象, 非const变量以及需要到运行阶段才知道其值的 ...
- Redis 原子操作INCR
The content below come from http://try.redis.io/ There is something special about INCR. Why do we pr ...
- 【[Offer收割]编程练习赛12 C】矩形分割
[题目链接]:http://hihocoder.com/problemset/problem/1495 [题意] [题解] 把每个方块都再分成3*3的小块; 这样; 对于一个方块来说 如果是'\' 则 ...
- 给sunpinyin加速
因为sunpinyin词库一大就会卡,因此需要自己添加一个脚本给sunpinyin加速. 加速的原理就是把词库添加到内存,现在内存都这么大,根本不在乎这么几兆,当然输入体验更重要啦- 首先先建一个脚本 ...
- sql中对数值四舍五入取小数点后两位数字
用:cast(value as decimal(10,2)) 来实现.
- 设置ckeditor的高度
设置ckeditor的高度 CKEDITOR.replace('text', { filebrowserImageUploadUrl : 'upload_img.do', language : 'zh ...
- Deep Learning Toolboxs
一些好用的 Deep learning toolboxs DeepLearningToolbox MATLAB实现,能够使用CPU或GPU.GPU运算用gpumat实现.改动内核代码很方便 支持主要的 ...
- ExtJs 给grid某一单元格重新赋值
// 司机,搬运工提成 var commissionMoney = (waybillsFtQty * commissionMoneyRatio / personCount).toFixed(2); / ...