分享

IOS 圆圈加载动画(中间带有文本,可放进度值等)

 wintelsui 2020-04-17

效果图:


示例代码:

  1. #pragma mark 添加加载进度
  2. - (void)addLoadProgress
  3. {
  4. IWLoadingProgressView *loadProgress = [IWLoadingProgressView progressView];
  5. loadProgress.frame = CGRectMake(0, 0, AdaptedWidth(100), AdaptedHeight(100));
  6. loadProgress.progress = 0.0;
  7. loadProgress.center = self.view.center;
  8. [self.view addSubview:loadProgress];
  9. }

自定义控件代码:
  1. #import <UIKit/UIKit.h>
  2. @interface IWLoadingProgressView : UIView
  3. //进度值
  4. @property (nonatomic,assign) CGFloat progress;
  5. //清除指示器
  6. - (void)dismiss;
  7. //示例化对象
  8. + (id)progressView;
  9. @end

  1. #import "IWLoadingProgressView.h"
  2. static const CGFloat WLoadingProgressViewItemMargin = 10;
  3. @implementation IWLoadingProgressView
  4. - (instancetype)initWithFrame:(CGRect)frame
  5. {
  6. self = [super initWithFrame:frame];
  7. if (self) {
  8. self.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0];
  9. self.layer.cornerRadius = 5;
  10. self.clipsToBounds = YES;
  11. }
  12. return self;
  13. }
  14. - (void)setProgress:(CGFloat)progress
  15. {
  16. _progress = progress;
  17. if (progress >= 1.0) {
  18. [self removeFromSuperview];
  19. }else {
  20. [self setNeedsDisplay];
  21. }
  22. }
  23. - (void)drawRect:(CGRect)rect
  24. {
  25. //获取上下文
  26. CGContextRef context = UIGraphicsGetCurrentContext();
  27. //获取中心点
  28. CGFloat centerX = rect.size.width * 0.5;
  29. CGFloat centerY = rect.size.height * 0.5;
  30. //设置线宽
  31. CGContextSetLineWidth(context, 4);
  32. //设置目标角度
  33. CGFloat to = M_PI * 2 * _progress;
  34. CGFloat radius = MIN(centerX, centerY) - WLoadingProgressViewItemMargin;
  35. //添加路径
  36. CGContextAddArc(context, centerX, centerY, radius, 0, to, 0);
  37. //设置填充颜色
  38. [KGlobalBlueColor set];
  39. CGContextStrokePath(context);
  40. //添加路径2
  41. CGContextAddArc(context, centerX, centerY, radius, to, M_PI * 2, 0);
  42. [[UIColor whiteColor] set];
  43. CGContextStrokePath(context);
  44. //加载时显示的文字
  45. NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
  46. attributes[NSFontAttributeName] = [UIFont systemFontOfSize:14.0];
  47. attributes[NSForegroundColorAttributeName] = [UIColor colorWithRed:234/255.0 green:186/255.0 blue:133/255.0 alpha:1.0];
  48. NSInteger p = _progress * 100;
  49. NSString *showText = [NSString stringWithFormat:@"%ld",(long)p];
  50. showText = [showText stringByAppendingString:@"%"];
  51. [self setCenterProgressText:showText withAttributes:attributes];
  52. }
  53. #pragma mark 设置加载时显示的文字
  54. - (void)setCenterProgressText:(NSString *)text withAttributes:(NSDictionary *)attributes
  55. {
  56. CGFloat centerX = self.frame.size.width * 0.5;
  57. CGFloat centerY = self.frame.size.height * 0.5;
  58. CGSize strSize;
  59. NSAttributedString *attrStr = nil;
  60. if (attributes[NSFontAttributeName]) {
  61. strSize = [text sizeWithAttributes:@{NSFontAttributeName:attributes[NSFontAttributeName]}];
  62. attrStr = [[NSAttributedString alloc] initWithString:text attributes:attributes];
  63. } else {
  64. strSize = [text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:[UIFont systemFontSize]]}];
  65. attrStr = [[NSAttributedString alloc] initWithString:text attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:[UIFont systemFontSize]]}];
  66. }
  67. CGFloat strX = centerX - strSize.width * 0.5;
  68. CGFloat strY = centerY - strSize.height * 0.5;
  69. [attrStr drawAtPoint:CGPointMake(strX, strY)];
  70. }
  71. #pragma mark 清除指示器
  72. - (void)dismiss
  73. {
  74. self.progress = 1.0f;
  75. }
  76. + (id)progressView
  77. {
  78. return [[self alloc] init];
  79. }
  80. @end

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多