iphone - 如何判断两个物体是否相交?

标签 iphone objective-c

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

//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];

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

提前致谢

最佳答案

这是我通常用来测试交叉点的方法。但是,我不确定它是否会在动画中间工作。

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

    //They are intersecting
}

它在当前动画时无法测试交叉点,请尝试使用 View 层的 presentationLayer 属性。这是 presentationLayer 的内容,取自 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.

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

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

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

    //They are intersecting
}

所以如果第一种方法行不通,第二种方法肯定行。

关于iphone - 如何判断两个物体是否相交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11642947/

相关文章:

iphone - AlertView iOS 7 中的 UiTableView

ios - TestFlight 构建信息问题 : ITC. apps.tf.build.test.info.missing.feedback testflight

iphone - ASIHTTPRequest 假人

ios - 在 IOS 上绘制颜色网格的最快方法是什么?

iphone - Objective-C - 比 NSCaseInsensitiveSearch 更好的搜索?关键词? NSPredicate?

iphone - 像这样加载openGL纹理是否正确?

ios - 带有自定义 rootVievController 的主 Storyboard不会调用 vc 的 init

objective-c - 如何在应用程序中应用晕影和复古图像滤镜

ios - 地址簿 UI 框架弃用的方法

ios - 使用 sdwebimage 在 tableview 中动态图像高度