2014-01-28 16 views
1

我試圖爲iPad構建一個基本的Jigsaw應用程序,但我對於Objective C和xcode非常新,這意味着這是我第一次在完全正常工作的應用程序中使用。如何檢查圖像的位置,並在iOS中嵌入

我一直在網上學習一些教程,並已經建立了一個非常基本的三個圖像拼圖,並已成功設法讓這個運行。不過,我希望能夠在用戶拖動正確的區域並顯示動畫或聲音時將圖像捕捉到位。

我有點迷路了,我知道我需要指定每個拼圖的x和y座標然而,如果這是正確的位置,然後觸發一個動作,如動畫和聲音?

這是到目前爲止我的代碼

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches]anyObject]; 
    CGPoint touchLocation = [touch 
          locationInView:self.view]; 
    if ([touch view] == image1){ 
     image1.center = touchLocation; 
     NSLog(@"Touch x : %f y : %f", touchLocation.x, touchLocation.y); 

         } 
    else if ([touch view] == image2){ 
     image2.center = touchLocation; 
     NSLog(@"Touch x : %f y : %f", touchLocation.x, touchLocation.y); 
    } 
    else if ([touch view] == image3){ 
     image3.center = touchLocation; 
     NSLog(@"Touch x : %f y : %f", touchLocation.x, touchLocation.y); 
    } 



} 

正如你所看到的IM登錄電子x和不過ÿ即時通訊不能確定如何與這個

回答

0

讓說{X1,Y1}是如果您在touchesMovedtouchesEnded中檢查圖像1的位置是否正好在{x1,y1}上,那麼沒有人會這麼做。 因此,讓我們添加一個錯誤marge讓它命名爲Epsilon,而不是再次檢查一個點,現在我們將嘗試檢查Epsilon的正方形(中心{x1,y1}和邊長)的內部:這裏有一些代碼可以幫助您:

float epislon = 20.0; 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches]anyObject]; 
    CGPoint touchLocation = [touch 
          locationInView:self.view]; 
    if ([touch view] == image1) 
    { 
     image1.center = touchLocation; 
     NSLog(@"Touch x : %f y : %f", touchLocation.x, touchLocation.y); 

     CGRect rect1= CGRectMake(x1-epsilon/2.0, y1-epsilon/2.0, epsilon, epsilon); 
     if(CGRectContainsPoint(rect1,touchLocation)) 
     { 
      NSLog(@"did find position for image1"); 
     } 
    } 
} 
+0

由於與編輯的一點點,玩耍取得了這項工作完美! – user3246508

+0

不客氣:) – MAB

0

這是一個很好的問題,我也一直在想繼續關於這樣一個簡單的拼圖應用程序爲孩子一段時間。

您正處在正確的軌道上。我相信訣竅是知道原始X,Y圖像的位置應該在哪裏。一旦用戶在這些座標上拖動它的權利,然後你鎖定它併發出快樂的聲音。這是我的方式。 (這是僞代碼)

這將是冷靜,如果有一種方法可以找到,如果你足夠接近X,原始圖像的y座標

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouches]anyObject]; 
    CGPoint touchLocation = [touch 
          locationInView:self.view]; 
    if ([touch view] == image1 && image1Locked == NO) 
    { 
     image1.center = touchLocation; 
     NSLog(@"Touch x : %f y : %f", touchLocation.x, touchLocation.y); 

if ((touchLocation.x == to where x location of image1 should be) && 
    (touchLocation.y == to where y location of image1 should be) 
    { 
      //lock image by not checking drag anymore 
      image1Locked = YES; 

      //move the image1 in place where its should be 

      //happy sounds 
      [self happySounds]; 

    } 

    } 
    ... 
+0

這是偉大的,但作爲馬伯說,我不能讓用戶在準確的位置下降非常感謝壽:) – user3246508

相關問題