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.

Coder949
  • 987
  • 1
  • 8
  • 29

1 Answers1

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

Dawid
  • 171
  • 5
  • 2
    Hey, @Dawid 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. – Coder949 Dec 11 '21 at 10:53
  • Send a repo and we can tackle that problem – Dawid Dec 20 '21 at 07:38