// // ActivityIndicatorAnimation.m // DGActivityIndicatorExample // // Created by 小花 on 2018/11/1. // Copyright © 2018年 Danil Gontovnik. All rights reserved. // #import "ActivityIndicatorAnimation.h" @implementation ActivityIndicatorAnimation - (void)setupAnimationInLayer:(CALayer *)layer withSize:(CGSize)size tintColor:(NSArray *)tintColors { CGFloat circlePadding = 10.0f; CGFloat circleSize = (size.width - 2 * circlePadding) / 3; CGFloat x = (layer.bounds.size.width - size.width) / 2; CGFloat y = (layer.bounds.size.height - circleSize) / 2; CGFloat duration = 0.75f; NSArray *timeBegins = @[@0.12f, @0.24f, @0.36f]; CAMediaTimingFunction *timingFunction = [CAMediaTimingFunction functionWithControlPoints:0.2f :0.68f :0.18f :1.08f]; CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; animation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0f, 1.0f, 1.0f)], [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.3f, 0.3f, 1.0f)], [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0f, 1.0f, 1.0f)]]; animation.keyTimes = @[@0.0f, @0.3f, @1.0f]; animation.timingFunctions = @[timingFunction, timingFunction]; animation.duration = duration; animation.repeatCount = HUGE_VALF; animation.removedOnCompletion = NO; for (int i = 0; i < 3; i++) { CALayer *circle = [CALayer layer]; circle.frame = CGRectMake(x + i * circleSize + i * circlePadding, y, circleSize, circleSize); UIColor *color = tintColors[i]; circle.backgroundColor = color.CGColor; circle.cornerRadius = circle.bounds.size.width / 2; animation.beginTime = [timeBegins[i] floatValue]; [circle addAnimation:animation forKey:@"animation"]; [layer addSublayer:circle]; } } @end