2016-07-07 94 views
0

我已經實現了多個圖像的拖放功能,可以正常工作,但我面臨着一個問題。當我拖動一張圖像時,當我的手指移過其他圖像時,其他圖像會一直向前移動,而Im仍在拖動原始圖像。我希望能夠一次只能移動一張圖片。拖放。移動時移動其他圖像

繼承人一些我的代碼。

-(void)touchesBegan: (NSSet *) touches withEvent:(UIEvent *)event{ 

UITouch* touch = [touches anyObject]; 

    for (UIImageView *noseImage in noseArray) { 

     if ([touch.view isEqual:noseArray]) { 

      firstTouchPoint = [touch locationInView:[self view]]; 
      xd = firstTouchPoint.x - [[touch view]center].x; 
      yd = firstTouchPoint.y - [[touch view]center].y; 

     } 
    } 
} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 

UITouch *touch = [touches anyObject]; 
CGPoint oldPoint = [touch previousLocationInView:touch.view]; 
CGPoint newPoint = [touch locationInView:touch.view]; 
CGPoint diff = CGPointMake(newPoint.x - oldPoint.x, newPoint.y - oldPoint.y); 

    for (UIImageView *noseImageView in noseArray) { 

     if (CGRectContainsPoint(noseImageView.frame, newPoint)) { 
      CGPoint cntr = [noseImageView center]; 
      [noseImageView setCenter:CGPointMake(cntr.x + diff.x, cntr.y + diff.y)]; 

     } 
    } 
} 

回答

0

通常情況下,您將確定在touchesBegan:中正在拖動哪個圖像並記住該圖像。然後在touchesMoved:中,將記住的圖像移動給定量。

但是,手勢識別器比這些低級別的方法工作起來要容易得多,所以我建議您使用它。

0

您寫道:

for (UIImageView *noseImage in noseArray) { 

     if ([touch.view isEqual:noseArray]) { 

確定第二行不應該是以下?

if ([touch.view isEqual: noseImage]) { 
+0

不幸的是,我已經嘗試將該行更改爲您之前所建議的內容,並且不會更改任何內容。 –