【问题标题】:How do I detect whenever two UIImageView overlap?如何检测两个 UIImageView 何时重叠?
【发布时间】:2011-02-08 05:00:23
【问题描述】:

我有两个 UIImageView,一个是从左向右移动的,另一个是可触摸拖动的。我希望 NSLog 在 imagetwo 与 imageone 重叠时在控制台上显示一条消息。我该怎么做?

【问题讨论】:

    标签: iphone objective-c iphone-sdk-3.0 ios4


    【解决方案1】:

    您可以使用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 ] )
    【解决方案2】:

    试试这样的。

    if (CGRectContainsRect([myImageView1 frame], [myImageView2 frame])) {
            NSLog(@"Overlaped, it's working!");
    }
    

    【讨论】:

      【解决方案3】:

      你可以使用:

      CGRectIsNull(CGRectIntersection(view1.bounds, view2.bounds));

      【讨论】:

        【解决方案4】:

        在 Swift 3.0 中已经变成...

        if (self.image2P.bounds.contains(self.image3P.bounds)) {
            print("Overlaped, it's working!")
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-10
          • 1970-01-01
          • 1970-01-01
          • 2021-06-24
          • 1970-01-01
          相关资源
          最近更新 更多