【问题标题】:Drag multiple UIImageViews in an Array拖动数组中的多个 UIImageView
【发布时间】:2012-11-16 04:34:54
【问题描述】:

这是我目前所拥有的

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [[event allTouches] anyObject];

    for (UIImageView *imageView in _imageViewArray) {
            CGPoint Location = [touch locationInView:touch.view];
            imageView.center = Location;
    }   
}

我面临的问题是,当我移动一张图像时,它们都会跳到同一个地方。

感谢cyberpawn,这就是我所做的让它工作

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

    CGPoint oldPoint = [touch previousLocationInView:touch.view];
    CGPoint newPoint = [touch locationInView:touch.view];

    CGPoint diff = CGPointMake(newPoint.x - oldPoint.x, newPoint.y - oldPoint.y);

    for (UIImageView *imageView in _imageViewArray) {
        if (CGRectContainsPoint(imageView.frame, newPoint)) {
            CGPoint cntr = [imageView center];
            [imageView setCenter:CGPointMake(cntr.x + diff.x, cntr.y + diff.y)];

        }
}
}

【问题讨论】:

    标签: objective-c ios drag-and-drop uiimageview touch


    【解决方案1】:

    那是因为您将它们全部移动到同一位置,您需要计算触摸位置之间的差异并将该位移添加到所有视图。下面的代码应该可以解决您的问题!忘记 t​​ouchesBegan 并像这样覆盖 touchesMoved 方法。

    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
    
        CGPoint oldPoint = [touch previousLocationInView:touch.view];
        CGPoint newPoint = [touch locationInView:touch.view];
    
        CGPoint diff = CGPointMake(newPoint.x - oldPoint.x, newPoint.y - oldPoint.y);
    
        for (UIImageView *imageView in _imageViewArray) {
            CGPoint cntr = [imageView center];
            [imageView setCenter:CGPointMake(cntr.x + diff.x, cntr.y + diff.y)];
        }
    }
    

    如果您想在点击其中任何一个时单独移动它们,而不是使用下面的代码!

    float oldX, oldY;
    
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [touches anyObject];
        CGPoint pt = [touch locationInView:touch.view];
        for (UIImageView *imageView in _imageViewArray) {
            if(CGRectContainsPoint(imageView.frame, pt)) {
                oldX = imageView.center.x - imageView.frame.origin.x - pt.x;
                oldY = imageView.center.y - imageView.frame.origin.y - pt.y;
                break;
            }
        }
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint pt = [touch locationInView:touch.view];
    
        for (UIImageView *imageView in _imageViewArray) {
            if (CGRectContainsPoint(imageView.frame, pt)) {
                [self setCenter:CGPointMake(pt.x+oldX, pt.y+oldY)];
            }
        }
    

    享受编程!

    【讨论】:

    • 这真的很好,唯一的事情是我希望图像现在分开移动,它们都一起移动。非常感谢。
    • @Jonathan 将它们分别移动查看我编辑的答案,希望对您有所帮助!
    • @Jonathan 再次编辑有一些小故障!试试看能不能解决你的问题!
    • 非常感谢你的工作我只需要稍微调整你的代码,它所做的是当你触摸它会射击到(0,0)原点的对象然后我可以拖动它大约。我刚刚结合了你的两个代码,它工作了。
    【解决方案2】:

    在这里你只是这样编码。如果要移动单个图像,则必须找到该图像,并且应该单独移动该图像。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-06
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      • 2012-05-25
      • 1970-01-01
      相关资源
      最近更新 更多