0

那么这里是我的问题: 我有两个图像:flakeImage和ViewToRotate。我想要的是,如果flakeImage触及ViewToRotate,ViewToRotate.alpha = 0.5;但是当FlakeImage出现在屏幕上时ViewToRotate.alpha = 0.5;不用碰它。我认为这是一个问题,我的看法我东阳有:两个图像碰撞的问题

UIImageView* flakeView = [[UIImageView alloc] initWithImage:flakeImage]; 

这里是代码:

UIImageView* flakeView = [[UIImageView alloc] initWithImage:flakeImage]; 

// use the random() function to randomize up our flake attributes 
int startY = round(random() % 320); 

// set the flake start position 
flakeView.center = CGPointMake(490, startY); 
flakeView.alpha = 1; 

// put the flake in our main view 
[self.view addSubview:flakeView]; 

[UIView beginAnimations:nil context:flakeView]; 
// set up how fast the flake will fall 
[UIView setAnimationDuration:7 ]; 

// set the postion where flake will move to 
flakeView.center = viewToRotate.center; 

// set a stop callback so we can cleanup the flake when it reaches the 
// end of its animation 
[UIView setAnimationDelegate:self]; 
[UIView commitAnimations]; 

我怎样才能解决这个问题吗? 如果有人能帮助我,这将是非常酷。

+0

当动画结束时,将alpha设置为0.5就足够了吗?或者你需要真正的碰撞代码? – 2011-05-08 08:46:22

+0

我需要真实的碰撞代码,但是当图像(flakeView)出现在屏幕上而没有碰撞另一个is.alpha = 0.5; – 2011-05-08 12:56:09

+0

嗯我仍然不确定你真的需要什么。所以你有一个片,也许是一个雪片?它从一个随机的高度(startY)开始并下降,或者去到viewToRotate的位置,对吗?那么为什么当动画的目标是让片状物位于另一个视图的相同位置时,您需要碰撞检测?到目前为止,这是关于职位,阿尔法与它有什么关系?当薄片碰到它的最终目的地时,它应该变成半透明的?请帮助我理解。 – 2011-05-08 16:29:21

回答

0

CGRect具有相交功能。 UIViews的框架是CGRects。

if (CGRectIntersectsRect(view1.frame, view2.frame) == 1) 
    NSLog(@"The views intersect"); 
else 
    NSLog(@"The views do not intersect"); 

我预见到的问题是,如果rects有很多空白的,它们将出现交叉,他们实际上触及

之前其次,你应该切换为拦截动画。这是强烈鼓励

UIImageView* flakeView = [[[UIImageView alloc] initWithImage:flakeImage] autorelease]; 

// use the random() function to randomize up our flake attributes 
int startY = round(random() % 320); 

// set the flake start position 
flakeView.center = CGPointMake(490, startY); 
flakeView.alpha = 1; 

// put the flake in our main view 
[self.view addSubview:flakeView]; 

[UIView animateWithDuration:.7 
       animations:^ { 
         // set the postion where flake will move to 
         flakeView.center = viewToRotate.center; 
        }; 

这一切都从内存中,不知道是否有错误。

通知碰撞:

A^2 + B^2 < C^2意味着他们碰撞

if(pow(view2.frame.origin.x - view1.frame.origin.x, 2) + 
    pow(view2.frame.origin.y - view1.frame.origin.y, 2) < 
    pow(view1.frame.size.width/2, 2)) 
{ 
    //collision 
} 
else 
{ 
    //no collision 
} 

同样,所有从内存中,检查是否存在错误在自己的

+0

这正是我的问题“他们似乎在他们实际接触之前相交”我该如何解决这个问题? – 2011-05-09 20:03:49

+0

您必须为您的对象定义自定义碰撞检测。因此,而不是矩形碰撞,你需要圆形碰撞。首先检查编辑 – ColdLogic 2011-05-10 21:58:34

+0

什么是pow它是一个变量?第二,而不是/ /我必须写CGIntesectsRect ...或例如view2.alpha = 0; – 2011-05-11 05:54:32

0

我有有一些与此有关的经验,有写http://itunes.apple.com/us/app/balls/id372269039?mt=8。如果您检查该应用程序,您会看到一些相同的问题。这个话题是一个相当深刻的兔子洞。当我开始使用这款应用程序时,我甚至不知道如何编写一个体面的游戏循环。您首先需要这一点,因为您需要执行精确的时间步骤计算。 AFA的碰撞,你分别更新你的模型和视图,所以如果你更新模型和对象重叠,你需要备份它们,直到它们不碰撞,然后用结果更新你的视图。如果你打算有很多碰撞对象,你可以使用UIViews打墙。更复杂的事情,如果你的对象是圆的,CGRectIntersectsRect不会正好工作。这会使数学复杂化一些,但这并不算太坏。用我的应用程序,我发现让物理看起来很现实很困难。这个问题变成了球A和球B重叠,所以你将它们备份起来,然后他们现在相交其他球等等。This链接是一个很好的起点,但是有很多代码的例子,工作。