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 ...
随机推荐
- 手把手从python安装到setuptools、pip工具安装
一.python安装1.基础开发库 apt-get install gccapt-get install openssl libssl-dev 2.安装数据库和开发库 apt-get install ...
- 集成学习_Bagging 和随机森林(rf)
集成学习方式总共有3种:bagging-(RF).boosting-(GBDT/Adaboost/XGBOOST).stacking 下面将对Bagging 进行介绍:(如下图所示) ...
- python - 函数的定义和使用
目录 函数的定义和使用 一. 为什么要用函数? 二. 函数的参数 三. 函数的变量 global和nolocal 四. 递归函数 五. lamabda匿名函数 函数的定义和使用 1 def test( ...
- python json结构
=====================================================json==============================import reques ...
- SpringBoot杂记
一.配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的: •application.properties •application.yml 配置文件的作用:修改SpringBoo ...
- [BZOJ 4999]This Problem Is Too Simple!
[BZOJ 4999]This Problem Is Too Simple! 题目 给您一颗树,每个节点有个初始值. 现在支持以下两种操作: 1. C i x(0<=x<2^31) 表示将 ...
- 查看OS 各项参数
查看CPU 在linux下 cat /proc/cpuinfo 可以得到CPU信息. 要注意的是CPU型号有不同的种类比如AMD Intel.可能在这个文件中显示的信息也不同.但终归是存在这个文件中的 ...
- MyEclipse10及插件安装教程(附安装包和破解文件)
MyEclipse10安装包+破解文件:MyEclipse10安装包.MyEclipse10破解文件MyEclipse10安装包地址:http://pan.baidu.com/s/1pJrCLB1My ...
- 新的HTML5语义元素
先看一个传统的HTML4的文档: <div class="header"> <h1>My Site Name</h1> <h2>My ...
- [Algorithms] The Bayes Rule
Prior odd: The idea is to take the odds for something happening (against it not happening), which we ...