1.自定义UIActivityIndicatorView
自定义简单的UIActivityIndicatorView,可以调大小。可以设定颜色等等。
本以为UIActivityIndicatorView自带的可以满足。但是UIActivityIndicatorView的不够灵活。大小不能设置(暂时没发现方API),所以自己试着自己写了一个。
1.1 Quartz2D绘制。
1.开启一个定时器。不停的渲染。
2.设定两个同心圆,计算圆上点的坐标。两点一线!
3.绘制线条。设定透明度渲染等等。
1.2 使用CALayer。
1.设定一个圆计算圆上的坐标。
2.创建layer根据位置调整角度。
3.设定CAKeyframeAnimation(帧动画)
使用layer觉得比较麻烦就使用了第一种试着做了一个
2.计算圆上的点坐标。
3.代码
1.DDProgressHUD.h
@interface DDProgressHUD : UIView
+ (void)showHUDAddedTo:(UIView *)view;
+ (void)dismiss;
@end
DDProgressHUD.m
#import "DDProgressHUD.h"
static DDProgressHUD *hud = nil;
//内半径
const CGFloat INRADIUS = 20.0f;
//外半径
const CGFloat OUTRADIUS = 28.0f;
//线宽
const CGFloat LINEWIDTH = 3.0f;
#ifndef COLORA(r, g, b, a)
#define COLORA(r, g, b, a) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]
#endif
@interface DDProgressHUD () {
uint _start;
NSTimer *_timer;
uint _max;
CGFloat *_capacity;
}
@end
@implementation DDProgressHUD
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor blackColor];
self.frame = CGRectMake(0, 0, 80, 80);
self.layer.cornerRadius = 8;
self.layer.masksToBounds = YES;
_max = 24;
self.alpha= 0.7;
CGFloat average = 1.0f / _max;
//分配内存
_capacity = (CGFloat *)malloc(sizeof(CGFloat) * _max);
//计算出每一个白条的透明度
for (int i = 0; i < _max ; ++i) {
_capacity[i] = 1 - average * i;
}
//开启一个定时器
_timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target: self selector: @selector(draw) userInfo: nil repeats: YES];
}
return self;
}
- (void)draw {
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
//横坐标用半径乘cosa,纵坐标用半径乘sina.
CGFloat x = self.layer.bounds.size.width / 2;
CGFloat y = self.layer.bounds.size.height / 2;
const CGFloat PI2 = M_PI * 2;
for (int i = 0; i < _max; i++) {
CGFloat cosa = cos(PI2 / _max * i);
CGFloat sina = sin(PI2 / _max * i);
CGFloat minx = x + INRADIUS * cosa;
CGFloat miny = y + INRADIUS * sina;
CGFloat maxx = x + OUTRADIUS * cosa;
CGFloat maxy = y + OUTRADIUS * sina;
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(minx, miny)];
[path addLineToPoint:CGPointMake(maxx, maxy)];
path.lineWidth = LINEWIDTH;
path.lineCapStyle = kCGLineCapRound;
UIColor *strokeColor = COLOR(0xff, 0xff, 0xff, _capacity[(i + _start) % _max]);
[strokeColor set];
[path stroke];
}
_start = ++_start % _max;
}
+ (void)showHUDAddedTo:(UIView *)view {
if (hud) {
[hud removeFromSuperview];
hud = nil;
}
hud = [[DDProgressHUD alloc] init];
hud.center = view.center;
[view addSubview:hud];
}
+ (void)dismiss {
[UIView animateWithDuration:0.3 animations:^{
hud.alpha = 0;
} completion:^(BOOL finished) {
[hud removeFromSuperview];
hud = nil;
}];
}
- (void)dealloc {
free(_capacity);
}
@end