Skip to content

feat(useNativeDriver): add possibility of using native driver for ani… #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ The `HeaderImageScrollView` handle also the following props. None is required :
| `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) |
| `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) |
| `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) |
| `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`) | - |



Expand Down
26 changes: 23 additions & 3 deletions src/ImageHeaderScrollView.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export type Props = ScrollViewProps & {
ScrollViewComponent: React$ComponentType<ScrollViewProps>,
scrollViewBackgroundColor: string,
headerImage?: ?SourceProps,
useNativeDriver: ?boolean,
};

export type DefaultProps = {
Expand Down Expand Up @@ -186,6 +187,15 @@ class ImageHeaderScrollView extends Component<Props, State> {
return <View />;
}

if (this.props.useNativeDriver) {
if (__DEV__) {
console.warn(
'useNativeDriver=true and renderTouchableFixedForeground is not supported at the moment due to the animation of height unsupported with the native driver'
);
}
return null;
}

return (
<Animated.View style={[styles.header, styles.touchableFixedForeground, { height }]}>
{this.props.renderTouchableFixedForeground()}
Expand Down Expand Up @@ -230,12 +240,16 @@ class ImageHeaderScrollView extends Component<Props, State> {
style,
contentContainerStyle,
onScroll,
ScrollViewComponent,
scrollViewBackgroundColor,
useNativeDriver,
...scrollViewProps
} = this.props;
/* eslint-enable no-unused-vars */

const ScrollViewComponent = useNativeDriver
? Animated.ScrollView
: this.props.ScrollViewComponent;

const inset = maxHeight - minHeight;

return (
Expand All @@ -253,7 +267,7 @@ class ImageHeaderScrollView extends Component<Props, State> {
{this.renderHeader()}
<ScrollViewComponent
ref={ref => (this.scrollViewRef = ref)}
scrollEventThrottle={16}
scrollEventThrottle={useNativeDriver ? 1 : 16}
overScrollMode="never"
{...scrollViewProps}
contentContainerStyle={[
Expand All @@ -266,7 +280,13 @@ class ImageHeaderScrollView extends Component<Props, State> {
childrenStyle,
]}
style={[styles.container, style]}
onScroll={this.onScroll}
onScroll={
useNativeDriver
? Animated.event([{ nativeEvent: { contentOffset: { y: this.state.scrollY } } }], {
useNativeDriver: true,
})
: this.onScroll
}
/>
{this.renderTouchableFixedForeground()}
{this.renderForeground()}
Expand Down