NIMLoadProgressView.m 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // NIMLoadProgressView.m
  3. // NIM
  4. //
  5. // Created by ios on 13-11-9.
  6. // Copyright (c) 2013年 Netease. All rights reserved.
  7. //
  8. #import "NIMLoadProgressView.h"
  9. @implementation NIMLoadProgressView
  10. - (id)initWithFrame:(CGRect)frame {
  11. self = [super initWithFrame:frame];
  12. if (self) {
  13. _maskView = [[UIImageView alloc] initWithFrame:CGRectZero];
  14. _maskView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
  15. [self addSubview:_maskView];
  16. _progressLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  17. _progressLabel.backgroundColor = [UIColor clearColor];
  18. _progressLabel.font = [UIFont systemFontOfSize:16];
  19. _progressLabel.textColor = [UIColor whiteColor];
  20. [self addSubview:_progressLabel];
  21. _activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
  22. [self addSubview:_activity];
  23. }
  24. return self;
  25. }
  26. #define kPadding 8
  27. - (void)maskBubbleImageView:(CGRect)maskRect markImageView:(UIImageView*)messageContentImageView
  28. {
  29. CALayer *maskLayer = [CALayer layer];
  30. maskLayer.frame = maskRect;
  31. maskLayer.contentsGravity = kCAGravityResize;
  32. messageContentImageView.layer.mask = maskLayer;
  33. messageContentImageView.layer.masksToBounds = YES;
  34. }
  35. - (void)layoutSubviews {
  36. [super layoutSubviews];
  37. _maskView.frame = self.bounds;
  38. CGFloat activityHeight = CGRectGetHeight(_activity.bounds);
  39. CGSize size = [_progressLabel.text sizeWithAttributes:@{NSFontAttributeName:_progressLabel.font}];
  40. CGFloat progressHeight = size.height;
  41. CGFloat totalHeight = activityHeight;
  42. if (progressHeight) {
  43. totalHeight += kPadding + size.height;
  44. }
  45. CGFloat y = (CGRectGetHeight(self.bounds) - totalHeight)/2.0;
  46. _activity.center = CGPointMake(CGRectGetMidX(self.bounds), y+CGRectGetMidY(_activity.bounds));
  47. _progressLabel.bounds = CGRectMake(0, 0, size.width, size.height);
  48. _progressLabel.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMaxY(_activity.frame) + kPadding + size.height/2.0);
  49. CGRect frame = self.bounds;
  50. frame.origin.x = frame.origin.x - 4;
  51. frame.origin.y = frame.origin.y - 4;
  52. frame.size.width = frame.size.width + 12;
  53. frame.size.height = frame.size.height + 8;
  54. [self maskBubbleImageView:frame markImageView:_maskView];
  55. }
  56. - (void)setProgress:(CGFloat)progress {
  57. if (progress > self.maxProgress) {
  58. _progressLabel.text = [NSString stringWithFormat:@"%d%%", (int)(self.maxProgress*100)];
  59. [_activity stopAnimating];
  60. }else if (progress <= 0) {
  61. _progressLabel.text = @"0%";
  62. [_activity startAnimating];
  63. }else {
  64. _progressLabel.text = [NSString stringWithFormat:@"%d%%", (int)(progress*100)];
  65. [_activity startAnimating];
  66. }
  67. [self setNeedsLayout];
  68. }
  69. @end