iphone - 通过触摸移动图像

标签 iphone objective-c ios uiscrollview uitouch

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

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

    }

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

请引用图片了解更多详情 enter image description here

最佳答案

这就是我要做的:

向每个图像添加一个 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];
}

关于iphone - 通过触摸移动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13119360/

相关文章:

objective-c - NSMigrationManager 'Model already contains an entity named <Entity Name>'

ios - block 作为 block 的参数

ios - RestKit RKDynamicObjectMapping 仅适用于第一个 JSON 结果

ios - 在 plist 文件上实现 2 个 ATS 规范

iphone - Xcode for iPhone 静态库和动态库的区别

iphone - 添加小数点后的字典上的ios双引号

ios - <Error> : ImageIO: PNG [5C][30][0D]I: invalid chunk type mean? 是什么意思

iphone - 如何显示指标

iphone - 如何在自定义 UITableViewCell 中控制 UITextFields 的编辑

ios - iOS 中的沙盒是什么?我可以在一个应用程序与另一个应用程序之间传输数据吗?