react-native-reanimated icon indicating copy to clipboard operation
react-native-reanimated copied to clipboard

How to evaluate an Animated Value against normal index Value

Open igarcido95 opened this issue 6 years ago • 8 comments

Hello,

I'm trying to evaluate an Animated Value against a normal index value. What I'm trying to do is create an output range array based on the active index (which is the animated Value). What I've tried to do is the following.

set(
  state.position,
  interpolate(activeIndexTransition, {
    inputRange: tabs.map((_tab, i) => i),
    outputRange: positions.map((value, index) => {
      cond(eq(activeIndexTransition, index), [state.position], [-1 * value])
    })
  })
),

So basically, if current activeIndexTransition equals index, then return the state.position por this given position in the outputrange array, if not return the predefined value. I've also tried to equal the map index to an animated value like this. But it seems it does not work.

const AnimatedIndex = new Value(index)

How could I achieve this logic? Thanks

igarcido95 avatar Jan 23 '20 10:01 igarcido95

Hi @igarcido95,

I'm afraid I don't understand what you want to achieve. interpolate's inputRange and outputRange take an increasing list of numbers, not nodes. Could you describe the animation you want to create?

jgonet avatar Feb 18 '20 12:02 jgonet

Hi @jakub-gonet,

I've created a scrollable header that is translated based on two sources: Y-Position and horizontal scroll. This is what I have right now: Scrollable header

In order to translate the header based on Y Position, I use an activeIndex to detect which section of the screen is active and then interpolate to an X position of the header.

However, the position of the header can also be modified by scrolling. In that case, I would need that the interpolatation goes from the current position to the next interpolate value. Right now I have this weird behaviour if I scroll the header to a position different than the outputRange value: Weird behaviour

If I could compare the activeIndex with the index of the .map I could replace that value within the output range with the current position. Does it make sense? Do you think in another workaround?

igarcido95 avatar Feb 18 '20 12:02 igarcido95

So if I understand correctly: the header is scrolled to some position based by where you scrolled in the list and by scrolling header itself.

You want to map items scroll position to some position in the header and I'm confused here: "the interpolatation goes from the current position to the next interpolate value", how should it work exactly? I think scrolling header should override automatic scroll in some way.

And, you're animating the entire header or that green selector? Some snack would be very helpful.

jgonet avatar Feb 18 '20 14:02 jgonet

Sorry @jakub-gonet, I know it's a little but difficult to explain.

The header uses a PanGestureHandler not a Scrollview, as I need that two sources of movement affect the same value.

The thing is that the interpolate has a set of output values based on X position of each category of the header [50, 100, 160, ...]. The tricky part is you can modify the position of the header by scrolling. So in that case, the interpolation from i.e 50 to 100 position can be wrong, as you are not in 50 but in 300 if you have scrolled manually. That's why I'm trying to replace the output value for a given input for the dynamic value current.state.

Here is a snack so you can take a look: Snack

igarcido95 avatar Feb 18 '20 14:02 igarcido95

@igarcido95

Sorry that I left you hanging, do you still face this issue or you managed to solve that?

jgonet avatar May 28 '20 08:05 jgonet

Yes @jakub-gonet, still facing the issue. Really stuck on how to update the outputRange array based on activeIndexTransition value.

igarcido95 avatar May 28 '20 10:05 igarcido95

Your snack isn't working, unfortunately. I think it'd be needed to basically recreate your use case because I don't know yet how to easily solve your problem.

I'll keep it open for now though.

jgonet avatar Jun 08 '20 07:06 jgonet

Closing due to no response.

jgonet avatar Jun 26 '20 10:06 jgonet