12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #import "SDWaitingView.h"
- @implementation SDWaitingView
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- self.backgroundColor = SDWaitingViewBackgroundColor;
- self.layer.cornerRadius = 5;
- self.clipsToBounds = YES;
- self.mode = SDWaitingViewModeLoopDiagram;
- }
- return self;
- }
- - (void)setProgress:(CGFloat)progress
- {
- _progress = progress;
- [self setNeedsDisplay];
- if (progress >= 1) {
- [self removeFromSuperview];
- }
- }
- - (void)drawRect:(CGRect)rect
- {
- CGContextRef ctx = UIGraphicsGetCurrentContext();
-
- CGFloat xCenter = rect.size.width * 0.5;
- CGFloat yCenter = rect.size.height * 0.5;
- [[UIColor whiteColor] set];
-
- switch (self.mode) {
- case SDWaitingViewModePieDiagram:
- {
- CGFloat radius = MIN(rect.size.width * 0.5, rect.size.height * 0.5) - SDWaitingViewItemMargin;
-
-
- CGFloat w = radius * 2 + SDWaitingViewItemMargin;
- CGFloat h = w;
- CGFloat x = (rect.size.width - w) * 0.5;
- CGFloat y = (rect.size.height - h) * 0.5;
- CGContextAddEllipseInRect(ctx, CGRectMake(x, y, w, h));
- CGContextFillPath(ctx);
-
- [SDWaitingViewBackgroundColor set];
- CGContextMoveToPoint(ctx, xCenter, yCenter);
- CGContextAddLineToPoint(ctx, xCenter, 0);
- CGFloat to = - M_PI * 0.5 + self.progress * M_PI * 2 + 0.001;
- CGContextAddArc(ctx, xCenter, yCenter, radius, - M_PI * 0.5, to, 1);
- CGContextClosePath(ctx);
-
- CGContextFillPath(ctx);
- }
- break;
-
- default:
- {
- CGContextSetLineWidth(ctx, 15);
- CGContextSetLineCap(ctx, kCGLineCapRound);
- CGFloat to = - M_PI * 0.5 + self.progress * M_PI * 2 + 0.05;
- CGFloat radius = MIN(rect.size.width, rect.size.height) * 0.5 - SDWaitingViewItemMargin;
- CGContextAddArc(ctx, xCenter, yCenter, radius, - M_PI * 0.5, to, 0);
- CGContextStrokePath(ctx);
- }
- break;
- }
- }
- @end
|