我使用以下代码来创建对象并为其设置动画
//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/