forked from mukyasa/MMCamScanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RippleAnimation.m
101 lines (68 loc) · 4.18 KB
/
RippleAnimation.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// RippleAnimation.m
// MaterialDesign
//
// Created by mukesh mandora on 26/02/15.
// Copyright (c) 2015 . All rights reserved.
//
#import "RippleAnimation.h"
@implementation RippleAnimation
- (NSTimeInterval)transitionDuration:(id <UIViewControllerContextTransitioning>)transitionContext{
return 0.8f;
}
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext{
self.transitionContext = transitionContext;
isPresenting=self.isPresentingRipple;
UIViewController *fromVC =[transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
UIViewController *toVC = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
UIView* containerView = [transitionContext containerView];
if(isPresenting){
[containerView addSubview:toVC.view];
UIBezierPath *circleMaskPathInitial = [UIBezierPath bezierPathWithOvalInRect:_touchPoint];
CGPoint extremePoint=CGPointMake(fromVC.view.center.x, fromVC.view.center.y-CGRectGetHeight(toVC.view.bounds));
CGFloat radius=sqrt((extremePoint.x*extremePoint.x) + (extremePoint.y*extremePoint.y));
UIBezierPath *circleMaskPathFinal = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(fromVC.view.frame, -radius, -radius)];
CAShapeLayer *maskLayer = [CAShapeLayer new];
maskLayer.path = circleMaskPathFinal.CGPath;
toVC.view.layer.mask = maskLayer;
CABasicAnimation *maskLayerAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
maskLayerAnimation.fromValue = (__bridge id)(circleMaskPathInitial.CGPath);
maskLayerAnimation.toValue = (__bridge id)(circleMaskPathFinal.CGPath);
maskLayerAnimation.duration = 0.8;
maskLayerAnimation.delegate = self;
maskLayerAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[maskLayer addAnimation:maskLayerAnimation forKey:@"path"];
}
else{
[containerView addSubview:toVC.view];
[containerView addSubview:fromVC.view];
UIBezierPath *circleMaskPathFinal = [UIBezierPath bezierPathWithOvalInRect:_touchPoint];
CGPoint extremePoint=CGPointMake(toVC.view.center.x, toVC.view.center.y-CGRectGetHeight(toVC.view.bounds));
CGFloat radius=sqrt((extremePoint.x*extremePoint.x) + (extremePoint.y*extremePoint.y));
UIBezierPath *circleMaskPathInitial = [UIBezierPath bezierPathWithOvalInRect:CGRectInset(toVC.view.frame, -radius, -radius)];
CAShapeLayer *maskLayer = [CAShapeLayer new];
maskLayer.path = circleMaskPathFinal.CGPath;
fromVC.view.layer.mask = maskLayer;
CABasicAnimation *maskLayerAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
maskLayerAnimation.fromValue = (__bridge id)(circleMaskPathInitial.CGPath);
maskLayerAnimation.toValue = (__bridge id)(circleMaskPathFinal.CGPath);
maskLayerAnimation.duration = 0.8;
maskLayerAnimation.delegate = self;
maskLayerAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
[maskLayer addAnimation:maskLayerAnimation forKey:@"path"];
}
}
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
self.isPresentingRipple=YES;
return self;
}
- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
self.isPresentingRipple=NO;
return self;
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
[self.transitionContext completeTransition:![self.transitionContext transitionWasCancelled]];
[self.transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view.layer.mask = nil;
[self.transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view.layer.mask = nil;
}
@end