如何找出两个物体是否相互交叉? [英] How to find whether two objects intersect each other?

查看:210
本文介绍了如何找出两个物体是否相互交叉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码创建对象并为其设置动画

I used the following code to create and animate the object

//For creating two imageview 
UIImageView *bbl1Obj=[[UIImageView alloc]initWithFrame:CGRectMake(34,77,70, 70)];
bbl1Obj.image=[UIImage imageNamed:@"bubble1.png"];
[self.view addSubview:bbl1Obj];
UIImageView *bbl2Obj=[[UIImageView alloc]initWithFrame:CGRectMake(224,77,70, 70)];
bbl2Obj.image=[UIImage imageNamed:@"bubble2.png"];
[self.view addSubview:bbl2Obj];
// for animating the objects
[UIImageView beginAnimations:nil context:NULL];
[UIImageView setAnimationDuration:3];
[bbl1Obj setFrame:CGRectMake(224,77,70, 70)];
[UIImageView commitAnimations];

[UIImageView beginAnimations:nil context:NULL];
[UIImageView setAnimationDuration:3];
[bbl2Obj setFrame:CGRectMake(34,77,70, 70)];
[UIImageView commitAnimations];

我想在两个图像相互交叉时显示图像。但我不知道如何在动画时找到图像是否与其他图像相交。
任何人都可以告诉我如何在动画时找到两个图像是否相互交叉。

I want to display an image when the two image intersect each other. but i don't know how to find whether the images intersect other or not while animating. Can anyone please tell me how to find whether two images intersect each other or not while animating.

提前致谢

推荐答案

这是我通常用来测试交叉点的方法。但是,我不确定它是否适用于动画中期。

This is what I would normally use to test for intersections. However, I'm not sure if it would work mid-animation.

if(CGRectIntersectsRect(bbl1Obj.frame, bbl2Obj.frame)) {

    //They are intersecting
}

它在当前动画时不适用于测试交叉点,请尝试使用视图图层的 presentationLayer 属性。以下是 presentationLayer 的内容,取自 CALayer类参考:

It that didn't work for testing for intersections while currently animating, try using the presentationLayer property of the view's layer. Here's what the presentationLayer is, taken from the CALayer Class Reference:


presentationLayer

返回包含当前事务开始时所有属性的图层副本,并应用任何活动动画。

Returns a copy of the layer containing all properties as they were at the start of the current transaction, with any active animations applied.

考虑到这一点,你现在可以试试这个:

With that in mind, you can try this now:

CALayer *bbl1ObjPresentationLayer = (CALayer*)[bb1Obj.layer presentationLayer];
CALayer *bbl2ObjPresentationLayer = (CALayer*)[bb2Obj.layer presentationLayer];

if(CGRectIntersectsRect(bbl1ObjPresentationLayer.frame, bbl2ObjPresentationLayer.frame)) {

    //They are intersecting
}

因此,如果第一种方式不起作用,第二种方式肯定会。

So if the first way doesn't work, the second way surely will.

这篇关于如何找出两个物体是否相互交叉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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