我不确定我是否理解这个问题。您可以使用标准平移手势识别器,每个手势识别器一个UIImageView
,例如:
UIPanGestureRecognizer *pan1 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanImage:)];
image01.userInteractionEnabled = YES;
[self.image01 addGestureRecognizer:pan1];
UIPanGestureRecognizer *pan2 = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanImage:)];
image02.userInteractionEnabled = YES;
[self.image02 addGestureRecognizer:pan2];
和
- (void)handlePanImage:(UIPanGestureRecognizer *)sender
{
static CGPoint originalCenter;
if (sender.state == UIGestureRecognizerStateBegan)
{
originalCenter = sender.view.center;
sender.view.alpha = 0.8;
[sender.view.superview bringSubviewToFront:sender.view];
}
else if (sender.state == UIGestureRecognizerStateChanged)
{
CGPoint translation = [sender translationInView:self.view];
sender.view.center = CGPointMake(originalCenter.x + translation.x, originalCenter.y + translation.y);
}
else if (sender.state == UIGestureRecognizerStateEnded || sender.state == UIGestureRecognizerStateCancelled || sender.state == UIGestureRecognizerStateFailed)
{
// do whatever post dragging you want, e.g.
// snap the piece into place
[UIView animateWithDuration:0.2 animations:^{
CGPoint center = sender.view.center;
center.x = round(center.x / 50.0) * 50.0;
center.y = round(center.y / 50.0) * 50.0;
sender.view.center = center;
sender.view.alpha = 1.0;
}];
}
}
或者您可以在他们的超级视图上创建一个手势识别器,然后使用CGRectContainsPoint
来查看您的开始locationInView
是否在您的两个 UIImageViews 之一的框架内,然后继续为适当的一个设置动画。
例如,在您的视图控制器中,您可以创建一个手势识别器:
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.image01.frame, location))
{
viewToMove = image01;
originalCenter = viewToMove.center;
}
else if (CGRectContainsPoint(self.image02.frame, location))
{
viewToMove = image02;
originalCenter = viewToMove.center;
}
else
{
viewToMove = nil;
}
if (viewToMove)
{
viewToMove.alpha = 0.8;
[viewToMove.superview bringSubviewToFront:viewToMove];
}
}
else 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
[UIView animateWithDuration:0.2 animations:^{
CGPoint center = viewToMove.center;
center.x = round(center.x / 50.0) * 50.0;
center.y = round(center.y / 50.0) * 50.0;
viewToMove.center = center;
viewToMove.alpha = 1.0;
}];
viewToMove = nil;
}
}
如果我误解了你的问题,请告诉我。