Skip to content

Instantly share code, notes, and snippets.

@agibson73
Last active May 20, 2017
Embed
What would you like to do?
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self longAnimation];
}
- (void)longAnimation {
UIView *ourView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
ourView.backgroundColor = [UIColor blueColor];
ourView.layer.allowsEdgeAntialiasing = YES;
[self.view addSubview:ourView];
//to center
CASpringAnimation *animation1 = [CASpringAnimation animationWithKeyPath:@"position"];
animation1.damping = 5;
animation1.beginTime = CACurrentMediaTime() + 2.0;
animation1.fillMode = kCAFillModeBackwards;
animation1.toValue = [NSValue valueWithCGPoint:self.view.center];
animation1.fromValue = [NSValue valueWithCGPoint:ourView.layer.position];
animation1.duration = 1.0 + animation1.settlingDuration;
animation1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
ourView.layer.position = self.view.center;
[ourView.layer addAnimation:animation1 forKey:nil];
//cornerRadius
[ourView layoutIfNeeded];
CABasicAnimation *cornerRadius = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
cornerRadius.duration = 0.5;
cornerRadius.toValue = [NSNumber numberWithFloat:ourView.frame.size.width/2];
cornerRadius.fromValue = [NSNumber numberWithFloat:ourView.layer.cornerRadius];
cornerRadius.fillMode = kCAFillModeBackwards;
cornerRadius.beginTime = animation1.duration + animation1.beginTime;
ourView.layer.cornerRadius = ourView.frame.size.width/2;
[ourView.layer addAnimation:cornerRadius forKey:nil];
//shapelayer
CAShapeLayer *spinner = [[CAShapeLayer alloc]init];
spinner.frame = ourView.bounds;
spinner.fillColor = [UIColor clearColor].CGColor;
CGRect insetRect = CGRectInset(ourView.bounds, 2, 2);
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:insetRect];
[spinner setPath:path.CGPath];
[spinner setLineWidth:4];
[spinner setStrokeColor:[UIColor greenColor].CGColor];
spinner.allowsEdgeAntialiasing = YES;
[ourView.layer addSublayer:spinner];
CABasicAnimation *spinnerAnim = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
spinnerAnim.duration = 1;
spinnerAnim.toValue = @1;
spinnerAnim.fromValue = @0;
spinnerAnim.fillMode = kCAFillModeBackwards;
spinnerAnim.autoreverses = YES;
spinnerAnim.beginTime = animation1.duration + animation1.beginTime + cornerRadius.duration;
spinnerAnim.repeatCount = 5;
[spinner addAnimation:spinnerAnim forKey:nil];
CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0];
rotationAnimation.duration = 1;
rotationAnimation.repeatCount = 10;
rotationAnimation.fillMode = kCAFillModeBackwards;
rotationAnimation.beginTime = animation1.duration + animation1.beginTime + cornerRadius.duration;
[ourView.layer addAnimation:rotationAnimation forKey:nil];
//remove STroke color
[CATransaction begin];
CABasicAnimation* colorAnim2 = [CABasicAnimation animationWithKeyPath:@"strokeColor"];
colorAnim2.duration = 1;
colorAnim2.toValue = (__bridge id _Nullable)([[UIColor clearColor] CGColor]);
colorAnim2.fillMode = kCAFillModeBackwards;
colorAnim2.beginTime = animation1.duration + animation1.beginTime + cornerRadius.duration + 10.0;
[CATransaction setCompletionBlock:^{
spinner.strokeColor = [UIColor clearColor].CGColor;
}];
[spinner addAnimation:colorAnim2 forKey:nil];
[CATransaction commit];
//animate blue to red circle
[CATransaction begin];
CABasicAnimation* colorAnim = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
colorAnim.duration = 1;
colorAnim.fromValue = (__bridge id _Nullable)([[UIColor blueColor] CGColor]);
colorAnim.toValue = (__bridge id _Nullable)([[UIColor greenColor] CGColor]);
colorAnim.autoreverses = YES;
colorAnim.fillMode = kCAFillModeBackwards;
colorAnim.beginTime = animation1.duration + animation1.beginTime + cornerRadius.duration + 10.0;
[CATransaction setCompletionBlock:^{
[spinner removeFromSuperlayer];
[self checkmarkAnimationWithFrame:ourView.bounds andTarget:ourView];
}];
[ourView.layer addAnimation:colorAnim forKey:nil];
[CATransaction commit];
//
// //cornerRadius
// CABasicAnimation *finalCornerRadius = [CABasicAnimation animationWithKeyPath:@"cornerRadius"];
// finalCornerRadius.duration = 0.5;
// finalCornerRadius.toValue = 0;
// finalCornerRadius.fromValue = [NSNumber numberWithFloat:ourView.frame.size.width/2];
// finalCornerRadius.fillMode = kCAFillModeBackwards;
// finalCornerRadius.beginTime = animation1.duration + animation1.beginTime + cornerRadius.duration + 20.0 + 2.0;
// ourView.layer.cornerRadius = 0;
// [ourView.layer addAnimation:finalCornerRadius forKey:nil];
//
// //to center
// [CATransaction begin];
// CASpringAnimation *finalSpring = [CASpringAnimation animationWithKeyPath:@"position"];
// finalSpring.damping = 5;
// finalSpring.beginTime = animation1.duration + animation1.beginTime + cornerRadius.duration + 20.0 + 2.0 + 0.5;
// finalSpring.fillMode = kCAFillModeBackwards;
// finalSpring.toValue = [NSValue valueWithCGPoint:CGPointMake(ourView.frame.size.width/2, ourView.frame.size.height/2)];
// finalSpring.fromValue = [NSValue valueWithCGPoint:ourView.layer.position];
// finalSpring.duration = 1.0 + animation1.settlingDuration;
// finalSpring.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
// ourView.layer.position = CGPointMake(ourView.frame.size.width/2, ourView.frame.size.height/2);
// [CATransaction setCompletionBlock:^{
// [self checkmarkAnimationWithFrame:ourView.bounds andTarget:ourView];
// }];
// [ourView.layer addAnimation:finalSpring forKey:nil];
// [CATransaction commit];
//
}
-(void)checkmarkAnimationWithFrame:(CGRect)frame andTarget:(UIView*)target{
//// Color Declarations
UIColor* color = [UIColor whiteColor];
//// Bezier Drawing
UIBezierPath* bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint: CGPointMake(CGRectGetMinX(frame) + 0.25000 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.50918 * CGRectGetHeight(frame))];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.38714 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.69286 * CGRectGetHeight(frame))];
[bezierPath addLineToPoint: CGPointMake(CGRectGetMinX(frame) + 0.76429 * CGRectGetWidth(frame), CGRectGetMinY(frame) + 0.30714 * CGRectGetHeight(frame))];
//shapelayer
CAShapeLayer *checkMark = [[CAShapeLayer alloc]init];
checkMark.frame = target.bounds;
checkMark.fillColor = [UIColor clearColor].CGColor;
checkMark.strokeColor = [UIColor lightGrayColor].CGColor;
checkMark.path = bezierPath.CGPath;
checkMark.lineWidth = frame.size.width * 0.08;
checkMark.strokeEnd = 1;
[target.layer addSublayer:checkMark];
checkMark.position = CGPointMake(target.bounds.size.width/2, target.bounds.size.height/2);
//show as gray with a scale
CABasicAnimation *scaleUp = [CABasicAnimation animationWithKeyPath:@"opacity"];
scaleUp.toValue = @1;
scaleUp.fromValue = @0;
scaleUp.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
scaleUp.beginTime = CACurrentMediaTime();
scaleUp.fillMode = kCAFillModeBackwards;
scaleUp.duration = 0.3;
[checkMark addAnimation:scaleUp forKey:nil];
[CATransaction begin];
CASpringAnimation *scaleDown = [CASpringAnimation animationWithKeyPath:@"transform.scale"];
scaleDown.toValue = @1;
scaleDown.fromValue = @1.2;
scaleDown.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
scaleDown.beginTime = CACurrentMediaTime() + 0.25;
scaleDown.fillMode = kCAFillModeBackwards;
scaleDown.damping = 5;
scaleDown.duration = 0.25 + scaleDown.settlingDuration;
[CATransaction setCompletionBlock:^{
CATransition *trans = [CATransition new];
trans.duration = 0.4;
trans.type = kCATransitionFade;
trans.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[checkMark addAnimation:trans forKey:nil];
checkMark.strokeColor = [UIColor greenColor].CGColor;
CABasicAnimation *checkMarkAnim = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
checkMarkAnim.duration = 0.35;
checkMarkAnim.toValue = @1;
checkMarkAnim.fromValue = @0;
checkMarkAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
checkMarkAnim.fillMode = kCAFillModeBackwards;
[checkMark addAnimation:checkMarkAnim forKey:nil];
}];
[checkMark addAnimation:scaleDown forKey:nil];
[CATransaction commit];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment