Skip to content
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

Add useNativeDriver to shine animations #134

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
17 changes: 10 additions & 7 deletions src/animations/Shine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ export class Shine extends React.Component<IShine> {
public render() {
const { children, style } = this.props;

const left = this.animation.interpolate({
const translateX = this.animation.interpolate({
inputRange: [START_VALUE, END_VALUE],
outputRange: ["0%", "100%"],
outputRange: ["0%", "250%"],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I ask why 250%? 😊

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. The width of the animated view is 40% of the parent. In order to Translate over the whole surface of the parent, we need to Translate with 2.5 of itself meaning 100% of the parent

Copy link
Owner

@mfrachet mfrachet Sep 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm seeing the following error: https://github.com/facebook/react-native/blob/aee88b6843cea63d6aa0b5879ad6ef9da4701846/ReactAndroid/src/main/java/com/facebook/react/bridge/ReadableNativeMap.java#L132

Screenshot_20200918-082052

Looks like % are not valid as a transform value. See the API here: https://reactnative.dev/docs/transforms#transform and I think this is the reason why I didn't rely on this when implementing, hard times 😅 !

Thank you for the effort on this pull requests 😊 🙏

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, this is on Android, right? Hmmm... To be fair, I only tested IOS, which was working. I will look into it, when I have some more time 👍

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep that's on android :)

extrapolateRight: "extend"
});

return (
<Provider value={[styles.shine, { left }, style]}>{children}</Provider>
<Provider value={[styles.shine, { transform: [{ translateX }] }, style]}>
{children}
</Provider>
);
}

Expand All @@ -43,8 +46,8 @@ export class Shine extends React.Component<IShine> {
duration: this.props.duration || 750,
isInteraction,
toValue: END_VALUE,
useNativeDriver: false,
}).start((e) => {
useNativeDriver: true
}).start(e => {
if (e.finished) {
this.start();
}
Expand All @@ -57,6 +60,6 @@ const styles = StyleSheet.create({
backgroundColor: "white",
height: "100%",
opacity: 0.5,
width: "40%",
},
width: "40%"
}
});
26 changes: 17 additions & 9 deletions src/animations/ShineOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ export interface IShine {
duration?: number;
}

export class ShineOverlay extends React.Component<IShine> {
export class ShineOverlay extends React.Component<IShine, { width: number }> {
private animation: Animated.Value;
constructor(props: IShine) {
super(props);

this.state = {
width: 0
};

this.animation = new Animated.Value(0);
}

Expand All @@ -25,15 +29,19 @@ export class ShineOverlay extends React.Component<IShine> {
public render() {
const { children } = this.props;

const left = this.animation.interpolate({
const translateX = this.animation.interpolate({
inputRange: [START_VALUE, END_VALUE],
outputRange: ["0%", "100%"],
outputRange: [0, this.state.width]
});

return (
<View>
<View
onLayout={e => this.setState({ width: e.nativeEvent.layout.width })}
>
{children}
<Animated.View style={[styles.shine, { left }]} />
<Animated.View
style={[styles.shine, { transform: [{ translateX }] }]}
/>
</View>
);
}
Expand All @@ -45,8 +53,8 @@ export class ShineOverlay extends React.Component<IShine> {
duration: this.props.duration || 750,
isInteraction,
toValue: END_VALUE,
useNativeDriver: false,
}).start((e) => {
useNativeDriver: true
}).start(e => {
if (e.finished) {
this.start();
}
Expand All @@ -60,6 +68,6 @@ const styles = StyleSheet.create({
height: "100%",
opacity: 0.4,
position: "absolute",
width: 30,
},
width: 30
}
});