1

同じ で 2 つの画像を移動したいのですが、最初の画像はUIPanGestureRecognizer移動できますが、 2 番目の画像を移動しようとするとすぐに最初の画像が元の位置に戻ります。位置を変更した後も最初の画像を保持したい。

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