1

我想移动两个相同的图像UIPanGestureRecognizer,我可以移动第一个图像,但是一旦我尝试移动第二个图像,第一个图像就会回到原来的位置。我希望第一张图像在更改位置后保留它。

-(void) viewWillAppear:(BOOL)animated
{
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self      action:@selector(handlePanSuper:)];
    [self.view addGestureRecognizer:pan];
}

- (void)handlePanSuper:(UIPanGestureRecognizer *)sender
    {
     static UIImageView *viewToMove;
     static CGPoint  originalCenter;

    if (sender.state == UIGestureRecognizerStateBegan)
    {
    CGPoint location = [sender locationInView:self.view];

    if (CGRectContainsPoint(self.imageView.frame, location))
    {
        viewToMove = imageView;
        originalCenter = viewToMove.center;
    }
    else if (CGRectContainsPoint(self.image2.frame, location))
    {
        viewToMove = image2;
        originalCenter = viewToMove.center;
    }
    else
    {
        viewToMove = nil;
    }

    if (viewToMove)
    {

        viewToMove.alpha = 0.8;
        [viewToMove.superview bringSubviewToFront:viewToMove];
        NSLog(@"hi i am being touched.");
     }
 }
 if (sender.state == UIGestureRecognizerStateChanged && viewToMove != nil)
    {
    CGPoint translation = [sender translationInView:self.view];
    viewToMove.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y +                                        translation.y);


}
else if ((sender.state == UIGestureRecognizerStateEnded ||
          sender.state == UIGestureRecognizerStateFailed ||
          sender.state == UIGestureRecognizerStateCancelled) && viewToMove != nil)
{
    // do whatever post dragging you want, e.g.
    // snap the piece into place

       // CGPoint center = viewToMove.center;
        //viewToMove.center = center;

        viewToMove.alpha  = 1.0;

        viewToMove = nil;
  }
}
4

1 回答 1

0

您正在超级视图中设置手势识别器。为什么?您可以将其设置在您尝试移动的视图中,它更容易并且不太可能出现我编辑您的代码的奇怪行为,见下文(未测试)

-(void) viewWillAppear:(BOOL)animated
{
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self      action:@selector(handlePanSuper:)];
    self.imageView.userInteractionEnabled = self.image2.userInteractionEnabled = YES;
    [self.imageView addGestureRecognizer:pan];
    [self.image2 addGestureRecognizer:pan];
}

- (void)handlePanSuper:(UIPanGestureRecognizer *)sender
{
    UIImageView *viewToMove = (UIImageView*)sender.view;
    CGPoint  originalCenter = viewToMove.center;

    if (sender.state == UIGestureRecognizerStateBegan)
    {
        CGPoint location = [sender locationInView:self.view];

        viewToMove.alpha = 0.8;
        [viewToMove.superview bringSubviewToFront:viewToMove];
        NSLog(@"hi i am being touched.");

    }
    else if (sender.state == UIGestureRecognizerStateChanged)
    {
        CGPoint translation = [sender translationInView:self.view];
        viewToMove.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y +                                        translation.y);

    }
    else if ((sender.state == UIGestureRecognizerStateEnded ||
          sender.state == UIGestureRecognizerStateFailed ||
          sender.state == UIGestureRecognizerStateCancelled))
    {
    // do whatever post dragging you want, e.g.
    // snap the piece into place

       // CGPoint center = viewToMove.center;
        //viewToMove.center = center;

        viewToMove.alpha  = 1.0;
        viewToMove = nil;
    }
}

请试试这个,如果有效,请告诉我。

于 2013-07-31T11:00:29.570 回答