快过年了,这几天手上也没有项目,于是自己就开始写一些小Demo,这个就是可以用于提示图片下载进度的一个指示器。
.h文件
#import <UIKit/UIKit.h>
@interface CustomView : UIView
/**
* 0<=hudValue && hudValue<=1
*/
@property (nonatomic, assign) CGFloat hudValue;
@end
.m文件
#import "CustomView.h"
@implementation CustomView
{
CGFloat _trValue;
}
- (void)setHudValue:(CGFloat)hudValue
{
if (hudValue >1)
{
_hudValue = 1;
}else
if (hudValue <0)
{
_hudValue = 0;
}else
{
_hudValue = hudValue;
}
_trValue = (_hudValue/1.0f) *360.0f - 90.0f;
[self setNeedsDisplay];
}
- (void)drawRect:(CGRect)rect
{
CGFloat w = self.frame.size.width;
CGFloat h = self.frame.size.height;
[[UIImage imageNamed:@"image"] drawAsPatternInRect:CGRectMake(0.0f, 0.0f,w, h)];
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextMoveToPoint(ctx, w * 0.5f, h * 0.5f);
CGContextAddArc(ctx, w * 0.5f, h * 0.5f,h * 0.5f, 270.0f * (M_PI/180.0f), _trValue* (M_PI/180.0f),0);
CGContextClosePath(ctx);
CGContextSetRGBFillColor(ctx, 1.0f, 0.0f, 0.0f, 0.4f);
CGContextFillPath(ctx);
}
控制器代码片段
@implementation ViewController
{
CustomView *_hudView;
}
- (void)viewDidLoad {
[super viewDidLoad];
// [UITableView]
CustomView *view = [[CustomView alloc]initWithFrame:CGRectMake(0.0f, 64.0f, self.view.frame.size.width, self.view.frame.size.width)];
[view setBackgroundColor:[UIColor colorWithRed:200.0f/255.0f green:200.0f/255.0f blue:200.0f/255.0f alpha:1.0f]];
[self.view addSubview:view];
view.hudValue = 1.0f;
_hudView = view;
}
- (IBAction)btnAction:(id)sender
{
_hudView.hudValue = 1.0f;
[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}
- (void)timerAction:(NSTimer *)timer
{
if (_hudView.hudValue >0)
{
_hudView.hudValue = _hudView.hudValue-0.01f;
}else
{
[timer invalidate];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
效果图如下: