Reliable absolute position at screen with React Native
Asked Answered
P

1

10

What is the most reliable way to get the absolute position on screen of any element?

I have the problem that when an element above (parent) is added with my methods I can not get the absolute position of an element.

Context: I need to get the absolute position on screen of an element to check if my element which follows the finger (animation made with reanimated) intersects with that element which I can not get the absolute position from.

Methods I have tried:

event.target.measure

Here is the problem that I have a swipe element over the "drop box" and it just produces false positions.

ref.current.measure

This one works but not reliable. It fires about ten times in useEffect and produces only once the correct position.

Are there other things I can try which are reliable? I do not understand why it is so hard to get the absolute position on screen not relative to the parent.

Pasture answered 1/12, 2021 at 11:19 Comment(0)
R
2

I am not sure how your code is connected, and not sure if you want to make this feature in hacky way but you can get the absolute position of the element from on layout property https://reactnative.dev/docs/view#onlayout

Ravelment answered 9/12, 2021 at 17:48 Comment(2)
Hey, @Ravelment thank you very much for your answer. The answer your gave only calculates the position relative to the parent. I will make an example repo where the problem is abstracted.Pasture
Send a repo and we can tackle that problemRavelment

© 2022 - 2024 — McMap. All rights reserved.