Detecting the intersection of two UIView objects with different observations

I am having trouble detecting the intersection of my UIView objects.

What I used below:

To intersect two objects, I need to figure out how to translate one coordinate system from the first supervisor to another coordinate system.

I used this approach: - (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view , the link described here.

As I know, it is very simple to use this method. But in different cases, this is difficult due to a small description of the documentation (but, perhaps, only for me).

This is my subheading structure, which is shown in the image below. I already have all the methods for the drag object. But I need to figure out how to get the intersection for UIView A and UIView B. Thanks for the help.

enter image description here

+8
ios uiview
source share
2 answers

I implemented this solution:

 - (void)putComponent:(NSNotification *)notif { // Catch B UIView. UIView *view = [notif object]; // Convertation. [self superview] - is view wher A UIView is placed. CGRect convertedRect = [[self superview] convertRect:view.frame fromView:[view superview]]; // Find center point. CGPoint point; point.x = convertedRect.origin.x + (convertedRect.size.width / 2.0f); point.y = convertedRect.origin.y + (convertedRect.size.height / 2.0f); // Find if CGRect (self.frame) contains a point (center of B UIView) BOOL contains = CGRectContainsPoint(self.frame, point); if (contains) { NSLog(@"intersect here"); } } 
+5
source share

I think this is equivalent to the code in your answer, but much shorter:

 - (void)putComponent:(NSNotification *)note { UIView *other = note.object; CGPoint otherCenterInMe = [self convertPoint:other.center fromView:other.superview]; if ([self pointInside:otherCenterInMe withEvent:nil]) { NSLog(@"intersect here"); } } 
0
source share

All Articles