拖放。移动时轻推其他图像

时间:2016-07-07 16:51:46

标签: objective-c

我已经为多张图片实施了拖放操作,但是我遇到了一个问题。当我拖动一个图像时,当我的手指移过它们时,其他图像将被轻推,而我仍然拖动原始图像。我希望能够同时只移动一张图像。

这是我的一些代码。

-(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)];

         }
    }
}

2 个答案:

答案 0 :(得分:0)

您写道:

 for (UIImageView *noseImage in noseArray) {

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

您确定第二行不应该是以下内容吗?

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

答案 1 :(得分:0)

通常,您会确定在touchesBegan:中拖动哪个图片并记住这一点。然后在touchesMoved:中,将记住的图像移动给定的数量。

但是手势识别器比这些低级方法更容易工作,所以我建议你改用它。