OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ios/chrome/browser/ui/reversed_animation.h" | 5 #include "ios/chrome/browser/ui/reversed_animation.h" |
6 | 6 |
7 #import <QuartzCore/QuartzCore.h> | 7 #import <QuartzCore/QuartzCore.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <cmath> | 9 #include <cmath> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 | 12 #include "base/mac/objc_property_releaser.h" |
13 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
14 #error "This file requires ARC support." | |
15 #endif | |
16 | 13 |
17 @protocol ReversedAnimationProtocol; | 14 @protocol ReversedAnimationProtocol; |
18 typedef CAAnimation<ReversedAnimationProtocol> ReversedAnimation; | 15 typedef CAAnimation<ReversedAnimationProtocol> ReversedAnimation; |
19 | 16 |
20 namespace { | 17 namespace { |
21 // Enum type used to denote the direction of a reversed animation relative to | 18 // Enum type used to denote the direction of a reversed animation relative to |
22 // the original animation's direction. | 19 // the original animation's direction. |
23 typedef enum { | 20 typedef enum { |
24 ANIMATION_DIRECTION_NORMAL, | 21 ANIMATION_DIRECTION_NORMAL, |
25 ANIMATION_DIRECTION_REVERSE | 22 ANIMATION_DIRECTION_REVERSE |
(...skipping 30 matching lines...) Expand all Loading... |
56 // The current direction for the animation. | 53 // The current direction for the animation. |
57 @property(nonatomic, assign) AnimationDirection animationDirection; | 54 @property(nonatomic, assign) AnimationDirection animationDirection; |
58 // The offset into the original animation's duration at the begining of the | 55 // The offset into the original animation's duration at the begining of the |
59 // reverse animation. | 56 // reverse animation. |
60 @property(nonatomic, assign) CFTimeInterval animationTimeOffset; | 57 @property(nonatomic, assign) CFTimeInterval animationTimeOffset; |
61 | 58 |
62 @end | 59 @end |
63 | 60 |
64 #pragma mark - ReversedBasicAnimation | 61 #pragma mark - ReversedBasicAnimation |
65 | 62 |
66 @interface ReversedBasicAnimation : CABasicAnimation<ReversedAnimationProtocol> | 63 @interface ReversedBasicAnimation |
| 64 : CABasicAnimation<ReversedAnimationProtocol> { |
| 65 base::mac::ObjCPropertyReleaser _propertyReleaser_ReversedBasicAnimation; |
| 66 } |
67 | 67 |
68 // Returns an animation that performs |animation| in reverse when added to | 68 // Returns an animation that performs |animation| in reverse when added to |
69 // |layer|. |parentBeginTime| should be set to the beginTime in absolute time | 69 // |layer|. |parentBeginTime| should be set to the beginTime in absolute time |
70 // of |animation|'s parent in the timing hierarchy. | 70 // of |animation|'s parent in the timing hierarchy. |
71 + (instancetype)reversedAnimationForAnimation:(CABasicAnimation*)animation | 71 + (instancetype)reversedAnimationForAnimation:(CABasicAnimation*)animation |
72 forLayer:(CALayer*)layer | 72 forLayer:(CALayer*)layer |
73 parent:(CAAnimationGroup*)parent | 73 parent:(CAAnimationGroup*)parent |
74 parentBeginTime:(CFTimeInterval)parentBeginTime; | 74 parentBeginTime:(CFTimeInterval)parentBeginTime; |
75 | 75 |
76 @end | 76 @end |
77 | 77 |
78 @implementation ReversedBasicAnimation | 78 @implementation ReversedBasicAnimation |
79 | 79 |
80 @synthesize originalAnimation = _originalAnimation; | 80 @synthesize originalAnimation = _originalAnimation; |
81 @synthesize animationDirection = _animationDirection; | 81 @synthesize animationDirection = _animationDirection; |
82 @synthesize animationTimeOffset = _animationTimeOffset; | 82 @synthesize animationTimeOffset = _animationTimeOffset; |
83 | 83 |
| 84 - (instancetype)init { |
| 85 self = [super init]; |
| 86 if (self) { |
| 87 _propertyReleaser_ReversedBasicAnimation.Init( |
| 88 self, [ReversedBasicAnimation class]); |
| 89 } |
| 90 return self; |
| 91 } |
| 92 |
84 - (instancetype)copyWithZone:(NSZone*)zone { | 93 - (instancetype)copyWithZone:(NSZone*)zone { |
85 ReversedBasicAnimation* copy = [super copyWithZone:zone]; | 94 ReversedBasicAnimation* copy = [super copyWithZone:zone]; |
86 copy.originalAnimation = self.originalAnimation; | 95 copy.originalAnimation = self.originalAnimation; |
87 copy.animationDirection = self.animationDirection; | 96 copy.animationDirection = self.animationDirection; |
88 copy.animationTimeOffset = self.animationTimeOffset; | 97 copy.animationTimeOffset = self.animationTimeOffset; |
89 return copy; | 98 return copy; |
90 } | 99 } |
91 | 100 |
92 + (instancetype)reversedAnimationForAnimation:(CABasicAnimation*)animation | 101 + (instancetype)reversedAnimationForAnimation:(CABasicAnimation*)animation |
93 forLayer:(CALayer*)layer | 102 forLayer:(CALayer*)layer |
(...skipping 26 matching lines...) Expand all Loading... |
120 reversedAnimation.fromValue = isReversed ? originalBasicAnimation.toValue | 129 reversedAnimation.fromValue = isReversed ? originalBasicAnimation.toValue |
121 : originalBasicAnimation.fromValue; | 130 : originalBasicAnimation.fromValue; |
122 } | 131 } |
123 return reversedAnimation; | 132 return reversedAnimation; |
124 } | 133 } |
125 | 134 |
126 @end | 135 @end |
127 | 136 |
128 #pragma mark - ReversedAnimationGroup | 137 #pragma mark - ReversedAnimationGroup |
129 | 138 |
130 @interface ReversedAnimationGroup : CAAnimationGroup<ReversedAnimationProtocol> | 139 @interface ReversedAnimationGroup |
| 140 : CAAnimationGroup<ReversedAnimationProtocol> { |
| 141 base::mac::ObjCPropertyReleaser _propertyReleaser_ReversedAnimationGroup; |
| 142 } |
131 | 143 |
132 // Returns an animation that performs |animation| in reverse when added to | 144 // Returns an animation that performs |animation| in reverse when added to |
133 // |layer|. |parentBeginTime| should be set to the beginTime in absolute time | 145 // |layer|. |parentBeginTime| should be set to the beginTime in absolute time |
134 // of the animation group to which |animation| belongs. | 146 // of the animation group to which |animation| belongs. |
135 + (instancetype)reversedAnimationGroupForGroup:(CAAnimationGroup*)group | 147 + (instancetype)reversedAnimationGroupForGroup:(CAAnimationGroup*)group |
136 forLayer:(CALayer*)layer | 148 forLayer:(CALayer*)layer |
137 parent:(CAAnimationGroup*)parent | 149 parent:(CAAnimationGroup*)parent |
138 parentBeginTime:(CFTimeInterval)parentBeginTime; | 150 parentBeginTime:(CFTimeInterval)parentBeginTime; |
139 | 151 |
140 @end | 152 @end |
141 | 153 |
142 @implementation ReversedAnimationGroup | 154 @implementation ReversedAnimationGroup |
143 | 155 |
144 @synthesize originalAnimation = _originalAnimation; | 156 @synthesize originalAnimation = _originalAnimation; |
145 @synthesize animationDirection = _animationDirection; | 157 @synthesize animationDirection = _animationDirection; |
146 @synthesize animationTimeOffset = _animationTimeOffset; | 158 @synthesize animationTimeOffset = _animationTimeOffset; |
147 | 159 |
| 160 - (instancetype)init { |
| 161 self = [super init]; |
| 162 if (self) { |
| 163 _propertyReleaser_ReversedAnimationGroup.Init( |
| 164 self, [ReversedAnimationGroup class]); |
| 165 } |
| 166 return self; |
| 167 } |
| 168 |
148 - (instancetype)copyWithZone:(NSZone*)zone { | 169 - (instancetype)copyWithZone:(NSZone*)zone { |
149 ReversedAnimationGroup* copy = [super copyWithZone:zone]; | 170 ReversedAnimationGroup* copy = [super copyWithZone:zone]; |
150 copy.originalAnimation = self.originalAnimation; | 171 copy.originalAnimation = self.originalAnimation; |
151 copy.animationDirection = self.animationDirection; | 172 copy.animationDirection = self.animationDirection; |
152 copy.animationTimeOffset = self.animationTimeOffset; | 173 copy.animationTimeOffset = self.animationTimeOffset; |
153 return copy; | 174 return copy; |
154 } | 175 } |
155 | 176 |
156 + (instancetype)reversedAnimationGroupForGroup:(CAAnimationGroup*)group | 177 + (instancetype)reversedAnimationGroupForGroup:(CAAnimationGroup*)group |
157 forLayer:(CALayer*)layer | 178 forLayer:(CALayer*)layer |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 } | 310 } |
290 | 311 |
291 void ReverseAnimationsForKeyForLayers(NSString* key, NSArray* layers) { | 312 void ReverseAnimationsForKeyForLayers(NSString* key, NSArray* layers) { |
292 for (CALayer* layer in layers) { | 313 for (CALayer* layer in layers) { |
293 CAAnimation* reversedAnimation = | 314 CAAnimation* reversedAnimation = |
294 CAAnimationMakeReverse([layer animationForKey:key], layer); | 315 CAAnimationMakeReverse([layer animationForKey:key], layer); |
295 [layer removeAnimationForKey:key]; | 316 [layer removeAnimationForKey:key]; |
296 [layer addAnimation:reversedAnimation forKey:key]; | 317 [layer addAnimation:reversedAnimation forKey:key]; |
297 } | 318 } |
298 } | 319 } |
OLD | NEW |