我想移动两个相同的图像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;
}
}