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

How can I get the scroll position all the time? #156

Open
picknamexd opened this issue Mar 14, 2021 · 2 comments
Open

How can I get the scroll position all the time? #156

picknamexd opened this issue Mar 14, 2021 · 2 comments
Labels
question Further information is requested

Comments

@picknamexd
Copy link

I want that when I scroll halfway, execute a function, but I can't get that value, I'm testing with const {top, height} = useHeaderMeasurements (); And with react native animated, but I can't get it, any suggestions?

Thanks!!

@picknamexd picknamexd added the question Further information is requested label Mar 14, 2021
@PedroBern
Copy link
Owner

Halfway of the header? Probably you can use useDerivedValue and when the top distance be equals half of the header, you execute your code. Can you share what you have tried?

@picknamexd
Copy link
Author

picknamexd commented Mar 16, 2021

Thanks for your answer @PedroBern

In the end I managed to make it work, I leave an example of how I have done it.
thank you very much for your help! and hope my problem can help others.

  const offset = useSharedValue(0);
  const {top, height} = useHeaderMeasurements();

  useDerivedValue(() => {
    if (top.value <= -(HEADER_HEIGHT / 1.5)) {
      offset.value = Math.random();
    }
  });

  const animatedStyles = useAnimatedStyle(() => {
    return {
      transform: [
        {
          translateY: withSpring(offset.value * 255),
        },
      ],
    };
  });

<Animated.View style={[animatedStyles]} >
    <Text>Test</Text>
</Animated.View>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants