酷店

ActivityIndicatorAnimation.m 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. //
  2. // ActivityIndicatorAnimation.m
  3. // DGActivityIndicatorExample
  4. //
  5. // Created by 小花 on 2018/11/1.
  6. // Copyright © 2018年 Danil Gontovnik. All rights reserved.
  7. //
  8. #import "ActivityIndicatorAnimation.h"
  9. @implementation ActivityIndicatorAnimation
  10. - (void)setupAnimationInLayer:(CALayer *)layer withSize:(CGSize)size tintColor:(NSArray *)tintColors {
  11. CGFloat circlePadding = 10.0f;
  12. CGFloat circleSize = (size.width - 2 * circlePadding) / 3;
  13. CGFloat x = (layer.bounds.size.width - size.width) / 2;
  14. CGFloat y = (layer.bounds.size.height - circleSize) / 2;
  15. CGFloat duration = 0.75f;
  16. NSArray *timeBegins = @[@0.12f, @0.24f, @0.36f];
  17. CAMediaTimingFunction *timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.2f :0.68f :0.18f :1.08f];
  18. CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
  19. animation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0f, 1.0f, 1.0f)],
  20. [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.3f, 0.3f, 1.0f)],
  21. [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0f, 1.0f, 1.0f)]];
  22. animation.keyTimes = @[@0.0f, @0.3f, @1.0f];
  23. animation.timingFunctions = @[timingFunction, timingFunction];
  24. animation.duration = duration;
  25. animation.repeatCount = HUGE_VALF;
  26. animation.removedOnCompletion = NO;
  27. for (int i = 0; i < 3; i++) {
  28. CALayer *circle = [CALayer layer];
  29. circle.frame = CGRectMake(x + i * circleSize + i * circlePadding, y, circleSize, circleSize);
  30. UIColor *color = tintColors[i];
  31. circle.backgroundColor = color.CGColor;
  32. circle.cornerRadius = circle.bounds.size.width / 2;
  33. animation.beginTime = [timeBegins[i] floatValue];
  34. [circle addAnimation:animation forKey:@"animation"];
  35. [layer addSublayer:circle];
  36. }
  37. }
  38. @end