@@ -22,30 +22,30 @@ static inline CGFloat degreesToRadians(CGFloat degrees) {
2222 return degrees * M_PI / 180.0 ;
2323}
2424
25- static CAMediaTimingFunction *timingFunctionForEasing (EaseViewTransitionEasing easing) {
25+ static CAMediaTimingFunction *
26+ timingFunctionForEasing (EaseViewTransitionEasing easing) {
2627 switch (easing) {
27- case EaseViewTransitionEasing::Linear:
28- return [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear ];
29- case EaseViewTransitionEasing::EaseIn:
30- return [CAMediaTimingFunction functionWithControlPoints: 0.42 :0.0 :1.0 :1.0 ];
31- case EaseViewTransitionEasing::EaseOut:
32- return [CAMediaTimingFunction functionWithControlPoints: 0.0 :0.0 :0.58 :1.0 ];
33- case EaseViewTransitionEasing::EaseInOut:
34- return [CAMediaTimingFunction functionWithControlPoints: 0.42 :0.0 :0.58 :1.0 ];
28+ case EaseViewTransitionEasing::Linear:
29+ return
30+ [CAMediaTimingFunction functionWithName: kCAMediaTimingFunctionLinear ];
31+ case EaseViewTransitionEasing::EaseIn:
32+ return [CAMediaTimingFunction functionWithControlPoints: 0.42 :0.0 :1.0 :1.0 ];
33+ case EaseViewTransitionEasing::EaseOut:
34+ return [CAMediaTimingFunction functionWithControlPoints: 0.0 :0.0 :0.58 :1.0 ];
35+ case EaseViewTransitionEasing::EaseInOut:
36+ return [CAMediaTimingFunction functionWithControlPoints: 0.42 :0.0 :0.58 :1.0 ];
3537 }
3638}
3739
3840@implementation EaseView {
3941 BOOL _isFirstMount;
4042}
4143
42- + (ComponentDescriptorProvider)componentDescriptorProvider
43- {
44+ + (ComponentDescriptorProvider)componentDescriptorProvider {
4445 return concreteComponentDescriptorProvider<EaseViewComponentDescriptor>();
4546}
4647
47- - (instancetype )initWithFrame : (CGRect)frame
48- {
48+ - (instancetype )initWithFrame : (CGRect)frame {
4949 if (self = [super initWithFrame: frame]) {
5050 static const auto defaultProps = std::make_shared<const EaseViewProps>();
5151 _props = defaultProps;
@@ -56,8 +56,7 @@ - (instancetype)initWithFrame:(CGRect)frame
5656
5757#pragma mark - Animation helpers
5858
59- - (NSValue *)presentationValueForKeyPath : (NSString *)keyPath
60- {
59+ - (NSValue *)presentationValueForKeyPath : (NSString *)keyPath {
6160 CALayer *presentationLayer = self.layer .presentationLayer ;
6261 if (presentationLayer) {
6362 return [presentationLayer valueForKeyPath: keyPath];
@@ -68,10 +67,10 @@ - (NSValue *)presentationValueForKeyPath:(NSString *)keyPath
6867- (CAAnimation *)createAnimationForKeyPath : (NSString *)keyPath
6968 fromValue : (NSValue *)fromValue
7069 toValue : (NSValue *)toValue
71- props : (const EaseViewProps &)props
72- {
70+ props : (const EaseViewProps &)props {
7371 if (props.transitionType == EaseViewTransitionType::Spring) {
74- CASpringAnimation *spring = [CASpringAnimation animationWithKeyPath: keyPath];
72+ CASpringAnimation *spring =
73+ [CASpringAnimation animationWithKeyPath: keyPath];
7574 spring.fromValue = fromValue;
7675 spring.toValue = toValue;
7776 spring.damping = props.transitionDamping ;
@@ -98,8 +97,7 @@ - (void)applyAnimationForKeyPath:(NSString *)keyPath
9897 animationKey : (NSString *)animationKey
9998 fromValue : (NSValue *)fromValue
10099 toValue : (NSValue *)toValue
101- props : (const EaseViewProps &)props
102- {
100+ props : (const EaseViewProps &)props {
103101 [CATransaction begin ];
104102 [CATransaction setDisableActions: YES ];
105103 [self .layer setValue: toValue forKeyPath: keyPath];
@@ -112,8 +110,7 @@ - (void)applyAnimationForKeyPath:(NSString *)keyPath
112110 [self .layer addAnimation: animation forKey: animationKey];
113111}
114112
115- - (void )setModelValue : (NSValue *)value forKeyPath : (NSString *)keyPath
116- {
113+ - (void )setModelValue : (NSValue *)value forKeyPath : (NSString *)keyPath {
117114 [CATransaction begin ];
118115 [CATransaction setDisableActions: YES ];
119116 [self .layer setValue: value forKeyPath: keyPath];
@@ -122,28 +119,37 @@ - (void)setModelValue:(NSValue *)value forKeyPath:(NSString *)keyPath
122119
123120#pragma mark - Props update
124121
125- - (void )updateProps : (const Props::Shared &)props oldProps : (const Props::Shared &)oldProps
126- {
127- const auto &newViewProps = *std::static_pointer_cast<const EaseViewProps>(props);
122+ - (void )updateProps : (const Props::Shared &)props
123+ oldProps : (const Props::Shared &)oldProps {
124+ const auto &newViewProps =
125+ *std::static_pointer_cast<const EaseViewProps>(props);
128126
129127 if (_isFirstMount) {
130128 _isFirstMount = NO ;
131129
132130 BOOL hasInitialAnimation =
133- newViewProps.initialAnimateOpacity != newViewProps.animateOpacity ||
134- newViewProps.initialAnimateTranslateX != newViewProps.animateTranslateX ||
135- newViewProps.initialAnimateTranslateY != newViewProps.animateTranslateY ||
136- newViewProps.initialAnimateScale != newViewProps.animateScale ||
137- newViewProps.initialAnimateRotate != newViewProps.animateRotate ;
131+ newViewProps.initialAnimateOpacity != newViewProps.animateOpacity ||
132+ newViewProps.initialAnimateTranslateX !=
133+ newViewProps.animateTranslateX ||
134+ newViewProps.initialAnimateTranslateY !=
135+ newViewProps.animateTranslateY ||
136+ newViewProps.initialAnimateScale != newViewProps.animateScale ||
137+ newViewProps.initialAnimateRotate != newViewProps.animateRotate ;
138138
139139 if (hasInitialAnimation) {
140140 // Set initial values immediately
141- [self setModelValue: @(newViewProps.initialAnimateOpacity) forKeyPath: @" opacity" ];
142- [self setModelValue: @(newViewProps.initialAnimateTranslateX) forKeyPath: @" transform.translation.x" ];
143- [self setModelValue: @(newViewProps.initialAnimateTranslateY) forKeyPath: @" transform.translation.y" ];
144- [self setModelValue: @(newViewProps.initialAnimateScale) forKeyPath: @" transform.scale.x" ];
145- [self setModelValue: @(newViewProps.initialAnimateScale) forKeyPath: @" transform.scale.y" ];
146- [self setModelValue: @(degreesToRadians (newViewProps.initialAnimateRotate)) forKeyPath: @" transform.rotation.z" ];
141+ [self setModelValue: @(newViewProps.initialAnimateOpacity)
142+ forKeyPath: @" opacity" ];
143+ [self setModelValue: @(newViewProps.initialAnimateTranslateX)
144+ forKeyPath: @" transform.translation.x" ];
145+ [self setModelValue: @(newViewProps.initialAnimateTranslateY)
146+ forKeyPath: @" transform.translation.y" ];
147+ [self setModelValue: @(newViewProps.initialAnimateScale)
148+ forKeyPath: @" transform.scale.x" ];
149+ [self setModelValue: @(newViewProps.initialAnimateScale)
150+ forKeyPath: @" transform.scale.y" ];
151+ [self setModelValue: @(degreesToRadians (newViewProps.initialAnimateRotate))
152+ forKeyPath: @" transform.rotation.z" ];
147153
148154 // Animate from initial to target for properties that differ
149155 if (newViewProps.initialAnimateOpacity != newViewProps.animateOpacity ) {
@@ -153,14 +159,16 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
153159 toValue: @(newViewProps.animateOpacity)
154160 props: newViewProps];
155161 }
156- if (newViewProps.initialAnimateTranslateX != newViewProps.animateTranslateX ) {
162+ if (newViewProps.initialAnimateTranslateX !=
163+ newViewProps.animateTranslateX ) {
157164 [self applyAnimationForKeyPath: @" transform.translation.x"
158165 animationKey: kAnimKeyTranslateX
159166 fromValue: @(newViewProps.initialAnimateTranslateX)
160167 toValue: @(newViewProps.animateTranslateX)
161168 props: newViewProps];
162169 }
163- if (newViewProps.initialAnimateTranslateY != newViewProps.animateTranslateY ) {
170+ if (newViewProps.initialAnimateTranslateY !=
171+ newViewProps.animateTranslateY ) {
164172 [self applyAnimationForKeyPath: @" transform.translation.y"
165173 animationKey: kAnimKeyTranslateY
166174 fromValue: @(newViewProps.initialAnimateTranslateY)
@@ -182,70 +190,84 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
182190 if (newViewProps.initialAnimateRotate != newViewProps.animateRotate ) {
183191 [self applyAnimationForKeyPath: @" transform.rotation.z"
184192 animationKey: kAnimKeyRotate
185- fromValue: @(degreesToRadians (newViewProps.initialAnimateRotate))
186- toValue: @(degreesToRadians (newViewProps.animateRotate))
193+ fromValue: @(degreesToRadians (
194+ newViewProps.initialAnimateRotate))
195+ toValue: @(degreesToRadians (
196+ newViewProps.animateRotate))
187197 props: newViewProps];
188198 }
189199 } else {
190200 // No initial animation, set target values directly
191201 [self setModelValue: @(newViewProps.animateOpacity) forKeyPath: @" opacity" ];
192- [self setModelValue: @(newViewProps.animateTranslateX) forKeyPath: @" transform.translation.x" ];
193- [self setModelValue: @(newViewProps.animateTranslateY) forKeyPath: @" transform.translation.y" ];
194- [self setModelValue: @(newViewProps.animateScale) forKeyPath: @" transform.scale.x" ];
195- [self setModelValue: @(newViewProps.animateScale) forKeyPath: @" transform.scale.y" ];
196- [self setModelValue: @(degreesToRadians (newViewProps.animateRotate)) forKeyPath: @" transform.rotation.z" ];
202+ [self setModelValue: @(newViewProps.animateTranslateX)
203+ forKeyPath: @" transform.translation.x" ];
204+ [self setModelValue: @(newViewProps.animateTranslateY)
205+ forKeyPath: @" transform.translation.y" ];
206+ [self setModelValue: @(newViewProps.animateScale)
207+ forKeyPath: @" transform.scale.x" ];
208+ [self setModelValue: @(newViewProps.animateScale)
209+ forKeyPath: @" transform.scale.y" ];
210+ [self setModelValue: @(degreesToRadians (newViewProps.animateRotate))
211+ forKeyPath: @" transform.rotation.z" ];
197212 }
198213 } else {
199214 // Subsequent updates: animate changed properties
200- const auto &oldViewProps = *std::static_pointer_cast<const EaseViewProps>(oldProps);
215+ const auto &oldViewProps =
216+ *std::static_pointer_cast<const EaseViewProps>(oldProps);
201217
202218 if (oldViewProps.animateOpacity != newViewProps.animateOpacity ) {
203- [self applyAnimationForKeyPath: @" opacity"
204- animationKey: kAnimKeyOpacity
205- fromValue: [self presentationValueForKeyPath: @" opacity" ]
206- toValue: @(newViewProps.animateOpacity)
207- props: newViewProps];
219+ [self
220+ applyAnimationForKeyPath: @" opacity"
221+ animationKey: kAnimKeyOpacity
222+ fromValue: [self presentationValueForKeyPath: @" opacity" ]
223+ toValue: @(newViewProps.animateOpacity)
224+ props: newViewProps];
208225 }
209226 if (oldViewProps.animateTranslateX != newViewProps.animateTranslateX ) {
210227 [self applyAnimationForKeyPath: @" transform.translation.x"
211228 animationKey: kAnimKeyTranslateX
212- fromValue: [self presentationValueForKeyPath: @" transform.translation.x" ]
229+ fromValue: [self presentationValueForKeyPath:
230+ @" transform.translation.x" ]
213231 toValue: @(newViewProps.animateTranslateX)
214232 props: newViewProps];
215233 }
216234 if (oldViewProps.animateTranslateY != newViewProps.animateTranslateY ) {
217235 [self applyAnimationForKeyPath: @" transform.translation.y"
218236 animationKey: kAnimKeyTranslateY
219- fromValue: [self presentationValueForKeyPath: @" transform.translation.y" ]
237+ fromValue: [self presentationValueForKeyPath:
238+ @" transform.translation.y" ]
220239 toValue: @(newViewProps.animateTranslateY)
221240 props: newViewProps];
222241 }
223242 if (oldViewProps.animateScale != newViewProps.animateScale ) {
224243 [self applyAnimationForKeyPath: @" transform.scale.x"
225244 animationKey: kAnimKeyScaleX
226- fromValue: [self presentationValueForKeyPath: @" transform.scale.x" ]
245+ fromValue: [self presentationValueForKeyPath:
246+ @" transform.scale.x" ]
227247 toValue: @(newViewProps.animateScale)
228248 props: newViewProps];
229249 [self applyAnimationForKeyPath: @" transform.scale.y"
230250 animationKey: kAnimKeyScaleY
231- fromValue: [self presentationValueForKeyPath: @" transform.scale.y" ]
251+ fromValue: [self presentationValueForKeyPath:
252+ @" transform.scale.y" ]
232253 toValue: @(newViewProps.animateScale)
233254 props: newViewProps];
234255 }
235256 if (oldViewProps.animateRotate != newViewProps.animateRotate ) {
236257 [self applyAnimationForKeyPath: @" transform.rotation.z"
237258 animationKey: kAnimKeyRotate
238- fromValue: [self presentationValueForKeyPath: @" transform.rotation.z" ]
239- toValue: @(degreesToRadians (newViewProps.animateRotate))
259+ fromValue: [self presentationValueForKeyPath:
260+ @" transform.rotation.z" ]
261+ toValue: @(degreesToRadians (
262+ newViewProps.animateRotate))
240263 props: newViewProps];
241264 }
242265 }
243266
244267 [super updateProps: props oldProps: oldProps];
245268}
246269
247- - (void )prepareForRecycle
248- {
270+ - (void )prepareForRecycle {
249271 [super prepareForRecycle ];
250272 [self .layer removeAllAnimations ];
251273 _isFirstMount = YES ;
0 commit comments