Skip to content

Commit 1fb4b6c

Browse files
janicduplessisfacebook-github-bot
authored andcommitted
Native Animated - Support events using RCT{Direct|Bubbling}EventBlock on iOS (#25317)
Summary: Reland #15611 and added the gcc warning that was different from fb internal config. The original PR missed the static keyword for the `RCTNormalizeAnimatedEventName` function which triggered the gcc warning internally but not with the OSS xcode config. When calling a prop of type `RCTDirectEventBlock` or `RCTBubblingEventBlock` it uses a completely different code path than events using `[RCTEventDispatcher sendEvent:]` and those were not dispatched to the `RCTEventDispatcherListener`s. We also do some event name normalization which caused issues between the JS and native event names. To fix that I simply remove the parts we normalize from the event key. ## Changelog: [iOS] [Fixed] - Support events using RCT{Direct|Bubbling}EventBlock Pull Request resolved: #25317 Test Plan: Added a Slider (it uses RCTBubblingEventBlock for its onValueChange event) that can control a native animated value in RNTester to reproduce the bug and made sure this diff fixes it. Differential Revision: D15938856 Pulled By: cpojer fbshipit-source-id: 7e7a3459e2a2e8b1254a2f1ec8153a159ea73eed
1 parent d010fc3 commit 1fb4b6c

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

Libraries/NativeAnimation/RCTAnimation.xcodeproj/project.pbxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,7 @@
426426
developmentRegion = English;
427427
hasScannedForEncodings = 0;
428428
knownRegions = (
429+
English,
429430
en,
430431
);
431432
mainGroup = 58B511D21A9E6C8500147676;
@@ -566,6 +567,7 @@
566567
);
567568
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
568569
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
570+
GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES;
569571
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
570572
GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED = YES;
571573
GCC_WARN_SHADOW = YES;
@@ -619,6 +621,7 @@
619621
GCC_C_LANGUAGE_STANDARD = gnu99;
620622
GCC_NO_COMMON_BLOCKS = YES;
621623
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
624+
GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES;
622625
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
623626
GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED = YES;
624627
GCC_WARN_SHADOW = YES;

Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@
2828
#import "RCTValueAnimatedNode.h"
2929
#import "RCTTrackingAnimatedNode.h"
3030

31+
// We do some normalizing of the event names in RCTEventDispatcher#RCTNormalizeInputEventName.
32+
// To make things simpler just get rid of the parts we change in the event names we use here.
33+
// This is a lot easier than trying to denormalize because there would be multiple possible
34+
// denormalized forms for a single input.
35+
static NSString *RCTNormalizeAnimatedEventName(NSString *eventName)
36+
{
37+
if ([eventName hasPrefix:@"on"]) {
38+
return [eventName substringFromIndex:2];
39+
}
40+
if ([eventName hasPrefix:@"top"]) {
41+
return [eventName substringFromIndex:3];
42+
}
43+
return eventName;
44+
}
45+
3146
@implementation RCTNativeAnimatedNodesManager
3247
{
3348
__weak RCTBridge *_bridge;
@@ -324,7 +339,7 @@ - (void)addAnimatedEventToView:(nonnull NSNumber *)viewTag
324339
RCTEventAnimation *driver =
325340
[[RCTEventAnimation alloc] initWithEventPath:eventPath valueNode:(RCTValueAnimatedNode *)node];
326341

327-
NSString *key = [NSString stringWithFormat:@"%@%@", viewTag, eventName];
342+
NSString *key = [NSString stringWithFormat:@"%@%@", viewTag, RCTNormalizeAnimatedEventName(eventName)];
328343
if (_eventDrivers[key] != nil) {
329344
[_eventDrivers[key] addObject:driver];
330345
} else {
@@ -338,7 +353,7 @@ - (void)removeAnimatedEventFromView:(nonnull NSNumber *)viewTag
338353
eventName:(nonnull NSString *)eventName
339354
animatedNodeTag:(nonnull NSNumber *)animatedNodeTag
340355
{
341-
NSString *key = [NSString stringWithFormat:@"%@%@", viewTag, eventName];
356+
NSString *key = [NSString stringWithFormat:@"%@%@", viewTag, RCTNormalizeAnimatedEventName(eventName)];
342357
if (_eventDrivers[key] != nil) {
343358
if (_eventDrivers[key].count == 1) {
344359
[_eventDrivers removeObjectForKey:key];
@@ -360,7 +375,7 @@ - (void)handleAnimatedEvent:(id<RCTEvent>)event
360375
return;
361376
}
362377

363-
NSString *key = [NSString stringWithFormat:@"%@%@", event.viewTag, event.eventName];
378+
NSString *key = [NSString stringWithFormat:@"%@%@", event.viewTag, RCTNormalizeAnimatedEventName(event.eventName)];
364379
NSMutableArray<RCTEventAnimation *> *driversForKey = _eventDrivers[key];
365380
if (driversForKey) {
366381
for (RCTEventAnimation *driver in driversForKey) {

RNTester/js/examples/NativeAnimation/NativeAnimationsExample.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ class InternalSettings extends React.Component<
221221

222222
class EventExample extends React.Component<{}, $FlowFixMeState> {
223223
state = {
224-
scrollX: new Animated.Value(0),
224+
anim: new Animated.Value(0),
225225
};
226226

227227
render() {
@@ -233,7 +233,7 @@ class EventExample extends React.Component<{}, $FlowFixMeState> {
233233
{
234234
transform: [
235235
{
236-
rotate: this.state.scrollX.interpolate({
236+
rotate: this.state.anim.interpolate({
237237
inputRange: [0, 1],
238238
outputRange: ['0deg', '1deg'],
239239
}),
@@ -246,7 +246,7 @@ class EventExample extends React.Component<{}, $FlowFixMeState> {
246246
horizontal
247247
style={{height: 100, marginTop: 16}}
248248
onScroll={Animated.event(
249-
[{nativeEvent: {contentOffset: {x: this.state.scrollX}}}],
249+
[{nativeEvent: {contentOffset: {x: this.state.anim}}}],
250250
{useNativeDriver: true},
251251
)}>
252252
<View
@@ -259,6 +259,13 @@ class EventExample extends React.Component<{}, $FlowFixMeState> {
259259
<Text>Scroll me sideways!</Text>
260260
</View>
261261
</Animated.ScrollView>
262+
<AnimatedSlider
263+
maximumValue={200}
264+
onValueChange={Animated.event(
265+
[{nativeEvent: {value: this.state.anim}}],
266+
{useNativeDriver: true},
267+
)}
268+
/>
262269
</View>
263270
);
264271
}

0 commit comments

Comments
 (0)