2016-07-07 2 views
0

複数の画像に対してドラッグアンドドロップを実装していますが、問題は1つあります。 1つのイメージをドラッグすると、元のイメージをドラッグしている間に、指がそれらの上を移動すると、他のイメージがナッジされます。一度に1つの画像しか動かすことができないのが大好きです。ドラッグアンドドロップします。

私のコードの一部です。

-(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

残念ながら私は既にあなたが以前提案したものにその行を変更しようとしましたが、何も変わらないのです。 –

関連する問題