How do I detect whenever two UIImageView overlap?

痞子三分冷 提交于 2019-12-04 03:19:28

问题


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?


回答1:


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.");
    }
  }

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




回答2:


try something like this.

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



回答3:


You can use:

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




回答4:


In Swift 3.0 has become...

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


来源:https://stackoverflow.com/questions/4929747/how-do-i-detect-whenever-two-uiimageview-overlap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!