每当两个UIImageView重叠时,我如何检测? [英] How do I detect whenever two UIImageView overlap?

查看:117
本文介绍了每当两个UIImageView重叠时,我如何检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two UIImageViews, one of them is moving from left to right, the other one is touch draggable. iI want NSLog to show up a message on console whenever imagetwo overlaps imageone. How Do I do this?

推荐答案

你可以使用 CGRectIntersectsRect 函数如果UIImageViews共享相同的superview(更准确地说,具有相同的坐标空间),可以轻松测试矩形交集。

You can use the CGRectIntersectsRect function to easily test rectangle intersection, provided the UIImageViews share the same superview (more exactly, have the same coordinate space).

可能需要添加如下代码:

Likely you will need to add code like the following:

  -(void) touchesEnded:(NSSet *) touches {
    if(CGRectIntersectsRect([imageViewA frame], [imageViewB frame])) {
      NSLog(@"Do something.");
    }
  }

到托管两个图像视图的UIView,或者拖动完成时调用的类似方法。

to the UIView that hosts both image views, or a similar method that is called whenever the drag is completed.

这篇关于每当两个UIImageView重叠时,我如何检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