GGProgressView.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // GGProgressView.m
  3. //
  4. // Created by GG on 2016/10/20.
  5. // Copyright © 2016年 GG. All rights reserved.
  6. //
  7. #import "GGProgressView.h"
  8. @interface GGProgressView()
  9. {
  10. // UIView *_progressView;
  11. float _progress;
  12. }
  13. @end
  14. @implementation GGProgressView
  15. -(instancetype)initWithFrame:(CGRect)frame{
  16. if (self=[super initWithFrame:frame]) {
  17. // _progressView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, frame.size.height)];
  18. _progress=0;
  19. // self.progressViewStyle=style;
  20. // [self addSubview:_progressView];
  21. }
  22. return self;
  23. // return [self initWithFrame:frame progressViewStyle:GGProgressViewStyleDefault];
  24. }
  25. //
  26. //- (instancetype)initWithFrame:(CGRect)frame progressViewStyle:(GGProgressViewStyle)style
  27. //{
  28. // if (self=[super initWithFrame:frame]) {
  29. //// _progressView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, frame.size.height)];
  30. // _progress=0;
  31. // self.progressViewStyle=style;
  32. //// [self addSubview:_progressView];
  33. // }
  34. // return self;
  35. //}
  36. //-(void)setProgressViewStyle:(GGProgressViewStyle)progressViewStyle
  37. //{
  38. // _progressViewStyle=progressViewStyle;
  39. // if (progressViewStyle==GGProgressViewStyleTrackFillet) {
  40. // self.layer.masksToBounds=YES;
  41. // self.layer.cornerRadius=self.bounds.size.height/2;
  42. // }
  43. // else if (progressViewStyle==GGProgressViewStyleAllFillet)
  44. // {
  45. // self.layer.masksToBounds=YES;
  46. // self.layer.cornerRadius=self.bounds.size.height/2;
  47. //// _progressView.layer.cornerRadius=self.bounds.size.height/2;
  48. // }
  49. //}
  50. -(void)setTrackTintColor:(UIColor *)trackTintColor{
  51. _trackTintColor=trackTintColor;
  52. [self setNeedsDisplay];
  53. // if (self.trackImage) {
  54. //
  55. // }
  56. // else
  57. // {
  58. // self.backgroundColor=trackTintColor;
  59. // }
  60. }
  61. -(void)setProgress:(float)progress
  62. {
  63. _progress=MIN(progress, 1);
  64. // _progressView.frame=CGRectMake(0, 0, self.bounds.size.width*_progress, self.bounds.size.height);
  65. [self setNeedsDisplay];
  66. }
  67. -(float)progress
  68. {
  69. return _progress;
  70. }
  71. -(void)setProgressTintColor:(UIColor *)progressTintColor{
  72. _progressTintColor = progressTintColor;
  73. [self setNeedsDisplay];
  74. // _progressView.backgroundColor=progressTintColor;
  75. }
  76. //-(void)setTrackImage:(UIImage *)trackImage
  77. //{
  78. // _trackImage=trackImage;
  79. // if(self.isTile)
  80. // {
  81. // self.backgroundColor=[UIColor colorWithPatternImage:trackImage];
  82. // }
  83. // else
  84. // {
  85. // self.backgroundColor=[UIColor colorWithPatternImage:[self stretchableWithImage:trackImage]];
  86. // }
  87. //}
  88. //-(void)setIsTile:(BOOL)isTile
  89. //{
  90. // _isTile = isTile;
  91. // if (self.progressImage) {
  92. // [self setProgressImage:self.progressImage];
  93. // }
  94. // if (self.trackImage) {
  95. // [self setTrackImage:self.trackImage];
  96. // }
  97. //}
  98. //-(void)setProgressImage:(UIImage *)progressImage
  99. //{
  100. // _progressImage = progressImage;
  101. // if(self.isTile)
  102. // {
  103. // _progressView.backgroundColor=[UIColor colorWithPatternImage:progressImage];
  104. // }
  105. // else
  106. // {
  107. // _progressView.backgroundColor=[UIColor colorWithPatternImage:[self stretchableWithImage:progressImage]];
  108. // }
  109. //}
  110. //- (UIImage *)stretchableWithImage:(UIImage *)image{
  111. // UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0.f);
  112. // [image drawInRect:self.bounds];
  113. // UIImage *lastImage = UIGraphicsGetImageFromCurrentImageContext();
  114. // UIGraphicsEndImageContext();
  115. // return lastImage;
  116. //}
  117. - (void)drawRect:(CGRect)rect {
  118. CGFloat width = self.bounds.size.width*_progress;
  119. [self.progressTintColor set]; //设置线条颜色
  120. {
  121. UIBezierPath* path = [UIBezierPath bezierPath];
  122. path.lineWidth = 1.0;
  123. path.lineCapStyle = kCGLineCapRound; //线条拐角
  124. path.lineJoinStyle = kCGLineJoinRound; //终点处理
  125. [path moveToPoint:CGPointMake(width - 9.5f + 19.0f * 0.8f, 0.0f)];//起点
  126. // Draw the lines
  127. [path addLineToPoint:CGPointMake(width - 9.5f + 19.0f * 0.2f, self.mj_h)];
  128. [path addLineToPoint:CGPointMake(0.0f, self.mj_h)];
  129. [path addLineToPoint:CGPointMake(0.0f, 0.0f)];
  130. [path closePath];//第五条线通过调用closePath方法得到的
  131. [path fill];//颜色填充
  132. }
  133. [self.trackTintColor set];
  134. {
  135. UIBezierPath* path = [UIBezierPath bezierPath];
  136. path.lineWidth = 1.0;
  137. path.lineCapStyle = kCGLineCapRound; //线条拐角
  138. path.lineJoinStyle = kCGLineJoinRound; //终点处理
  139. [path moveToPoint:CGPointMake(width - 9.5f + 19.0f * 0.8f, 0.0f)];//起点
  140. // Draw the lines
  141. [path addLineToPoint:CGPointMake(self.mj_w, 0.0f)];
  142. [path addLineToPoint:CGPointMake(self.mj_w, self.mj_h)];
  143. [path addLineToPoint:CGPointMake(width - 9.5f + 19.0f * 0.2f, self.mj_h)];
  144. [path closePath];//第五条线通过调用closePath方法得到的
  145. [path fill];//颜色填充
  146. }
  147. }
  148. @end