iOS如何使用touchesMoved移动两个图像?

时间:2012-07-02 20:07:02

标签: ios ios5 ios4 xcode4.3

我有两个不同的UIImage。 我想允许用户单独在UIView上移动这些图片。 我怎么能这样做?

IBOutlet UIImageView *image01;
IBOutlet UIImageView *image02;

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:touch.view];
}

1 个答案:

答案 0 :(得分:2)

我不确定我是否理解这个问题。您可以使用标准的平移手势识别器,每个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是否在您的两个UIImageView之一的框架内,然后继续为相应的一个创建动画

例如,在视图控制器中,您可以创建手势识别器:

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;
    }
}

如果我误解了你的问题,请告诉我。