forked from oiledCode/AWPercentDrivenInteractiveTransition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AWPercentDrivenInteractiveTransition.m
157 lines (136 loc) · 5.54 KB
/
AWPercentDrivenInteractiveTransition.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// AWPercentDrivenInteractiveTransition.m
//
// Created by Alek Astrom on 2014-04-27.
//
// Copyright (c) 2014 Alek Åström
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "AWPercentDrivenInteractiveTransition.h"
@implementation AWPercentDrivenInteractiveTransition {
__weak id<UIViewControllerContextTransitioning> _transitionContext;
BOOL _isInteracting;
CADisplayLink *_displayLink;
}
#pragma mark - Initialization
- (instancetype)initWithAnimator:(id<UIViewControllerAnimatedTransitioning>)animator {
self = [super init];
if (self) {
[self _commonInit];
_animator = animator;
}
return self;
}
- (instancetype)init {
self = [super init];
if (self) {
[self _commonInit];
}
return self;
}
- (void)_commonInit {
_completionSpeed = 1;
}
#pragma mark - Public methods
- (BOOL)isInteracting {
return _isInteracting;
}
- (CGFloat)duration {
return [_animator transitionDuration:_transitionContext];
}
- (void)startInteractiveTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
NSAssert(_animator, @"The animator property must be set at the start of an interactive transition");
_transitionContext = transitionContext;
[_transitionContext containerView].layer.speed = 0;
[_animator animateTransition:_transitionContext];
}
- (void)updateInteractiveTransition:(CGFloat)percentComplete {
self.percentComplete = fmaxf(fminf(percentComplete, 1), 0); // Input validation
}
- (void)cancelInteractiveTransition {
[_transitionContext cancelInteractiveTransition];
[self _completeTransition];
}
- (void)finishInteractiveTransition {
/*CALayer *layer = [_transitionContext containerView].layer;
layer.speed = 1;
CFTimeInterval pausedTime = [layer timeOffset];
layer.timeOffset = 0.0;
layer.beginTime = 0.0; // Need to reset to 0 to avoid flickering :S
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
layer.beginTime = timeSincePause;*/
[_transitionContext finishInteractiveTransition];
[self _completeTransition];
}
- (UIViewAnimationCurve)completionCurve {
return UIViewAnimationCurveLinear;
}
#pragma mark - Private methods
- (void)setPercentComplete:(CGFloat)percentComplete {
_percentComplete = percentComplete;
[self _setTimeOffset:percentComplete*[self duration]];
[_transitionContext updateInteractiveTransition:percentComplete];
}
- (void)_setTimeOffset:(NSTimeInterval)timeOffset {
[_transitionContext containerView].layer.timeOffset = timeOffset;
}
- (void)_completeTransition {
_displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(_tickAnimation)];
[_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
- (void)_tickAnimation {
NSTimeInterval timeOffset = [self _timeOffset];
NSTimeInterval tick = [_displayLink duration]*[self completionSpeed];
timeOffset += [_transitionContext transitionWasCancelled] ? -tick : tick;
if (timeOffset < 0 || timeOffset > [self duration]) {
[self _transitionFinished];
} else {
[self _setTimeOffset:timeOffset];
}
}
- (CFTimeInterval)_timeOffset {
return [_transitionContext containerView].layer.timeOffset;
}
- (void)_transitionFinished {
[_displayLink invalidate];
CALayer *layer = [_transitionContext containerView].layer;
layer.speed = 1;
if (![_transitionContext transitionWasCancelled]) {
CFTimeInterval pausedTime = [layer timeOffset];
layer.timeOffset = 0.0;
layer.beginTime = 0.0; // Need to reset to 0 to avoid flickering :S
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
layer.beginTime = timeSincePause;
} else {
UIView *toView, *fromView;
if ([_transitionContext respondsToSelector:@selector(viewForKey:)]) {
toView = [_transitionContext viewForKey:UITransitionContextToViewKey];
fromView = [_transitionContext viewForKey:UITransitionContextFromViewKey];
}else{
toView = [_transitionContext viewControllerForKey:UITransitionContextToViewControllerKey].view;
fromView = [_transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey].view;
}
toView.alpha = 0;
fromView.alpha = 1.0;
fromView.transform = CGAffineTransformIdentity;
[[_transitionContext containerView] bringSubviewToFront:fromView];
}
}
@end