简体   繁体   English

触摸移动图像

[英]Move image with touch

I have a scroll view with a fixed number of thumbnail images added as subview. 我有一个滚动视图,其中添加了固定数量的缩略图作为子视图。 I want to move these images along touch to the rectangle in another view. 我想将这些图像沿触摸移动到另一个视图中的矩形。 I am able to move the image along scrollview (ie, along the same view) but not able to move across another view. 我能够沿滚动视图(即沿同一视图)移动图像,但不能跨另一个视图移动。 Now am changing the center of image depending on touch position. 现在,根据触摸位置更改图像的中心。 When touch point increases beyond the frame of scrollview the image disappears. 当触摸点增加到滚动视图的框架之外时,图像消失。 This is my problem 这是我的问题

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

    }

Any solution to this problem will be a great help for me !!! 任何解决此问题的方法将对我有很大帮助!

Please refer image for further details 请参考图片了解更多详情 在此处输入图片说明

Here is what I'd do : 这是我要做的:

Add a panGestureRecognizer to each image, with the following handlePan: method as its action. 将panGestureRecognizer添加到每个图像,并使用以下handlePan:方法作为其动作。 You still have to figure out how to get the correct imageView ( myImageView ), but this will make your image follow your finger. 您仍然必须弄清楚如何获取正确的myImageViewmyImageView ),但这将使您的图像顺应您的手指。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM