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 ...
随机推荐
- Linux自动化之Cobbler补鞋匠安装
cobbler介绍: 快速网络安装linux操作系统的服务,支持众多的Linux发行版:Red Hat. Fedora.CentOS.Debian.Ubuntu和SuSE,也可以支持网络安装w ...
- PAT 1103 Integer Factorization
The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...
- [bzoj2213][Poi2011]Difference_动态规划
Difference bzoj-2213 Poi-2011 题目大意:已知一个长度为n的由小写字母组成的字符串,求其中连续的一段,满足该段中出现最多的字母出现的个数减去该段中出现最少的字母出现的个数最 ...
- Java 和JS Base64加密
项目在登录.注册等场景实现时,经常会用到用户信息前端加密,然后项目后端二次解密,避免信息直接在浏览器上以明文显示. 本文主要介绍了base64加密的方式处理代码,不支持中文 源码如下: base64. ...
- 网站配置https(腾讯云域名操作)
我们都知道http协议是超文本传输协议,早期的网站使用的都是http,但是并不安全,数据在传输过程中容易被拦截篡改.所以后面有了https,也就是经过ssl加密的http协议.本文主要对网站配置htt ...
- Linux Container测试之block IO
简介 Linux Container是OS级别的虚拟化方案,它相比于一般的虚拟机没有了硬件模拟以及指令模拟,相比传统虚拟机具有更低的开销,因此可以应用到私有云之中.LXC目前的版本支持对memor ...
- 2.4-EN_STP
2.4-EN_STP 增强型生成树协议(EN_STP): Spannig Tree port states: blocking 20s+listening 15s+learning 15s最后 ...
- HDOJ 2196 Computer 树的直径
由树的直径定义可得,树上随意一点到树的直径上的两个端点之中的一个的距离是最长的... 三遍BFS求树的直径并预处理距离....... Computer Time Limit: 1000/1000 MS ...
- ANDROID窗体管理服务实现机制和架构分析
一.功能 窗体管理是ANDROID框架一个重要部分,主要包含例如以下功能: )Z-ordered的维护 )窗体的创建.销毁 )窗体的绘制.布局 )Token管理,AppToken )活动窗体管理 ...
- 怎样利用WordPress创建自己定义注冊表单插件
来源:http://www.ido321.com/1031.html 原文:Creating a Custom WordPress Registration Form Plugin 译文:创建一个定制 ...