#import <UIKit/UIKit.h>

@interface JianBianView : UIView

//为了增加一个表示进度条的进行,可们可以使用mask属性来屏蔽一部分

@property (nonatomic, strong) CALayer *maskLayer;

@property (nonatomic, assign) CGFloat progress;

//动画方法

-(void)performAnimation;

-(void)setProgress:(CGFloat)value;

@end

#import "JianBianView.h"

@implementation JianBianView

@synthesize maskLayer,progress;

+(Class)layerClass

{

//设置默认是 CAGradientLayer

return [CAGradientLayer class];

}

-(id)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self)

{

maskLayer = [CALayer layer];

[maskLayer setFrame:CGRectMake(0, 0, 0, frame.size.height)];

[maskLayer setBackgroundColor:[UIColor blackColor].CGColor];

CAGradientLayer *layer = (id)[self layer];

[layer setStartPoint:CGPointMake(0.0, 0.5)];

[layer setEndPoint:CGPointMake(1.0, 0.5)];

NSMutableArray *colors = [[NSMutableArray alloc] init];

for (NSInteger hue = 0; hue < 360; hue += 5)

{

UIColor *color;

//hue 色调 saturation 饱和度 brightness 亮度

color = [UIColor colorWithHue:1.0*hue/360.0 saturation:1.0 brightness:1.0 alpha:1.0];

[colors addObject:(id)[color CGColor]];

}

[layer setColors:[NSArray arrayWithArray:colors]];

[layer setMask:maskLayer];

}

return self;

}

//创建一个宽度为0的mask覆盖整个View,mask的颜色不重要,当我们progress属性更新的时候我们会增加它的宽度 然后在initWithFrame:里面添加:

/*

maskLayer = [CALayer layer];

[maskLayer setFrame:CGRectMake(0, 0, 0, frame.size.height)];

[maskLayer setBackgroundColor:[UIColor blackColor].CGColor];

*/

//所以重写setProgress:

-(void)setProgress:(CGFloat)value

{

if (progress != value)

{

progress = MIN(1.0, fabs(value));

[self setNeedsLayout];

}

}

-(void)layoutSubviews

{

CGRect maskRect = [maskLayer frame];

maskRect.size.width = CGRectGetWidth([self bounds]) * progress;

[maskLayer setFrame:maskRect];

}

-(void)performAnimation

{

// Move the last color in the array to the front

// shifting all the other colors.

CAGradientLayer *layer = (id)[self layer];

NSMutableArray *mutable = [[layer colors] mutableCopy];

id lastColor = [mutable lastObject];

[mutable removeLastObject];

[mutable insertObject:lastColor atIndex:0];

NSArray *shiftColors = [NSArray arrayWithArray:mutable];

[layer setColors:shiftColors];

CABasicAnimation *animation;

animation = [CABasicAnimation animationWithKeyPath:@"Colors"];

[animation setToValue:shiftColors];

[animation setDuration:0.08];

[animation setRemovedOnCompletion:YES];

[animation setFillMode:kCAFillModeForwards];

[animation setDelegate:self];

[layer addAnimation:animation forKey:@""];

}

-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag

{

[self performAnimation];

}

@end

视图控制器 调用

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.title = @"渐变测试";

self.view.backgroundColor = [UIColor whiteColor];

//    [self jianBianMethord];

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeMethod) userInfo:nil repeats:YES];

[self addJianBianView];

}

-(void)timeMethod

{

NSLog(@"进入");

progress += 0.1;

[self.jianBianView setProgress:progress];

}

//-----------添加渐变view

-(void)addJianBianView

{

if (self.jianBianView == nil)

{

self.jianBianView = [[JianBianView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 10)];

[self.view addSubview:self.jianBianView];

[self.jianBianView performAnimation];

}

}

