我使用以下代码创建对象并为其设置动画
//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];
我希望在两个图像相互交叉时显示图像。但我不知道如何在动画时找到图像是否相交。 任何人都可以告诉我如何在动画时找到两个图像是否相互交叉。
提前致谢
答案 0 :(得分:5)
这是我通常用来测试交叉路口的东西。但是,我不确定它是否适用于动画中期。
if(CGRectIntersectsRect(bbl1Obj.frame, bbl2Obj.frame)) {
//They are intersecting
}
在当前动画时,它不适用于测试交叉点,请尝试使用视图图层的presentationLayer
属性。以下是presentationLayer
取自CALayer Class Reference:
<强>表示层
返回包含所有属性的图层副本,与当前事务开始时一样,并应用任何活动动画。
考虑到这一点,你现在可以试试这个:
CALayer *bbl1ObjPresentationLayer = (CALayer*)[bb1Obj.layer presentationLayer];
CALayer *bbl2ObjPresentationLayer = (CALayer*)[bb2Obj.layer presentationLayer];
if(CGRectIntersectsRect(bbl1ObjPresentationLayer.frame, bbl2ObjPresentationLayer.frame)) {
//They are intersecting
}
因此,如果第一种方式不起作用,第二种方式肯定会。
答案 1 :(得分:1)
使用CGRectContainsPoint为对象提供交叉。
if(CGRectContainsPoint([bbl1Obj bounds], CGPointMake(bbl2Obj.frame.origin.x, bbl2Obj.frame.origin.y)))
{
NSLog(@"intersect");
}