I have 2 UIImageViews in a ViewController and I'm trying to work out when they intersect.
imageViewA: is in my storyboard view, with constraints, and is in a hierarchy of views as follows:
- Background
-- Images
--- imageViewA
imageViewB: is created dynamically and is dragged around the screen using a UIPanGestureRecognizer.
When the drag ends, I want to check if imageViewB intersects with imageViewB. I used the intersects function, but don't get the results I expect, I suppose because imageViewA is in a hierachy of views, which means it's in a different coordinate system. So I want to convert both views to the same coordinate system. How I can do this?
I've tried the following:
let frameA = imageViewA.convert(imageViewA.frame, to: self.view)
let frameB = imageViewB.convert(imageViewB.frame, to: self.view)
But it doesn't give me the results I expect, which frameB having a much larger Y coordinate.
Do I need to do something like this:
let frameA = imageViewA.superview?.superview?.convert(imageViewA.superview?.superview?.frame, to: self.view)
There are other questions covering conversion to coordinate systems, but they don't seem to tackle what to do when the view is in a hierarchy.