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鏈接是一個很好的起點,但是有很多代碼的例子,工作。