Skip to content

Commit d9357a0

Browse files
authored
Merge pull request #55 from bamlab/feature/native-animation
feat(useNativeDriver): add possibility of using native driver for ani…
2 parents 13cdc27 + d7f9f52 commit d9357a0

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ The `HeaderImageScrollView` handle also the following props. None is required :
6060
| `minOverlayOpacity` | `number` | `0` | Opacity of a black overlay on the header before any scroll | [example](https://github.com/bamlab/react-native-image-header-scroll-view-example/blob/3b9d2d0d7f71c6bf877e2d10cc65c9ab7e1b484d/src/Pages/TvShow.js#L96) |
6161
| `maxOverlayOpacity` | `number` | `0.3` | Opacity of a black overlay on the header when in navbar mode | [example](https://github.com/bamlab/react-native-image-header-scroll-view-example/blob/3b9d2d0d7f71c6bf877e2d10cc65c9ab7e1b484d/src/Pages/TvShow.js#L96) |
6262
| `overlayColor` | `string` | `black` | Color of the overlay on the header | [example](https://github.com/bamlab/react-native-image-header-scroll-view-example/blob/master/src/Pages/Colors.js#L16) |
63+
| `useNativeDriver` | `boolean` | `false` | Use native driver for the animation for performance improvement. A few props are unsupported at the moment if `useNativeDriver=true` (`onScroll`, `ScrollComponent`, `renderTouchableFixedForeground`) | - |
6364

6465

6566

src/ImageHeaderScrollView.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export type Props = ScrollViewProps & {
4242
ScrollViewComponent: React$ComponentType<ScrollViewProps>,
4343
scrollViewBackgroundColor: string,
4444
headerImage?: ?SourceProps,
45+
useNativeDriver: ?boolean,
4546
};
4647

4748
export type DefaultProps = {
@@ -186,6 +187,15 @@ class ImageHeaderScrollView extends Component<Props, State> {
186187
return <View />;
187188
}
188189

190+
if (this.props.useNativeDriver) {
191+
if (__DEV__) {
192+
console.warn(
193+
'useNativeDriver=true and renderTouchableFixedForeground is not supported at the moment due to the animation of height unsupported with the native driver'
194+
);
195+
}
196+
return null;
197+
}
198+
189199
return (
190200
<Animated.View style={[styles.header, styles.touchableFixedForeground, { height }]}>
191201
{this.props.renderTouchableFixedForeground()}
@@ -230,12 +240,16 @@ class ImageHeaderScrollView extends Component<Props, State> {
230240
style,
231241
contentContainerStyle,
232242
onScroll,
233-
ScrollViewComponent,
234243
scrollViewBackgroundColor,
244+
useNativeDriver,
235245
...scrollViewProps
236246
} = this.props;
237247
/* eslint-enable no-unused-vars */
238248

249+
const ScrollViewComponent = useNativeDriver
250+
? Animated.ScrollView
251+
: this.props.ScrollViewComponent;
252+
239253
const inset = maxHeight - minHeight;
240254

241255
return (
@@ -253,7 +267,7 @@ class ImageHeaderScrollView extends Component<Props, State> {
253267
{this.renderHeader()}
254268
<ScrollViewComponent
255269
ref={ref => (this.scrollViewRef = ref)}
256-
scrollEventThrottle={16}
270+
scrollEventThrottle={useNativeDriver ? 1 : 16}
257271
overScrollMode="never"
258272
{...scrollViewProps}
259273
contentContainerStyle={[
@@ -266,7 +280,13 @@ class ImageHeaderScrollView extends Component<Props, State> {
266280
childrenStyle,
267281
]}
268282
style={[styles.container, style]}
269-
onScroll={this.onScroll}
283+
onScroll={
284+
useNativeDriver
285+
? Animated.event([{ nativeEvent: { contentOffset: { y: this.state.scrollY } } }], {
286+
useNativeDriver: true,
287+
})
288+
: this.onScroll
289+
}
270290
/>
271291
{this.renderTouchableFixedForeground()}
272292
{this.renderForeground()}

0 commit comments

Comments
 (0)