iOS 渐变进度条的更多相关文章

  1. 【iOS】环形渐变进度条实现

    之前有人在找渐变进度条的效果,闲来无事就顺手写了一个,然后画了视图层级,方便讲解. 环境信息: Mac OS X 10.10.3 Xcode 6.3.1 iOS 8.3 效果图: 源码下载地址: ht ...

  2. iOS圆弧渐变进度条的实现

    由于项目需要一个环形渐变进度条显示课程,这方便网上的确有很多相关资料但是,都是比较零散的而且,大多数只是放一堆代码就算完了.这里我想详细写一篇我自己实现这个进度条的过程. 实现一个圆弧进度条主要分为三 ...

  3. 自定义控件之圆形颜色渐变进度条--SweepGradient

    前几天在群里面有人找圆形可颜色渐变进度条,其中主要的知识点是SweepGradient: mSweepGradient = new SweepGradient(240, 360, new int[] ...

  4. iOS 自定义进度条

    自定义条形进度条(iOS) ViewController.m文件 #import "ViewController.h" @interface ViewController () @ ...

  5. [iOS]圆形进度条及计时功能

    平时用战网安全令的时候很喜欢圆形倒计时的效果,然后简单看了一下Android的圆形进度条,后来又写了一个IOS的.整体界面参照IOS系统的倒计时功能,顺便熟悉了UIPickerView的一些特性的实现 ...

  6. android自己定义渐变进度条

    项目中须要用到一个弧形渐变的进度条,通过android自带是不能实现的.我是没有找到实现的方法,有大神知道的能够指点.效果图是以下这种 这是通过继承VIew来绘制出来的,网上也有相似的,可是代码那是相 ...

  7. vue 渐变 进度条 progress

    废话 不多少说 ,直接上代码 新建文件 gradual-progress.vue <!-- * @Author: gfc * @Date: 2019-11-07 14:00:11 * @Last ...

  8. svg和css3创建环形渐变进度条

    在负责的项目中,有一个环形渐变读取进度的效果的需求,于是在网上查阅相关资料整理一下.代码如下: <!DOCTYPE html> <html lang="en"&g ...

  9. canvas锥形渐变进度条

    从一个渐变圆角进度条浅出画一个圆 开始 这一切需要从一个(简单)的需求开始,在最开始对设计第一眼看到这张图的时候,感觉挺简单的嘛,直接用echarts饼图模拟出来一个就好了 echarts 然后上ec ...

随机推荐

  1. 仿苹果导航菜单js问题

    通过鼠标与不同图片的间距比对图片做相应的放大缩小. <div id="box"> <img src="images/1.png" class= ...

  2. linux shell技巧

    一.在SHELL编程中,经常要处理一些字符串变量.比如,计算长度啊.截取子串啊.字符替换啊等等,常常要用到awk.expr.sed.tr等命令.下面给大家介绍个简单的字符串处理方法,用不着嵌套复杂的子 ...

  3. hive外部表的建立与数据匹配

    1.建立hive的外部表匹配hdfs上的数据 出现如下报错: hive (solar)> ; OK Failed with exception java.io.IOException:java. ...

  4. Nginx+php+fastcgi在win7下的配置

    首先装载php 1.从www.php.net上下载php对应版本 2.解压之后放到c盘下(其实放哪无所谓,Apache会有配置指向,但是Nginx不用) 3.因为用的5.3.17版本,已经有了php- ...

  5. PDF 补丁丁 0.5.0.2731 发布(增加去除页面表单和链接水印功能)

    新的版本增加了简单的删除表单和链接批注的功能,使用该功能可去掉某些软件打上的水印. 在 PDF 文档选项中选中“清除页面所有表单”和“清除页面所有链接批注”项后,程序将会删除页面的表单和链接批注. 效 ...

  6. 《精通MVC5.0》笔记Razor

    1.1.视图声明数据类型 Razor声明都是@开始,例如@model MVC.Models.Product声明了控制器创给视图的数据类型,这样就可以在视图使用@Modle.property访问数据,如 ...

  7. This TableLayout layout or its LinearLayout parent is possibly useless

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  8. Spring AOP基本概念

    Spring AOP基本概念 目录 Spring AOP定义 AOP基本术语 通知类型 AOP定义 AOP基本术语 切面( Aspect ):一个能横切多个对象的模块化的关注点.对Spring AOP ...

  9. The type String cannot be constructed. You must configure the container to supply this value.

    利用 Enterprise Library 5.0 Microsoft.Practices.EnterpriseLibrary.Common Microsoft.Practices.Enterpris ...

  10. Anjs分词器以及关键词抓取使用的方法

    首先介绍一下这个网址非常有用本文所有的关于Anjs起源来自这里请先查看一下 https://github.com/NLPchina/ansj_seg 在本次测试使用的是     import java ...