Skip to content
This repository was archived by the owner on Oct 19, 2023. It is now read-only.
This repository was archived by the owner on Oct 19, 2023. It is now read-only.

Could we use animated value from 0 to 1 and interpolate #26

Description

@gabrielbull

If we used an animated value from 0 to 1 instead of 0 to dimension we could then use that value to interpolate other animations. Take for example the header used by Apple in their Settings app:

ezgif com-video-to-gif

It uses multiple transitions derived from the animated value. Look at how the back arrow is transitioned by fade in/fade out. Also look at how the title is transitioned into the back button, the General title moves and changes color to become the back button.

These changes would need to be made

Change the pan responder to give a fraction instead of the moveX value:

onPanResponderMove: (event, { moveX }) => {
this.animatedValue.setValue(moveX);
},

    onPanResponderMove: (event, { moveX }) => {
-      this.animatedValue.setValue(moveX);
+      this.animatedValue.setValue(moveX / this.getDimension());
    },

Change the finish animation to go to 1:

finishNavigation = duration => {
Animated.timing(this.animatedValue, {
toValue: this.getDimension(),
duration,
useNativeDriver: true,
}).start(({ finished }) => {
if (finished) {
this.afterPan();
}
});
};

  finishNavigation = duration => {
    Animated.timing(this.animatedValue, {
-      toValue: this.getDimension(),
+      toValue: 1,
      duration,
      useNativeDriver: true,
    }).start(({ finished }) => {
      if (finished) {
        this.afterPan();
      }
    });
  };

This one too:

this.animation = Animated.timing(this.animatedValue, {
duration: getDuration(routeAnimationType || nextProps.animationType, action),
toValue: action === PUSH ? -dimension : dimension,
easing: getEasing(routeAnimationType || nextProps.animationType),
useNativeDriver: true,
}).start(({ finished }) => {

          this.animation = Animated.timing(this.animatedValue, {
            duration: getDuration(routeAnimationType || nextProps.animationType, action),
-            toValue: action === PUSH ? -dimension : dimension,
+            toValue: action === PUSH ? 0: 1,
            easing: getEasing(routeAnimationType || nextProps.animationType),
            useNativeDriver: true,
          }).start()

All lines that use the animated value in getTransforms would need to be converted to interpolate:

const baseStyle = {
elevation: 1,
transform: [{ translateX: animation }],
};

    const baseStyle = {
      elevation: 1,
-      transform: [{ translateX: animation }],
+      transform: [{ translateX: animation.interpolate({
+        inputRange: [0, 1],
+         outputRange: [0, width]
+      }) }],
    };

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions