口袋优选

SDWaitingView.m 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // SDWaitingView.m
  3. // SDPhotoBrowser
  4. //
  5. // Created by aier on 15-2-6.
  6. // Copyright (c) 2015年 GSD. All rights reserved.
  7. //
  8. #import "SDWaitingView.h"
  9. //// 图片下载进度指示器背景色
  10. //#define SDWaitingViewBackgroundColor [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7]
  11. //
  12. //// 图片下载进度指示器内部控件间的间距
  13. //
  14. //#define SDWaitingViewItemMargin 10
  15. @implementation SDWaitingView
  16. - (id)initWithFrame:(CGRect)frame
  17. {
  18. self = [super initWithFrame:frame];
  19. if (self) {
  20. self.backgroundColor = SDWaitingViewBackgroundColor;
  21. self.layer.cornerRadius = 5;
  22. self.clipsToBounds = YES;
  23. self.mode = SDWaitingViewModeLoopDiagram;
  24. }
  25. return self;
  26. }
  27. - (void)setProgress:(CGFloat)progress
  28. {
  29. _progress = progress;
  30. [self setNeedsDisplay];
  31. if (progress >= 1) {
  32. [self removeFromSuperview];
  33. }
  34. }
  35. - (void)drawRect:(CGRect)rect
  36. {
  37. CGContextRef ctx = UIGraphicsGetCurrentContext();
  38. CGFloat xCenter = rect.size.width * 0.5;
  39. CGFloat yCenter = rect.size.height * 0.5;
  40. [[UIColor whiteColor] set];
  41. switch (self.mode) {
  42. case SDWaitingViewModePieDiagram:
  43. {
  44. CGFloat radius = MIN(rect.size.width * 0.5, rect.size.height * 0.5) - SDWaitingViewItemMargin;
  45. CGFloat w = radius * 2 + SDWaitingViewItemMargin;
  46. CGFloat h = w;
  47. CGFloat x = (rect.size.width - w) * 0.5;
  48. CGFloat y = (rect.size.height - h) * 0.5;
  49. CGContextAddEllipseInRect(ctx, CGRectMake(x, y, w, h));
  50. CGContextFillPath(ctx);
  51. [SDWaitingViewBackgroundColor set];
  52. CGContextMoveToPoint(ctx, xCenter, yCenter);
  53. CGContextAddLineToPoint(ctx, xCenter, 0);
  54. CGFloat to = - M_PI * 0.5 + self.progress * M_PI * 2 + 0.001; // 初始值
  55. CGContextAddArc(ctx, xCenter, yCenter, radius, - M_PI * 0.5, to, 1);
  56. CGContextClosePath(ctx);
  57. CGContextFillPath(ctx);
  58. }
  59. break;
  60. default:
  61. {
  62. CGContextSetLineWidth(ctx, 15);
  63. CGContextSetLineCap(ctx, kCGLineCapRound);
  64. CGFloat to = - M_PI * 0.5 + self.progress * M_PI * 2 + 0.05; // 初始值0.05
  65. CGFloat radius = MIN(rect.size.width, rect.size.height) * 0.5 - SDWaitingViewItemMargin;
  66. CGContextAddArc(ctx, xCenter, yCenter, radius, - M_PI * 0.5, to, 0);
  67. CGContextStrokePath(ctx);
  68. }
  69. break;
  70. }
  71. }
  72. @end
  73. // 版权属于原作者
  74. // http://code4app.com (cn) http://code4app.net (en)
  75. // 发布代码于最专业的源码分享网站: Code4App.com