-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathEaseView.mm
More file actions
1356 lines (1263 loc) · 57.5 KB
/
Copy pathEaseView.mm
File metadata and controls
1356 lines (1263 loc) · 57.5 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import "EaseView.h"
#import <React/RCTConversions.h>
#import <react/renderer/components/EaseViewSpec/ComponentDescriptors.h>
#import <react/renderer/components/EaseViewSpec/EventEmitters.h>
#import <react/renderer/components/EaseViewSpec/Props.h>
#import <react/renderer/components/EaseViewSpec/RCTComponentViewHelpers.h>
#import "RCTFabricComponentsPlugins.h"
// Forward-declare private method so we can override it.
@interface RCTViewComponentView ()
- (void)invalidateLayer;
@end
using namespace facebook::react;
// Animation key constants
static NSString *const kAnimKeyOpacity = @"ease_opacity";
static NSString *const kAnimKeyTransform = @"ease_transform";
static NSString *const kAnimKeyTransformRotateZ = @"ease_transform_rotZ";
static NSString *const kAnimKeyTransformRotateX = @"ease_transform_rotX";
static NSString *const kAnimKeyTransformRotateY = @"ease_transform_rotY";
static NSString *const kAnimKeyTransformScaleX = @"ease_transform_scX";
static NSString *const kAnimKeyTransformScaleY = @"ease_transform_scY";
static NSString *const kAnimKeyTransformTransX = @"ease_transform_trX";
static NSString *const kAnimKeyTransformTransY = @"ease_transform_trY";
static NSString *const kAnimKeyCornerRadius = @"ease_cornerRadius";
static NSString *const kAnimKeyBackgroundColor = @"ease_backgroundColor";
static NSString *const kAnimKeyBorderWidth = @"ease_borderWidth";
static NSString *const kAnimKeyBorderColor = @"ease_borderColor";
static NSString *const kAnimKeyShadowOpacity = @"ease_shadowOpacity";
static NSString *const kAnimKeyShadowRadius = @"ease_shadowRadius";
static NSString *const kAnimKeyShadowColor = @"ease_shadowColor";
static NSString *const kAnimKeyShadowOffset = @"ease_shadowOffset";
static inline CGFloat degreesToRadians(CGFloat degrees) {
return degrees * M_PI / 180.0;
}
// Compose a full CATransform3D from individual animate values.
// Order: Scale → RotateY → RotateX → RotateZ → Translate.
// Default perspective (1280) matches React Native's default.
// https://github.com/facebook/react-native/blob/a98aa814/ReactAndroid/src/main/java/com/facebook/react/uimanager/BaseViewManager.java#L624
static CATransform3D composeTransform(CGFloat scaleX, CGFloat scaleY,
CGFloat translateX, CGFloat translateY,
CGFloat rotateZ, CGFloat rotateX,
CGFloat rotateY, CGFloat perspective) {
CATransform3D t = CATransform3DIdentity;
t.m34 = -1.0 / perspective;
t = CATransform3DTranslate(t, translateX, translateY, 0);
t = CATransform3DRotate(t, rotateZ, 0, 0, 1);
t = CATransform3DRotate(t, rotateX, 1, 0, 0);
t = CATransform3DRotate(t, rotateY, 0, 1, 0);
t = CATransform3DScale(t, scaleX, scaleY, 1);
return t;
}
// Bitmask flags — must match JS constants
static const int kMaskOpacity = 1 << 0;
static const int kMaskTranslateX = 1 << 1;
static const int kMaskTranslateY = 1 << 2;
static const int kMaskScaleX = 1 << 3;
static const int kMaskScaleY = 1 << 4;
static const int kMaskRotate = 1 << 5;
static const int kMaskRotateX = 1 << 6;
static const int kMaskRotateY = 1 << 7;
static const int kMaskBorderRadius = 1 << 8;
static const int kMaskBackgroundColor = 1 << 9;
static const int kMaskBorderWidth = 1 << 10;
static const int kMaskBorderColor = 1 << 11;
static const int kMaskShadowOpacity = 1 << 12;
static const int kMaskShadowRadius = 1 << 13;
static const int kMaskShadowColor = 1 << 14;
static const int kMaskShadowOffset = 1 << 15;
// kMaskElevation = 1 << 16 — Android-only, no-op on iOS
static const int kMaskAnyTransform = kMaskTranslateX | kMaskTranslateY |
kMaskScaleX | kMaskScaleY | kMaskRotate |
kMaskRotateX | kMaskRotateY;
// Per-property transition config resolved from the transitions struct
struct EaseTransitionConfig {
std::string type;
int duration;
float bezier[4];
float damping;
float stiffness;
float mass;
std::string loop;
int delay;
};
// Convert from a codegen-generated transition config struct to our local
// EaseTransitionConfig
template <typename T>
static EaseTransitionConfig transitionConfigFromStruct(const T &src) {
EaseTransitionConfig config;
config.type = src.type;
config.duration = src.duration;
const auto &b = src.easingBezier;
if (b.size() == 4) {
config.bezier[0] = b[0];
config.bezier[1] = b[1];
config.bezier[2] = b[2];
config.bezier[3] = b[3];
} else {
config.bezier[0] = 0.42f;
config.bezier[1] = 0.0f;
config.bezier[2] = 0.58f;
config.bezier[3] = 1.0f;
}
config.damping = src.damping;
config.stiffness = src.stiffness;
config.mass = src.mass;
config.loop = src.loop;
config.delay = src.delay;
return config;
}
// Check if a category config was explicitly set (non-empty type means JS sent
// it)
template <typename T> static bool hasConfig(const T &cfg) {
return !cfg.type.empty();
}
static EaseTransitionConfig
transitionConfigForProperty(const std::string &name,
const EaseViewProps &props) {
const auto &t = props.transitions;
// Map property name to category, check if category override exists
if (name == "opacity" && hasConfig(t.opacity)) {
return transitionConfigFromStruct(t.opacity);
} else if ((name == "translateX" || name == "translateY" ||
name == "scaleX" || name == "scaleY" || name == "rotate" ||
name == "rotateX" || name == "rotateY") &&
hasConfig(t.transform)) {
return transitionConfigFromStruct(t.transform);
} else if (name == "borderRadius" && hasConfig(t.borderRadius)) {
return transitionConfigFromStruct(t.borderRadius);
} else if (name == "backgroundColor" && hasConfig(t.backgroundColor)) {
return transitionConfigFromStruct(t.backgroundColor);
} else if ((name == "borderWidth" || name == "borderColor") &&
hasConfig(t.border)) {
return transitionConfigFromStruct(t.border);
} else if ((name == "shadowOpacity" || name == "shadowRadius" ||
name == "shadowColor" || name == "shadowOffset") &&
hasConfig(t.shadow)) {
return transitionConfigFromStruct(t.shadow);
}
// Fallback to defaultConfig
return transitionConfigFromStruct(t.defaultConfig);
}
// Find lowest property name with a set mask bit among transform properties
static std::string lowestTransformPropertyName(int mask) {
if (mask & kMaskTranslateX)
return "translateX";
if (mask & kMaskTranslateY)
return "translateY";
if (mask & kMaskScaleX)
return "scaleX";
if (mask & kMaskScaleY)
return "scaleY";
if (mask & kMaskRotate)
return "rotate";
if (mask & kMaskRotateX)
return "rotateX";
if (mask & kMaskRotateY)
return "rotateY";
return "translateX"; // fallback
}
// CAAnimation strongly retains its delegate, and the loop animations saved in
// _loopAnimations never complete, so making the view itself the delegate
// would create a permanent retain cycle (view → _loopAnimations → animation →
// view) that leaks the view on any release path that skips prepareForRecycle.
// Animations retain this proxy instead; it holds the view weakly and forwards
// the completion callback.
@interface EaseAnimationDelegateProxy : NSObject <CAAnimationDelegate>
@property(nonatomic, weak) EaseView *target;
@end
@implementation EaseAnimationDelegateProxy
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
[self.target animationDidStop:anim finished:flag];
}
@end
@implementation EaseView {
BOOL _isFirstMount;
BOOL _hasPendingFirstMountUpdate;
NSInteger _animationBatchId;
NSInteger _pendingAnimationCount;
BOOL _anyInterrupted;
CGFloat _transformOriginX;
CGFloat _transformOriginY;
CGFloat _transformPerspective;
// Snapshot of in-flight loop animations, keyed by animation key. iOS
// removes CAAnimations when a layer leaves the window hierarchy (e.g.
// react-navigation tab switches), so we re-add these on re-attach.
// Each saved animation has an explicit beginTime, which iOS preserves
// through addAnimation's copy — so phase continues seamlessly via
// (currentMediaTime - beginTime) mod period.
NSMutableDictionary<NSString *, CAAnimation *> *_loopAnimations;
EaseAnimationDelegateProxy *_delegateProxy;
}
+ (ComponentDescriptorProvider)componentDescriptorProvider {
return concreteComponentDescriptorProvider<EaseViewComponentDescriptor>();
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
static const auto defaultProps = std::make_shared<const EaseViewProps>();
_props = defaultProps;
_isFirstMount = YES;
_transformPerspective = 1280.0;
_hasPendingFirstMountUpdate = NO;
_transformOriginX = 0.5;
_transformOriginY = 0.5;
_loopAnimations = [NSMutableDictionary dictionary];
_delegateProxy = [EaseAnimationDelegateProxy new];
_delegateProxy.target = self;
}
return self;
}
#pragma mark - Transform origin
- (void)updateAnchorPoint {
CGPoint newAnchor = CGPointMake(_transformOriginX, _transformOriginY);
if (CGPointEqualToPoint(newAnchor, self.layer.anchorPoint)) {
return;
}
CGPoint oldAnchor = self.layer.anchorPoint;
CGSize size = self.layer.bounds.size;
CGPoint pos = self.layer.position;
pos.x += (newAnchor.x - oldAnchor.x) * size.width;
pos.y += (newAnchor.y - oldAnchor.y) * size.height;
self.layer.anchorPoint = newAnchor;
self.layer.position = pos;
}
- (void)updateLayoutMetrics:(const LayoutMetrics &)layoutMetrics
oldLayoutMetrics:(const LayoutMetrics &)oldLayoutMetrics {
// Temporarily reset to default anchorPoint so super's frame setting
// computes position correctly, then re-apply our custom anchorPoint.
CGPoint customAnchor = self.layer.anchorPoint;
BOOL hasCustomAnchor =
!CGPointEqualToPoint(customAnchor, CGPointMake(0.5, 0.5));
if (hasCustomAnchor) {
self.layer.anchorPoint = CGPointMake(0.5, 0.5);
}
[super updateLayoutMetrics:layoutMetrics oldLayoutMetrics:oldLayoutMetrics];
if (hasCustomAnchor) {
CGSize size = self.layer.bounds.size;
CGPoint pos = self.layer.position;
pos.x += (customAnchor.x - 0.5) * size.width;
pos.y += (customAnchor.y - 0.5) * size.height;
self.layer.anchorPoint = customAnchor;
self.layer.position = pos;
}
}
#pragma mark - Animation helpers
- (CATransform3D)presentationTransform {
CALayer *pl = self.layer.presentationLayer;
return pl ? pl.transform : self.layer.transform;
}
- (NSValue *)presentationValueForKeyPath:(NSString *)keyPath {
CALayer *presentationLayer = self.layer.presentationLayer;
if (presentationLayer) {
return [presentationLayer valueForKeyPath:keyPath];
}
return [self.layer valueForKeyPath:keyPath];
}
- (CAAnimation *)createAnimationForKeyPath:(NSString *)keyPath
fromValue:(NSValue *)fromValue
toValue:(NSValue *)toValue
config:(EaseTransitionConfig)config
loop:(BOOL)loop {
if (config.type == "spring") {
CASpringAnimation *spring =
[CASpringAnimation animationWithKeyPath:keyPath];
spring.fromValue = fromValue;
spring.toValue = toValue;
spring.damping = config.damping;
spring.stiffness = config.stiffness;
spring.mass = config.mass;
spring.initialVelocity = 0;
spring.duration = spring.settlingDuration;
return spring;
} else {
CABasicAnimation *timing = [CABasicAnimation animationWithKeyPath:keyPath];
timing.fromValue = fromValue;
timing.toValue = toValue;
timing.duration = config.duration / 1000.0;
timing.timingFunction = [CAMediaTimingFunction
functionWithControlPoints:config.bezier[0]:config.bezier[1
]:config.bezier[2]:config.bezier[3]];
if (loop) {
if (config.loop == "repeat") {
timing.repeatCount = HUGE_VALF;
} else if (config.loop == "reverse") {
timing.repeatCount = HUGE_VALF;
timing.autoreverses = YES;
}
}
return timing;
}
}
- (void)applyAnimationForKeyPath:(NSString *)keyPath
animationKey:(NSString *)animationKey
fromValue:(NSValue *)fromValue
toValue:(NSValue *)toValue
config:(EaseTransitionConfig)config
loop:(BOOL)loop {
_pendingAnimationCount++;
CAAnimation *animation = [self createAnimationForKeyPath:keyPath
fromValue:fromValue
toValue:toValue
config:config
loop:loop];
BOOL isLooping =
loop && (config.loop == "repeat" || config.loop == "reverse");
if (config.delay > 0) {
animation.beginTime = CACurrentMediaTime() + (config.delay / 1000.0);
animation.fillMode = kCAFillModeBackwards;
} else if (isLooping) {
// Set explicit beginTime so the phase survives the addAnimation copy.
// Without this, re-adding the saved animation later would reset to a
// fresh "now" and visually restart the loop from the start.
animation.beginTime = CACurrentMediaTime();
}
[animation setValue:@(_animationBatchId) forKey:@"easeBatchId"];
animation.delegate = _delegateProxy;
[self.layer addAnimation:animation forKey:animationKey];
if (isLooping) {
_loopAnimations[animationKey] = animation;
} else {
[_loopAnimations removeObjectForKey:animationKey];
}
}
// Remove the explicit animation from both the layer and our saved snapshot
// so it doesn't get re-added when the view re-enters a window.
- (void)removeEaseAnimationForKey:(NSString *)key {
[self.layer removeAnimationForKey:key];
[_loopAnimations removeObjectForKey:key];
}
- (void)reapplyLoopAnimations {
if (_loopAnimations.count == 0) {
return;
}
[CATransaction begin];
[CATransaction setDisableActions:YES];
for (NSString *key in _loopAnimations) {
// Increment to balance the eventual animationDidStop callback when the
// view detaches again (or the loop is replaced).
_pendingAnimationCount++;
[self.layer addAnimation:_loopAnimations[key] forKey:key];
}
[CATransaction commit];
}
/// Compose a CATransform3D from EaseViewProps target values.
- (CATransform3D)targetTransformFromProps:(const EaseViewProps &)p {
return composeTransform(
p.animateScaleX, p.animateScaleY, p.animateTranslateX,
p.animateTranslateY, degreesToRadians(p.animateRotate),
degreesToRadians(p.animateRotateX), degreesToRadians(p.animateRotateY),
_transformPerspective);
}
/// Compose a CATransform3D from EaseViewProps initial values.
- (CATransform3D)initialTransformFromProps:(const EaseViewProps &)p {
return composeTransform(
p.initialAnimateScaleX, p.initialAnimateScaleY,
p.initialAnimateTranslateX, p.initialAnimateTranslateY,
degreesToRadians(p.initialAnimateRotate),
degreesToRadians(p.initialAnimateRotateX),
degreesToRadians(p.initialAnimateRotateY), _transformPerspective);
}
- (void)beginAnimationBatch {
if (_pendingAnimationCount > 0 && _eventEmitter) {
auto emitter =
std::static_pointer_cast<const EaseViewEventEmitter>(_eventEmitter);
emitter->onTransitionEnd(EaseViewEventEmitter::OnTransitionEnd{
.finished = false,
});
}
_animationBatchId++;
_pendingAnimationCount = 0;
_anyInterrupted = NO;
}
- (void)applyFirstMountProps:(const EaseViewProps &)viewProps {
int mask = viewProps.animatedProperties;
BOOL hasTransform = (mask & kMaskAnyTransform) != 0;
// Check if initial differs from target for any masked property
BOOL hasInitialOpacity =
(mask & kMaskOpacity) &&
viewProps.initialAnimateOpacity != viewProps.animateOpacity;
BOOL hasInitialBorderRadius =
(mask & kMaskBorderRadius) &&
viewProps.initialAnimateBorderRadius != viewProps.animateBorderRadius;
BOOL hasInitialBackgroundColor = (mask & kMaskBackgroundColor) &&
viewProps.initialAnimateBackgroundColor !=
viewProps.animateBackgroundColor;
BOOL hasInitialBorderWidth =
(mask & kMaskBorderWidth) &&
viewProps.initialAnimateBorderWidth != viewProps.animateBorderWidth;
BOOL hasInitialBorderColor =
(mask & kMaskBorderColor) &&
viewProps.initialAnimateBorderColor != viewProps.animateBorderColor;
BOOL hasInitialShadowOpacity =
(mask & kMaskShadowOpacity) &&
viewProps.initialAnimateShadowOpacity != viewProps.animateShadowOpacity;
BOOL hasInitialShadowRadius =
(mask & kMaskShadowRadius) &&
viewProps.initialAnimateShadowRadius != viewProps.animateShadowRadius;
BOOL hasInitialShadowColor =
(mask & kMaskShadowColor) &&
viewProps.initialAnimateShadowColor != viewProps.animateShadowColor;
BOOL hasInitialShadowOffset =
(mask & kMaskShadowOffset) &&
(viewProps.initialAnimateShadowOffsetX !=
viewProps.animateShadowOffsetX ||
viewProps.initialAnimateShadowOffsetY != viewProps.animateShadowOffsetY);
BOOL hasInitialTransform = NO;
CATransform3D initialT = CATransform3DIdentity;
CATransform3D targetT = CATransform3DIdentity;
int changedInitTransform = 0;
if (hasTransform) {
initialT = [self initialTransformFromProps:viewProps];
targetT = [self targetTransformFromProps:viewProps];
// Compare raw prop values (not composed matrices) so that e.g.
// rotate 0→360 is correctly detected as a change even though the
// resulting CATransform3D matrices are identical.
if (viewProps.initialAnimateTranslateX != viewProps.animateTranslateX)
changedInitTransform |= kMaskTranslateX;
if (viewProps.initialAnimateTranslateY != viewProps.animateTranslateY)
changedInitTransform |= kMaskTranslateY;
if (viewProps.initialAnimateScaleX != viewProps.animateScaleX)
changedInitTransform |= kMaskScaleX;
if (viewProps.initialAnimateScaleY != viewProps.animateScaleY)
changedInitTransform |= kMaskScaleY;
if (viewProps.initialAnimateRotate != viewProps.animateRotate)
changedInitTransform |= kMaskRotate;
if (viewProps.initialAnimateRotateX != viewProps.animateRotateX)
changedInitTransform |= kMaskRotateX;
if (viewProps.initialAnimateRotateY != viewProps.animateRotateY)
changedInitTransform |= kMaskRotateY;
hasInitialTransform = changedInitTransform != 0;
}
if (hasInitialOpacity || hasInitialTransform || hasInitialBorderRadius ||
hasInitialBackgroundColor || hasInitialBorderWidth ||
hasInitialBorderColor || hasInitialShadowOpacity ||
hasInitialShadowRadius || hasInitialShadowColor ||
hasInitialShadowOffset) {
// Set initial values after props and layout have settled for this mount.
if (mask & kMaskOpacity)
self.layer.opacity = viewProps.initialAnimateOpacity;
if (hasTransform)
self.layer.transform = [self initialTransformFromProps:viewProps];
if (mask & kMaskBorderRadius) {
self.layer.cornerRadius = viewProps.initialAnimateBorderRadius;
}
if (mask & kMaskBackgroundColor)
self.layer.backgroundColor =
RCTUIColorFromSharedColor(viewProps.initialAnimateBackgroundColor)
.CGColor;
if (mask & kMaskBorderWidth)
self.layer.borderWidth = viewProps.initialAnimateBorderWidth;
if (mask & kMaskBorderColor)
self.layer.borderColor =
RCTUIColorFromSharedColor(viewProps.initialAnimateBorderColor)
.CGColor;
if (mask & kMaskShadowOpacity)
self.layer.shadowOpacity = viewProps.initialAnimateShadowOpacity;
if (mask & kMaskShadowRadius)
self.layer.shadowRadius = viewProps.initialAnimateShadowRadius;
if (mask & kMaskShadowColor)
self.layer.shadowColor =
RCTUIColorFromSharedColor(viewProps.initialAnimateShadowColor)
.CGColor;
if (mask & kMaskShadowOffset)
self.layer.shadowOffset =
CGSizeMake(viewProps.initialAnimateShadowOffsetX,
viewProps.initialAnimateShadowOffsetY);
// Animate from initial to target (skip if config is 'none')
if (hasInitialOpacity) {
EaseTransitionConfig opacityConfig =
transitionConfigForProperty("opacity", viewProps);
self.layer.opacity = viewProps.animateOpacity;
if (opacityConfig.type != "none") {
[self applyAnimationForKeyPath:@"opacity"
animationKey:kAnimKeyOpacity
fromValue:@(viewProps.initialAnimateOpacity)
toValue:@(viewProps.animateOpacity)
config:opacityConfig
loop:YES];
}
}
if (hasInitialTransform) {
std::string transformName =
lowestTransformPropertyName(changedInitTransform);
EaseTransitionConfig transformConfig =
transitionConfigForProperty(transformName, viewProps);
self.layer.transform = [self targetTransformFromProps:viewProps];
if (transformConfig.type != "none") {
// Animate each changed sub-property individually using key paths.
// This avoids matrix interpolation which fails for cases like
// rotate 0→360 (identical matrices, but visually a full rotation).
if (changedInitTransform & kMaskTranslateX) {
[self applyAnimationForKeyPath:@"transform.translation.x"
animationKey:kAnimKeyTransformTransX
fromValue:@(viewProps.initialAnimateTranslateX)
toValue:@(viewProps.animateTranslateX)
config:transformConfig
loop:YES];
}
if (changedInitTransform & kMaskTranslateY) {
[self applyAnimationForKeyPath:@"transform.translation.y"
animationKey:kAnimKeyTransformTransY
fromValue:@(viewProps.initialAnimateTranslateY)
toValue:@(viewProps.animateTranslateY)
config:transformConfig
loop:YES];
}
if (changedInitTransform & kMaskScaleX) {
[self applyAnimationForKeyPath:@"transform.scale.x"
animationKey:kAnimKeyTransformScaleX
fromValue:@(viewProps.initialAnimateScaleX)
toValue:@(viewProps.animateScaleX)
config:transformConfig
loop:YES];
}
if (changedInitTransform & kMaskScaleY) {
[self applyAnimationForKeyPath:@"transform.scale.y"
animationKey:kAnimKeyTransformScaleY
fromValue:@(viewProps.initialAnimateScaleY)
toValue:@(viewProps.animateScaleY)
config:transformConfig
loop:YES];
}
if (changedInitTransform & kMaskRotate) {
[self applyAnimationForKeyPath:@"transform.rotation.z"
animationKey:kAnimKeyTransformRotateZ
fromValue:@(degreesToRadians(
viewProps.initialAnimateRotate))
toValue:@(degreesToRadians(
viewProps.animateRotate))
config:transformConfig
loop:YES];
}
if (changedInitTransform & kMaskRotateX) {
[self applyAnimationForKeyPath:@"transform.rotation.x"
animationKey:kAnimKeyTransformRotateX
fromValue:@(degreesToRadians(
viewProps.initialAnimateRotateX))
toValue:@(degreesToRadians(
viewProps.animateRotateX))
config:transformConfig
loop:YES];
}
if (changedInitTransform & kMaskRotateY) {
[self applyAnimationForKeyPath:@"transform.rotation.y"
animationKey:kAnimKeyTransformRotateY
fromValue:@(degreesToRadians(
viewProps.initialAnimateRotateY))
toValue:@(degreesToRadians(
viewProps.animateRotateY))
config:transformConfig
loop:YES];
}
}
}
if (hasInitialBorderRadius) {
EaseTransitionConfig brConfig =
transitionConfigForProperty("borderRadius", viewProps);
self.layer.cornerRadius = viewProps.animateBorderRadius;
if (brConfig.type != "none") {
[self applyAnimationForKeyPath:@"cornerRadius"
animationKey:kAnimKeyCornerRadius
fromValue:@(viewProps.initialAnimateBorderRadius)
toValue:@(viewProps.animateBorderRadius)
config:brConfig
loop:YES];
}
}
if (hasInitialBackgroundColor) {
EaseTransitionConfig bgConfig =
transitionConfigForProperty("backgroundColor", viewProps);
self.layer.backgroundColor =
RCTUIColorFromSharedColor(viewProps.animateBackgroundColor).CGColor;
if (bgConfig.type != "none") {
[self applyAnimationForKeyPath:@"backgroundColor"
animationKey:kAnimKeyBackgroundColor
fromValue:(__bridge id)RCTUIColorFromSharedColor(
viewProps
.initialAnimateBackgroundColor)
.CGColor
toValue:(__bridge id)RCTUIColorFromSharedColor(
viewProps.animateBackgroundColor)
.CGColor
config:bgConfig
loop:YES];
}
}
if (hasInitialBorderWidth) {
EaseTransitionConfig config =
transitionConfigForProperty("borderWidth", viewProps);
self.layer.borderWidth = viewProps.animateBorderWidth;
if (config.type != "none") {
[self applyAnimationForKeyPath:@"borderWidth"
animationKey:kAnimKeyBorderWidth
fromValue:@(viewProps.initialAnimateBorderWidth)
toValue:@(viewProps.animateBorderWidth)
config:config
loop:YES];
}
}
if (hasInitialBorderColor) {
EaseTransitionConfig config =
transitionConfigForProperty("borderColor", viewProps);
self.layer.borderColor =
RCTUIColorFromSharedColor(viewProps.animateBorderColor).CGColor;
if (config.type != "none") {
[self applyAnimationForKeyPath:@"borderColor"
animationKey:kAnimKeyBorderColor
fromValue:(__bridge id)RCTUIColorFromSharedColor(
viewProps.initialAnimateBorderColor)
.CGColor
toValue:(__bridge id)RCTUIColorFromSharedColor(
viewProps.animateBorderColor)
.CGColor
config:config
loop:YES];
}
}
if (hasInitialShadowOpacity) {
EaseTransitionConfig config =
transitionConfigForProperty("shadowOpacity", viewProps);
self.layer.shadowOpacity = viewProps.animateShadowOpacity;
if (config.type != "none") {
[self applyAnimationForKeyPath:@"shadowOpacity"
animationKey:kAnimKeyShadowOpacity
fromValue:@(viewProps.initialAnimateShadowOpacity)
toValue:@(viewProps.animateShadowOpacity)
config:config
loop:YES];
}
}
if (hasInitialShadowRadius) {
EaseTransitionConfig config =
transitionConfigForProperty("shadowRadius", viewProps);
self.layer.shadowRadius = viewProps.animateShadowRadius;
if (config.type != "none") {
[self applyAnimationForKeyPath:@"shadowRadius"
animationKey:kAnimKeyShadowRadius
fromValue:@(viewProps.initialAnimateShadowRadius)
toValue:@(viewProps.animateShadowRadius)
config:config
loop:YES];
}
}
if (hasInitialShadowColor) {
EaseTransitionConfig config =
transitionConfigForProperty("shadowColor", viewProps);
self.layer.shadowColor =
RCTUIColorFromSharedColor(viewProps.animateShadowColor).CGColor;
if (config.type != "none") {
[self applyAnimationForKeyPath:@"shadowColor"
animationKey:kAnimKeyShadowColor
fromValue:(__bridge id)RCTUIColorFromSharedColor(
viewProps.initialAnimateShadowColor)
.CGColor
toValue:(__bridge id)RCTUIColorFromSharedColor(
viewProps.animateShadowColor)
.CGColor
config:config
loop:YES];
}
}
if (hasInitialShadowOffset) {
EaseTransitionConfig config =
transitionConfigForProperty("shadowOffset", viewProps);
CGSize targetOffset = CGSizeMake(viewProps.animateShadowOffsetX,
viewProps.animateShadowOffsetY);
self.layer.shadowOffset = targetOffset;
if (config.type != "none") {
CGSize initialOffset =
CGSizeMake(viewProps.initialAnimateShadowOffsetX,
viewProps.initialAnimateShadowOffsetY);
[self applyAnimationForKeyPath:@"shadowOffset"
animationKey:kAnimKeyShadowOffset
fromValue:[NSValue valueWithCGSize:initialOffset]
toValue:[NSValue valueWithCGSize:targetOffset]
config:config
loop:YES];
}
}
// If all per-property configs were 'none', no animations were queued.
// Fire onTransitionEnd immediately to match the scalar 'none' contract.
if (_pendingAnimationCount == 0 && _eventEmitter) {
auto emitter =
std::static_pointer_cast<const EaseViewEventEmitter>(_eventEmitter);
emitter->onTransitionEnd(EaseViewEventEmitter::OnTransitionEnd{
.finished = true,
});
}
} else {
// No initial animation — set target values directly
if (mask & kMaskOpacity)
self.layer.opacity = viewProps.animateOpacity;
if (hasTransform)
self.layer.transform = [self targetTransformFromProps:viewProps];
if (mask & kMaskBorderRadius)
self.layer.cornerRadius = viewProps.animateBorderRadius;
if (mask & kMaskBackgroundColor)
self.layer.backgroundColor =
RCTUIColorFromSharedColor(viewProps.animateBackgroundColor).CGColor;
if (mask & kMaskBorderWidth)
self.layer.borderWidth = viewProps.animateBorderWidth;
if (mask & kMaskBorderColor)
self.layer.borderColor =
RCTUIColorFromSharedColor(viewProps.animateBorderColor).CGColor;
if (mask & kMaskShadowOpacity)
self.layer.shadowOpacity = viewProps.animateShadowOpacity;
if (mask & kMaskShadowRadius)
self.layer.shadowRadius = viewProps.animateShadowRadius;
if (mask & kMaskShadowColor)
self.layer.shadowColor =
RCTUIColorFromSharedColor(viewProps.animateShadowColor).CGColor;
if (mask & kMaskShadowOffset)
self.layer.shadowOffset = CGSizeMake(viewProps.animateShadowOffsetX,
viewProps.animateShadowOffsetY);
}
}
- (void)tryApplyPendingFirstMountProps {
if (!_hasPendingFirstMountUpdate || !_isFirstMount || self.window == nil) {
return;
}
const auto &viewProps =
*std::static_pointer_cast<const EaseViewProps>(_props);
[CATransaction begin];
[CATransaction setDisableActions:YES];
[self beginAnimationBatch];
[self applyFirstMountProps:viewProps];
_hasPendingFirstMountUpdate = NO;
_isFirstMount = NO;
[CATransaction commit];
}
#pragma mark - Props update
- (void)updateProps:(const Props::Shared &)props
oldProps:(const Props::Shared &)oldProps {
const auto &newViewProps =
*std::static_pointer_cast<const EaseViewProps>(props);
// oldProps can be null. Fall back to props so the diff is a no-op.
const auto &oldViewProps = *std::static_pointer_cast<const EaseViewProps>(
oldProps ? oldProps : props);
[super updateProps:props oldProps:oldProps];
[CATransaction begin];
[CATransaction setDisableActions:YES];
if (_transformOriginX != newViewProps.transformOriginX ||
_transformOriginY != newViewProps.transformOriginY) {
_transformOriginX = newViewProps.transformOriginX;
_transformOriginY = newViewProps.transformOriginY;
[self updateAnchorPoint];
}
if (_transformPerspective != newViewProps.transformPerspective) {
_transformPerspective = newViewProps.transformPerspective;
}
// Bitmask: which properties are animated. Non-animated = let style handle.
int mask = newViewProps.animatedProperties;
BOOL hasTransform = (mask & kMaskAnyTransform) != 0;
if (_isFirstMount) {
// Delay enter animations until finalizeUpdates so Fabric has already
// applied props and layout metrics for this mount transaction.
_hasPendingFirstMountUpdate = YES;
} else if (newViewProps.transitions.defaultConfig.type == "none" &&
(!hasConfig(newViewProps.transitions.transform) ||
newViewProps.transitions.transform.type == "none") &&
(!hasConfig(newViewProps.transitions.opacity) ||
newViewProps.transitions.opacity.type == "none") &&
(!hasConfig(newViewProps.transitions.borderRadius) ||
newViewProps.transitions.borderRadius.type == "none") &&
(!hasConfig(newViewProps.transitions.backgroundColor) ||
newViewProps.transitions.backgroundColor.type == "none") &&
(!hasConfig(newViewProps.transitions.border) ||
newViewProps.transitions.border.type == "none") &&
(!hasConfig(newViewProps.transitions.shadow) ||
newViewProps.transitions.shadow.type == "none")) {
// All transitions are 'none' — set values immediately
[self beginAnimationBatch];
[self.layer removeAllAnimations];
// Drop saved loop snapshots so didMoveToWindow doesn't re-add the
// cancelled loops.
[_loopAnimations removeAllObjects];
if (mask & kMaskOpacity)
self.layer.opacity = newViewProps.animateOpacity;
if (hasTransform)
self.layer.transform = [self targetTransformFromProps:newViewProps];
if (mask & kMaskBorderRadius)
self.layer.cornerRadius = newViewProps.animateBorderRadius;
if (mask & kMaskBackgroundColor)
self.layer.backgroundColor =
RCTUIColorFromSharedColor(newViewProps.animateBackgroundColor)
.CGColor;
if (mask & kMaskBorderWidth)
self.layer.borderWidth = newViewProps.animateBorderWidth;
if (mask & kMaskBorderColor)
self.layer.borderColor =
RCTUIColorFromSharedColor(newViewProps.animateBorderColor).CGColor;
if (mask & kMaskShadowOpacity)
self.layer.shadowOpacity = newViewProps.animateShadowOpacity;
if (mask & kMaskShadowRadius)
self.layer.shadowRadius = newViewProps.animateShadowRadius;
if (mask & kMaskShadowColor)
self.layer.shadowColor =
RCTUIColorFromSharedColor(newViewProps.animateShadowColor).CGColor;
if (mask & kMaskShadowOffset)
self.layer.shadowOffset = CGSizeMake(newViewProps.animateShadowOffsetX,
newViewProps.animateShadowOffsetY);
if (_eventEmitter) {
auto emitter =
std::static_pointer_cast<const EaseViewEventEmitter>(_eventEmitter);
emitter->onTransitionEnd(EaseViewEventEmitter::OnTransitionEnd{
.finished = true,
});
}
} else {
// Subsequent updates: animate changed properties
[self beginAnimationBatch];
BOOL anyPropertyChanged = NO;
if ((mask & kMaskOpacity) &&
oldViewProps.animateOpacity != newViewProps.animateOpacity) {
anyPropertyChanged = YES;
EaseTransitionConfig opacityConfig =
transitionConfigForProperty("opacity", newViewProps);
if (opacityConfig.type == "none") {
self.layer.opacity = newViewProps.animateOpacity;
[self removeEaseAnimationForKey:kAnimKeyOpacity];
} else {
self.layer.opacity = newViewProps.animateOpacity;
[self
applyAnimationForKeyPath:@"opacity"
animationKey:kAnimKeyOpacity
fromValue:[self
presentationValueForKeyPath:@"opacity"]
toValue:@(newViewProps.animateOpacity)
config:opacityConfig
loop:NO];
}
}
// Check if ANY transform-related property changed
if (hasTransform) {
BOOL anyTransformChanged =
oldViewProps.animateTranslateX != newViewProps.animateTranslateX ||
oldViewProps.animateTranslateY != newViewProps.animateTranslateY ||
oldViewProps.animateScaleX != newViewProps.animateScaleX ||
oldViewProps.animateScaleY != newViewProps.animateScaleY ||
oldViewProps.animateRotate != newViewProps.animateRotate ||
oldViewProps.animateRotateX != newViewProps.animateRotateX ||
oldViewProps.animateRotateY != newViewProps.animateRotateY;
if (anyTransformChanged) {
anyPropertyChanged = YES;
// Determine which transform sub-properties changed for config selection
int changedTransformMask = 0;
if (oldViewProps.animateTranslateX != newViewProps.animateTranslateX)
changedTransformMask |= kMaskTranslateX;
if (oldViewProps.animateTranslateY != newViewProps.animateTranslateY)
changedTransformMask |= kMaskTranslateY;
if (oldViewProps.animateScaleX != newViewProps.animateScaleX)
changedTransformMask |= kMaskScaleX;
if (oldViewProps.animateScaleY != newViewProps.animateScaleY)
changedTransformMask |= kMaskScaleY;
if (oldViewProps.animateRotate != newViewProps.animateRotate)
changedTransformMask |= kMaskRotate;
if (oldViewProps.animateRotateX != newViewProps.animateRotateX)
changedTransformMask |= kMaskRotateX;
if (oldViewProps.animateRotateY != newViewProps.animateRotateY)
changedTransformMask |= kMaskRotateY;
std::string transformName =
lowestTransformPropertyName(changedTransformMask);
EaseTransitionConfig transformConfig =
transitionConfigForProperty(transformName, newViewProps);
if (transformConfig.type == "none") {
self.layer.transform = [self targetTransformFromProps:newViewProps];
[self removeEaseAnimationForKey:kAnimKeyTransform];
} else {
// Read "from" values from the presentation layer BEFORE setting
// the new model transform. During an active animation, CA tracks
// key-path values correctly. After completion, the model matrix
// with m34 can't be reliably decomposed, so fall back to old props.
BOOL isAnimating =
[self.layer animationForKey:kAnimKeyTransformRotateY] != nil ||
[self.layer animationForKey:kAnimKeyTransformRotateX] != nil ||
[self.layer animationForKey:kAnimKeyTransformRotateZ] != nil ||
[self.layer animationForKey:kAnimKeyTransformTransX] != nil ||
[self.layer animationForKey:kAnimKeyTransformTransY] != nil ||
[self.layer animationForKey:kAnimKeyTransformScaleX] != nil ||
[self.layer animationForKey:kAnimKeyTransformScaleY] != nil;
CGFloat fromTX, fromTY, fromSX, fromSY, fromR, fromRX, fromRY;
if (isAnimating) {
CALayer *pl = self.layer.presentationLayer ?: self.layer;
fromTX =
[[pl valueForKeyPath:@"transform.translation.x"] floatValue];
fromTY =
[[pl valueForKeyPath:@"transform.translation.y"] floatValue];
fromSX = [[pl valueForKeyPath:@"transform.scale.x"] floatValue];
fromSY = [[pl valueForKeyPath:@"transform.scale.y"] floatValue];
fromR = [[pl valueForKeyPath:@"transform.rotation"] floatValue];
fromRX = [[pl valueForKeyPath:@"transform.rotation.x"] floatValue];
fromRY = [[pl valueForKeyPath:@"transform.rotation.y"] floatValue];
} else {
fromTX = oldViewProps.animateTranslateX;
fromTY = oldViewProps.animateTranslateY;
fromSX = oldViewProps.animateScaleX;
fromSY = oldViewProps.animateScaleY;
fromR = degreesToRadians(oldViewProps.animateRotate);
fromRX = degreesToRadians(oldViewProps.animateRotateX);
fromRY = degreesToRadians(oldViewProps.animateRotateY);
}
self.layer.transform = [self targetTransformFromProps:newViewProps];
if (changedTransformMask & kMaskTranslateX) {
[self applyAnimationForKeyPath:@"transform.translation.x"
animationKey:kAnimKeyTransformTransX
fromValue:@(fromTX)
toValue:@(newViewProps.animateTranslateX)
config:transformConfig
loop:NO];
}
if (changedTransformMask & kMaskTranslateY) {
[self applyAnimationForKeyPath:@"transform.translation.y"
animationKey:kAnimKeyTransformTransY
fromValue:@(fromTY)
toValue:@(newViewProps.animateTranslateY)
config:transformConfig
loop:NO];
}
if (changedTransformMask & kMaskScaleX) {
[self applyAnimationForKeyPath:@"transform.scale.x"
animationKey:kAnimKeyTransformScaleX
fromValue:@(fromSX)
toValue:@(newViewProps.animateScaleX)
config:transformConfig
loop:NO];
}
if (changedTransformMask & kMaskScaleY) {
[self applyAnimationForKeyPath:@"transform.scale.y"
animationKey:kAnimKeyTransformScaleY
fromValue:@(fromSY)
toValue:@(newViewProps.animateScaleY)
config:transformConfig