由于项目的需要,需要做一个加载的动画效果,类似于那种安装的进度条
这个动画是�斜杠的灰色条会一直往右边移动,实现主要是不停的画灰色的条纹,并且整个黄色区域都要画满,上代码
// 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];