@@ -27,24 +27,50 @@ @implementation RCTTVView
2727- (instancetype )initWithFrame : (CGRect)frame
2828{
2929 if (self = [super initWithFrame: frame]) {
30- self.tvParallaxProperties = @{
31- @" enabled" : @YES ,
32- @" shiftDistanceX" : @2 .0f ,
33- @" shiftDistanceY" : @2 .0f ,
34- @" tiltAngle" : @0 .05f ,
35- @" magnification" : @1 .0f
36- };
30+ dispatch_once (&onceToken, ^{
31+ defaultTVParallaxProperties = @{
32+ @" enabled" : @YES ,
33+ @" shiftDistanceX" : @2 .0f ,
34+ @" shiftDistanceY" : @2 .0f ,
35+ @" tiltAngle" : @0 .05f ,
36+ @" magnification" : @1 .0f ,
37+ @" pressMagnification" : @1 .0f ,
38+ @" pressDuration" : @0 .3f ,
39+ @" pressDelay" : @0 .0f
40+ };
41+ });
42+ self.tvParallaxProperties = defaultTVParallaxProperties;
3743 }
3844
3945 return self;
4046}
4147
48+ static NSDictionary * defaultTVParallaxProperties = nil ;
49+ static dispatch_once_t onceToken;
50+
51+ - (void )setTvParallaxProperties : (NSDictionary *)tvParallaxProperties {
52+ if (_tvParallaxProperties == nil ) {
53+ _tvParallaxProperties = [defaultTVParallaxProperties copy ];
54+ return ;
55+ }
56+
57+ NSMutableDictionary *newParallaxProperties = [NSMutableDictionary dictionaryWithDictionary: _tvParallaxProperties];
58+ for (NSString *k in [defaultTVParallaxProperties allKeys ]) {
59+ if (tvParallaxProperties[k]) {
60+ newParallaxProperties[k] = tvParallaxProperties[k];
61+ }
62+ }
63+ _tvParallaxProperties = [newParallaxProperties copy ];
64+ }
65+
4266RCT_NOT_IMPLEMENTED (- (instancetype )initWithCoder:unused)
4367
4468- (void )setIsTVSelectable:(BOOL )isTVSelectable {
4569 self->_isTVSelectable = isTVSelectable;
46- if (isTVSelectable) {
47- UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc ] initWithTarget: self action: @selector (handleSelect: )];
70+ if (isTVSelectable) {
71+ UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc ]
72+ initWithTarget: self
73+ action: @selector (handleSelect: )];
4874 recognizer.allowedPressTypes = @[@(UIPressTypeSelect)];
4975 _selectRecognizer = recognizer;
5076 [self addGestureRecognizer: _selectRecognizer];
@@ -57,8 +83,37 @@ - (void)setIsTVSelectable:(BOOL)isTVSelectable {
5783
5884- (void )handleSelect : (__unused UIGestureRecognizer *)r
5985{
60- [[NSNotificationCenter defaultCenter ] postNotificationName: RCTTVNavigationEventNotification
61- object: @{@" eventType" :@" select" ,@" tag" :self.reactTag }];
86+ if ([self .tvParallaxProperties[@" enabled" ] boolValue ] == YES ) {
87+ float magnification = [self .tvParallaxProperties[@" magnification" ] floatValue ];
88+ float pressMagnification = [self .tvParallaxProperties[@" pressMagnification" ] floatValue ];
89+
90+ // Duration of press animation
91+ float pressDuration = [self .tvParallaxProperties[@" pressDuration" ] floatValue ];
92+
93+ // Delay of press animation
94+ float pressDelay = [self .tvParallaxProperties[@" pressDelay" ] floatValue ];
95+
96+ [[NSRunLoop currentRunLoop ] runUntilDate: [NSDate dateWithTimeIntervalSinceNow: pressDelay]];
97+
98+ [UIView animateWithDuration: (pressDuration/2 )
99+ animations: ^{
100+ self.transform = CGAffineTransformMakeScale (pressMagnification, pressMagnification);
101+ }
102+ completion: ^(__unused BOOL finished1){
103+ [UIView animateWithDuration: (pressDuration/2 )
104+ animations: ^{
105+ self.transform = CGAffineTransformMakeScale (magnification, magnification);
106+ }
107+ completion: ^(__unused BOOL finished2) {
108+ [[NSNotificationCenter defaultCenter ] postNotificationName: RCTTVNavigationEventNotification
109+ object: @{@" eventType" :@" select" ,@" tag" :self.reactTag }];
110+ }];
111+ }];
112+
113+ } else {
114+ [[NSNotificationCenter defaultCenter ] postNotificationName: RCTTVNavigationEventNotification
115+ object: @{@" eventType" :@" select" ,@" tag" :self.reactTag }];
116+ }
62117}
63118
64119- (BOOL )isUserInteractionEnabled
@@ -79,23 +134,25 @@ - (void)addParallaxMotionEffects
79134
80135 // Make horizontal movements shift the centre left and right
81136 UIInterpolatingMotionEffect *xShift = [[UIInterpolatingMotionEffect alloc ]
82- initWithKeyPath: @" center.x"
83- type: UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
137+ initWithKeyPath: @" center.x"
138+ type: UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
84139 xShift.minimumRelativeValue = @( shiftDistanceX * -1 .0f );
85140 xShift.maximumRelativeValue = @( shiftDistanceX);
86141
87142 // Make vertical movements shift the centre up and down
88143 UIInterpolatingMotionEffect *yShift = [[UIInterpolatingMotionEffect alloc ]
89- initWithKeyPath: @" center.y"
90- type: UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
144+ initWithKeyPath: @" center.y"
145+ type: UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
91146 yShift.minimumRelativeValue = @( shiftDistanceY * -1 .0f );
92147 yShift.maximumRelativeValue = @( shiftDistanceY);
93148
94149 // Size of tilt movements
95150 CGFloat const tiltAngle = [self .tvParallaxProperties[@" tiltAngle" ] floatValue ];
96151
97152 // Now make horizontal movements effect a rotation about the Y axis for side-to-side rotation.
98- UIInterpolatingMotionEffect *xTilt = [[UIInterpolatingMotionEffect alloc ] initWithKeyPath: @" layer.transform" type: UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
153+ UIInterpolatingMotionEffect *xTilt = [[UIInterpolatingMotionEffect alloc ]
154+ initWithKeyPath: @" layer.transform"
155+ type: UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
99156
100157 // CATransform3D value for minimumRelativeValue
101158 CATransform3D transMinimumTiltAboutY = CATransform3DIdentity;
@@ -112,7 +169,9 @@ - (void)addParallaxMotionEffects
112169 xTilt.maximumRelativeValue = [NSValue valueWithCATransform3D: transMaximumTiltAboutY];
113170
114171 // Now make vertical movements effect a rotation about the X axis for up and down rotation.
115- UIInterpolatingMotionEffect *yTilt = [[UIInterpolatingMotionEffect alloc ] initWithKeyPath: @" layer.transform" type: UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
172+ UIInterpolatingMotionEffect *yTilt = [[UIInterpolatingMotionEffect alloc ]
173+ initWithKeyPath: @" layer.transform"
174+ type: UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
116175
117176 // CATransform3D value for minimumRelativeValue
118177 CATransform3D transMinimumTiltAboutX = CATransform3DIdentity;
0 commit comments