質問を理解しているかどうかわかりません。標準のパン ジェスチャ レコグナイザを、それぞれに 1 つずつ使用できます。次に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
が 2 つの UIImageViews のいずれかのフレーム内にあるかどうかを確認してから、適切なものをアニメーション化することができます。
たとえば、View Controller でジェスチャ認識エンジンを作成できます。
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;
}
}
あなたの質問を誤解している場合は、お知らせください。