【发布时间】:2011-02-08 05:00:23
【问题描述】:
我有两个 UIImageView,一个是从左向右移动的,另一个是可触摸拖动的。我希望 NSLog 在 imagetwo 与 imageone 重叠时在控制台上显示一条消息。我该怎么做?
【问题讨论】:
标签: iphone objective-c iphone-sdk-3.0 ios4
我有两个 UIImageView,一个是从左向右移动的,另一个是可触摸拖动的。我希望 NSLog 在 imagetwo 与 imageone 重叠时在控制台上显示一条消息。我该怎么做?
【问题讨论】:
标签: iphone objective-c iphone-sdk-3.0 ios4
您可以使用CGRectIntersectsRect 函数轻松测试矩形相交,前提是 UIImageView 共享相同的父视图(更准确地说,具有相同的坐标空间)。
您可能需要添加如下代码:
-(void) touchesEnded:(NSSet *) touches {
if(CGRectIntersectsRect([imageViewA frame], [imageViewB frame])) {
NSLog(@"Do something.");
}
}
到承载两个图像视图的 UIView,或在拖动完成时调用的类似方法。
【讨论】:
BOOL intersects = CGRectIntersectRect( viewA.bounds, [ viewA convertRect:viewB.bounds fromView:viewB ] )
试试这样的。
if (CGRectContainsRect([myImageView1 frame], [myImageView2 frame])) {
NSLog(@"Overlaped, it's working!");
}
【讨论】:
你可以使用:
CGRectIsNull(CGRectIntersection(view1.bounds, view2.bounds));
【讨论】:
在 Swift 3.0 中已经变成...
if (self.image2P.bounds.contains(self.image3P.bounds)) {
print("Overlaped, it's working!")
}
【讨论】: