From 5fd15b33b51f0fdede6c7bcd3dda1471b9a9b7cf Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Sun, 21 Apr 2019 20:41:45 -0400 Subject: [PATCH 1/3] Native Animated - Support events using RCT{Direct|Bubbling}EventBlock on iOS --- .../RCTNativeAnimatedNodesManager.m | 21 ++++++++++++++++--- .../NativeAnimationsExample.js | 13 +++++++++--- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m b/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m index 1a47f1b7abd2ef..039112185b409d 100644 --- a/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m +++ b/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m @@ -28,6 +28,21 @@ #import "RCTValueAnimatedNode.h" #import "RCTTrackingAnimatedNode.h" +// We de some normalizing of the event names in RCTEventDispatcher#RCTNormalizeInputEventName. +// To make things simpler just get rid of the parts we change in the event names we use here. +// This is a lot easier than trying to denormalize because there would be multiple possible +// denormalized forms for a single input. +NSString *RCTNormalizeAnimatedEventName(NSString *eventName) +{ + if ([eventName hasPrefix:@"on"]) { + return [eventName substringFromIndex:2]; + } + if ([eventName hasPrefix:@"top"]) { + return [eventName substringFromIndex:3]; + } + return eventName; +} + @implementation RCTNativeAnimatedNodesManager { __weak RCTBridge *_bridge; @@ -321,7 +336,7 @@ - (void)addAnimatedEventToView:(nonnull NSNumber *)viewTag RCTEventAnimation *driver = [[RCTEventAnimation alloc] initWithEventPath:eventPath valueNode:(RCTValueAnimatedNode *)node]; - NSString *key = [NSString stringWithFormat:@"%@%@", viewTag, eventName]; + NSString *key = [NSString stringWithFormat:@"%@%@", viewTag, RCTNormalizeAnimatedEventName(eventName)]; if (_eventDrivers[key] != nil) { [_eventDrivers[key] addObject:driver]; } else { @@ -335,7 +350,7 @@ - (void)removeAnimatedEventFromView:(nonnull NSNumber *)viewTag eventName:(nonnull NSString *)eventName animatedNodeTag:(nonnull NSNumber *)animatedNodeTag { - NSString *key = [NSString stringWithFormat:@"%@%@", viewTag, eventName]; + NSString *key = [NSString stringWithFormat:@"%@%@", viewTag, RCTNormalizeAnimatedEventName(eventName)]; if (_eventDrivers[key] != nil) { if (_eventDrivers[key].count == 1) { [_eventDrivers removeObjectForKey:key]; @@ -357,7 +372,7 @@ - (void)handleAnimatedEvent:(id)event return; } - NSString *key = [NSString stringWithFormat:@"%@%@", event.viewTag, event.eventName]; + NSString *key = [NSString stringWithFormat:@"%@%@", event.viewTag, RCTNormalizeAnimatedEventName(event.eventName)]; NSMutableArray *driversForKey = _eventDrivers[key]; if (driversForKey) { for (RCTEventAnimation *driver in driversForKey) { diff --git a/RNTester/js/examples/NativeAnimation/NativeAnimationsExample.js b/RNTester/js/examples/NativeAnimation/NativeAnimationsExample.js index 2e1fcd22b78c29..acec03baa04dfa 100644 --- a/RNTester/js/examples/NativeAnimation/NativeAnimationsExample.js +++ b/RNTester/js/examples/NativeAnimation/NativeAnimationsExample.js @@ -221,7 +221,7 @@ class InternalSettings extends React.Component< class EventExample extends React.Component<{}, $FlowFixMeState> { state = { - scrollX: new Animated.Value(0), + anim: new Animated.Value(0), }; render() { @@ -233,7 +233,7 @@ class EventExample extends React.Component<{}, $FlowFixMeState> { { transform: [ { - rotate: this.state.scrollX.interpolate({ + rotate: this.state.anim.interpolate({ inputRange: [0, 1], outputRange: ['0deg', '1deg'], }), @@ -246,7 +246,7 @@ class EventExample extends React.Component<{}, $FlowFixMeState> { horizontal style={{height: 100, marginTop: 16}} onScroll={Animated.event( - [{nativeEvent: {contentOffset: {x: this.state.scrollX}}}], + [{nativeEvent: {contentOffset: {x: this.state.anim}}}], {useNativeDriver: true}, )}> { Scroll me sideways! + ); } From 4fe14cf8b05d4a528287f591a5a8e36d0460755a Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Mon, 22 Apr 2019 09:27:11 -0400 Subject: [PATCH 2/3] Fix typo --- Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m b/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m index 039112185b409d..c7fec8f7125925 100644 --- a/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m +++ b/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m @@ -28,7 +28,7 @@ #import "RCTValueAnimatedNode.h" #import "RCTTrackingAnimatedNode.h" -// We de some normalizing of the event names in RCTEventDispatcher#RCTNormalizeInputEventName. +// We do some normalizing of the event names in RCTEventDispatcher#RCTNormalizeInputEventName. // To make things simpler just get rid of the parts we change in the event names we use here. // This is a lot easier than trying to denormalize because there would be multiple possible // denormalized forms for a single input. From 70e2e99b75bb18bad7ea6ee7ff61c2910ab88720 Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Wed, 19 Jun 2019 14:04:55 -0400 Subject: [PATCH 3/3] Add missing prototype clang warning, add missing static --- .../NativeAnimation/RCTAnimation.xcodeproj/project.pbxproj | 3 +++ Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Libraries/NativeAnimation/RCTAnimation.xcodeproj/project.pbxproj b/Libraries/NativeAnimation/RCTAnimation.xcodeproj/project.pbxproj index d74745fb037bf6..5baff15c662d82 100644 --- a/Libraries/NativeAnimation/RCTAnimation.xcodeproj/project.pbxproj +++ b/Libraries/NativeAnimation/RCTAnimation.xcodeproj/project.pbxproj @@ -426,6 +426,7 @@ developmentRegion = English; hasScannedForEncodings = 0; knownRegions = ( + English, en, ); mainGroup = 58B511D21A9E6C8500147676; @@ -566,6 +567,7 @@ ); GCC_SYMBOLS_PRIVATE_EXTERN = NO; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED = YES; GCC_WARN_SHADOW = YES; @@ -619,6 +621,7 @@ GCC_C_LANGUAGE_STANDARD = gnu99; GCC_NO_COMMON_BLOCKS = YES; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED = YES; GCC_WARN_SHADOW = YES; diff --git a/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m b/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m index c7fec8f7125925..c103c26f2e8e9f 100644 --- a/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m +++ b/Libraries/NativeAnimation/RCTNativeAnimatedNodesManager.m @@ -32,7 +32,7 @@ // To make things simpler just get rid of the parts we change in the event names we use here. // This is a lot easier than trying to denormalize because there would be multiple possible // denormalized forms for a single input. -NSString *RCTNormalizeAnimatedEventName(NSString *eventName) +static NSString *RCTNormalizeAnimatedEventName(NSString *eventName) { if ([eventName hasPrefix:@"on"]) { return [eventName substringFromIndex:2];