8

我在检测我的UIView对象的交集时遇到了问题。

这就是我在下面使用的:

对于相交两个对象,我需要弄清楚如何将一个坐标系从第一个超级视图转换为另一个坐标系。

我使用了这种方法:- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view在此处描述链接

据我所知,使用这种方法非常简单。但是在不同的情况下,很难在文档中进行一些描述(但可能只是对我而言)。

这是我的子视图结构,如下图所示。我已经掌握了拖放对象的所有方法。但我需要弄清楚如何获得UIView AUIView B的交集。感谢帮助。

在此处输入图像描述

4

2 回答 2

5

我已经实现了这个解决方案:

- (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");
    }
}
于 2012-12-20T20:54:36.000 回答
0

我认为这等同于您答案中的代码,但要短得多:

- (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");
    }
}
于 2012-12-20T21:54:53.990 回答