财神随手记账

LCProgressView.m 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //
  2. // LCProgressView.m
  3. // MojiWeather
  4. //
  5. // Created by yang.yang on 16/8/25.
  6. // Copyright © 2016年 Moji Fengyun Software & Technology Development co., Ltd. All rights reserved.
  7. //
  8. #import "LCProgressView.h"
  9. @interface LCProgressView ()
  10. @property (nonatomic , strong) UIView *pointView;
  11. @property (nonatomic , strong) NSMutableArray *pointArray;
  12. @end
  13. @implementation LCProgressView
  14. - (instancetype)initWithDiameter:(CGFloat)diameter {
  15. self = [super init];
  16. if (self) {
  17. self.frame = CGRectMake(0, 0, diameter, diameter);
  18. _animationDuration = 2.0;
  19. _pointArray = [[NSMutableArray alloc] init];
  20. _pointWidth = 6.0;
  21. _pointNumber = 6;
  22. _pointColor = [UIColor redColor];
  23. [self configPoint];
  24. }
  25. return self;
  26. }
  27. #pragma makr - clear
  28. - (void)clearPoints {
  29. for (UIView *pointView in self.pointArray) {
  30. [pointView removeFromSuperview];
  31. }
  32. [self.pointArray removeAllObjects];
  33. }
  34. #pragma mark -- config point
  35. - (void)configPoint {
  36. NSInteger pointCount = self.pointNumber;
  37. CGFloat countFloat = (CGFloat)pointCount;
  38. //每个点动画之间的时间间隔;
  39. CGFloat timeInterval = 0.75 * _animationDuration / countFloat;
  40. [self clearPoints];
  41. for (NSInteger i = 0; i < pointCount; i++) {
  42. UIView *pointView = [self getPoint];
  43. [self addSubview:pointView];
  44. [self.pointArray addObject:pointView];
  45. pointView.alpha = 1;
  46. CAKeyframeAnimation *animation = [self getKeyAnimation];
  47. animation.timeOffset = ( 2 - i ) * timeInterval;
  48. [pointView.layer addAnimation:animation forKey:@"animation"];
  49. }
  50. }
  51. - (UIView *)getPoint {
  52. UIView *pointView = [[UIView alloc] init];
  53. pointView.backgroundColor = _pointColor ? _pointColor : [UIColor whiteColor];
  54. pointView.layer.cornerRadius = _pointWidth / 2.0;
  55. pointView.layer.masksToBounds = YES;
  56. pointView.frame = CGRectMake(0, 0, _pointWidth, _pointWidth);
  57. pointView.center = [self getBeginPoint];
  58. return pointView;
  59. }
  60. #pragma mark - set
  61. - (void)setPointColor:(UIColor *)pointColor {
  62. _pointColor = pointColor;
  63. for (UIView *pointView in self.pointArray) {
  64. pointView.backgroundColor = pointColor;
  65. }
  66. }
  67. - (void)setPointWidth:(CGFloat)pointWidth {
  68. _pointWidth = pointWidth;
  69. [self configPoint];
  70. }
  71. - (void)setAnimationDuration:(CGFloat)animationDuration {
  72. _animationDuration = animationDuration;
  73. [self configPoint];
  74. }
  75. - (void)setPointNumber:(NSInteger)pointNumber {
  76. _pointNumber = pointNumber;
  77. [self configPoint];
  78. }
  79. #pragma mark -- animation
  80. - (void)startAnimation {
  81. [self.pointView.layer addAnimation:[self getKeyAnimation] forKey:@"rotation"];
  82. }
  83. - (CAKeyframeAnimation *)getKeyAnimation {
  84. CGFloat radius = (self.frame.size.width - _pointWidth) / 2;
  85. CGPoint centerPoint = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2);
  86. UIBezierPath *path = [UIBezierPath bezierPath];
  87. [path moveToPoint:[self getBeginPoint]];
  88. [path addArcWithCenter:centerPoint radius:radius startAngle:- M_PI / 2 endAngle:M_PI * 3 / 2 clockwise:1];
  89. CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
  90. keyAnimation.repeatCount = MAXFLOAT;
  91. keyAnimation.path = path.CGPath;
  92. keyAnimation.duration = _animationDuration;
  93. keyAnimation.autoreverses = NO;
  94. keyAnimation.fillMode = kCAFillModeBoth;
  95. keyAnimation.removedOnCompletion = NO;
  96. keyAnimation.calculationMode = kCAAnimationPaced;
  97. keyAnimation.timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.95 :0.35 :0.05 :0.7];
  98. return keyAnimation;
  99. }
  100. - (CGPoint)getBeginPoint {
  101. return CGPointMake(self.frame.size.width / 2.0, _pointWidth / 2.0);
  102. }
  103. @end