美文网首页
动态的进度条

动态的进度条

作者: Jieqiudedede | 来源:发表于2014-12-09 00:11 被阅读627次

由于项目的需要,需要做一个加载的动画效果,类似于那种安装的进度条

这个动画是�斜杠的灰色条会一直往右边移动,实现主要是不停的画灰色的条纹,并且整个黄色区域都要画满,上代码

// Sizes

const NSInteger YLProgressBarSizeInset = 1; //px

const NSInteger YLProgressBarStripesDelta = 8; //px

// Global

#define YLProgressBarDefaultStripeWidth 7 //px

// Animation times

const NSTimeInterval YLProgressBarStripesAnimationTime = 1.0f / 30.0f; // s

-(void)drawRect:(CGRect)rect

{

CGContextRef context = UIGraphicsGetCurrentContext();

self.cornerRadius=rect.size.height/2; //主要是已高度为直径画圆角

self.stripesOffset=(self.stripesOffset>2 *_stripesWidth-1)?0:++self.stripesOffset; //灰色线条的移动主要是根据这个偏移量的改变来不断的移动灰色条

{

//画背景

UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, CGRectGetWidth(rect), CGRectGetHeight(rect)) cornerRadius:_cornerRadius]; //创建一个圆角区域path

[roundedRect addClip];//应该是裁剪 ?

CGContextSaveGState(context);

{

CGFloat trackHeight=CGRectGetHeight(rect)-1;

[[UIColor yellowColor] set];//设置颜色

UIBezierPath *roundRect=[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, CGRectGetWidth(rect), trackHeight) cornerRadius:_cornerRadius];

[roundRect fill]; //填满

//画白色的投影

[[UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.2] set];

UIBezierPath *shadow=[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0.5f, 0, CGRectGetWidth(rect)-1, trackHeight) cornerRadius:_cornerRadius];

[shadow stroke]; //不知道跟fill什么区别

// Draw the inner glow

[[UIColor colorWithRed:0 green:0 blue:0 alpha:0.4f] set];

UIBezierPath *glow = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(_cornerRadius, 0, CGRectGetWidth(rect) - _cornerRadius * 2, 1) cornerRadius:0];

[glow stroke];//这个好像是画和填满不一样

}

CGContextRestoreGState(context);

}

//计算内部的矩形

CGRect innerRect=CGRectMake(YLProgressBarSizeInset,YLProgressBarSizeInset,CGRectGetWidth(rect) - 2 *YLProgressBarSizeInset,CGRectGetHeight(rect) - 2 * YLProgressBarSizeInset);

//画条纹

{

CGContextSaveGState(context);

UIBezierPath *allStripes=[UIBezierPath bezierPath];

NSInteger start=-_stripesWidth;// 7

NSInteger end=innerRect.size.width/(2*_stripesWidth)+ (2 *_stripesWidth);

NSLog(@"start:%d----end:%d",start,end);

CGFloat yoffset=YLProgressBarSizeInset;

for(NSInteger i=start;i<=end;i++)

{

UIBezierPath *stripe=[self stripeWithOrigin:CGPointMake(i*2*_stripesWidth+_stripesOffset, yoffset) bounds:innerRect];

[allStripes appendPath:stripe];//这句不知道什么意思  感觉像是将所有的拼接起来 最后再画

}

UIBezierPath *clipPath=[UIBezierPath bezierPathWithRoundedRect:innerRect cornerRadius:_cornerRadius];

CGContextAddPath(context, clipPath.CGPath);

CGContextClip(context);

CGContextSaveGState(context);

{

CGContextAddPath(context, [allStripes CGPath]);

CGContextClip(context);

CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0 green:0 blue:0 alpha:0.36f].CGColor);

CGContextFillRect(context, innerRect);

}

CGContextRestoreGState(context);

}

CGContextRestoreGState(context);

}

- (UIBezierPath *)stripeWithOrigin:(CGPoint)origin bounds:(CGRect)frame //生成单个条纹区域

{

CGFloat height = CGRectGetHeight(frame);

UIBezierPath *rect = [UIBezierPath bezierPath];

[rect moveToPoint:origin]; //左上角点

[rect addLineToPoint:CGPointMake(origin.x + _stripesWidth, origin.y)];//右上角点

[rect addLineToPoint:CGPointMake(origin.x + _stripesWidth - YLProgressBarStripesDelta, origin.y + height)];//右下角点

[rect addLineToPoint:CGPointMake(origin.x - YLProgressBarStripesDelta, origin.y + height)];//左下角 

[rect addLineToPoint:origin];

[rect closePath]; //正好画了一个平行四边形条纹

return rect;

}

最后用定时器对这个view不停的setneedsdisplay

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

相关文章

  • ProgressBar 进度条

    设置动态进度条 activity_main.xml MainActivity.java

  • 动态的进度条

    由于项目的需要,需要做一个加载的动画效果,类似于那种安装的进度条 这个动画是�斜杠的灰色条会一直往右边移动,实现主...

  • 动态进度条

    前些天有时间,就写了一些可能会用到的功能。 本篇文章写的就是一个动态进度条的一个展示,代码非常简单。 咱们先看下页...

  • CSS3动态进度条

    CSS3的线性渐变生成动态进度条 渐变基本语法: 效果图如下:

  • 实例4:文本进度条

    描述 这是"实例"题,与课上讲解实例相同,请作答检验学习效果。 采用字符串方式打印可以动态变化的文本进度条,进度条...

  • vuejs style 计算属性 样式绑定 动态属性

    目标 动态设置进度条宽度 文档https://cn.vuejs.org/v2/guide/class-and-st...

  • 教程 | 用 Axure 制作具有动态效果的环形进度条

    在进行原型制作的时候,经常会遇到这样一个交互:进度条。并且有时候进度条会自动“走动”到指定的位置,并且具有动态效果...

  • 教程 | 用 Axure 制作具有动态效果的环形进度条

    在进行原型制作的时候,经常会遇到这样一个交互:进度条。并且有时候进度条会自动“走动”到指定的位置,并且具有动态效果...

  • 仿IWatch环形进度条自定义控件

    一、环形进度条1、自定义属性2、绘制3、实现动态刷新机制4、部署控件 二、进度条嵌套集合1、自定义属性2、控件部署...

  • Flutter 动态进度条

    视图比较粗糙,凑合看 需要引入依赖: percent_indicator: ^2.1.1+1 整理不易,给个赞谢谢!

网友评论

      本文标题:动态的进度条

      本文链接:https://www.haomeiwen.com/subject/gqdntttx.html