0

我有一个滚动视图,其中添加了固定数量的缩略图作为子视图。我想将这些图像沿触摸移动到另一个视图中的矩形。我能够沿着滚动视图(即沿着同一个视图)移动图像,但不能在另一个视图中移动。现在正在根据触摸位置更改图像的中心。当触摸点增加超出滚动视图的框架时,图像就会消失。这是我的问题

 - (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [[event allTouches] anyObject];

        CGPoint location = [touch locationInView: self];
        NSLog(@"%f,%f",location.x,location.y);
        touch.view.center = location;

    }

任何解决这个问题的方法对我都有很大的帮助!!!

请参考图片了解更多详情 在此处输入图像描述

4

1 回答 1

1

这是我要做的:

为每个图像添加一个 panGestureRecognizer,使用以下handlePan:方法作为其操作。您仍然需要弄清楚如何获得正确的 imageView ( myImageView),但这会使您的图像跟随您的手指。

- (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {

    // Find the correct image you're dragging
    // UIImageView *myImageview = ...

    CGPoint translation = [recognizer translationInView:self.view];

    if (recognizer.state == UIGestureRecognizerStateEnded) {
        // What to do when you start the gesture
        // You may also define myImageView here
        // (if so, make sure you put it in @interface,
        // because this method will be called multiple times,
        // but you will enter this "if" only when you start the touch)
    }

    // Keep your image in the screen
    if (myImageView.frame.origin.x + translation.x >= 0.0f &&
        myImageView.frame.origin.x + myImageView.frame.size.width + translation.x <= 320.0f &&
        myImageView.frame.origin.y + translation.y >= 0.0f &&
        myImageView.frame.origin.y + myImageView.frame.size.height + translation.y <= 480.0f) {

        myImageView.center = CGPointMake(myImageView.center.x + translation.x, 
                                         myImageView.center.y + translation.y);

    }

    if (recognizer.state == UIGestureRecognizerStateEnded) {
        // What to do when you remove your finger from the screen
    }

    [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
于 2012-10-29T10:40:00.773 回答