将图像拖放到UIScrollView的内部和外部

时间:2021-03-23 09:45:06

In my project I'm having a scrollview with a view images inside. I'd like to drag them from my scroller to my View and counterwise.

在我的项目中,我有一个滚动视图,里面有视图图像。我想将它们从我的滚动条拖到我的视图中并逆向移动。

Is there someone who has some experience with this type of function.

是否有人对此类功能有一定的经验。

1 个解决方案

#1


1  

In essence what you'll want to do it place a UIGestureRecognizer in each view you wish to drag. In the selector the recognizer calls, you need to handle each type of state in a switch statement, similar to the following:

从本质上讲,你想要做的就是在你想要拖动的每个视图中放置一个UIGestureRecognizer。在识别器调用的选择器中,您需要在switch语句中处理每种类型的状态,类似于以下内容:

- (void)handleDragging:(UILongPressGestureRecognizer *)gestureRecognizer
{    
    switch ([gestureRecognizer state]) 
    {
        case UIGestureRecognizerStateBegan:
            [self startDragging:gestureRecognizer];
            break;
        case UIGestureRecognizerStateChanged:
            [self doDrag:gestureRecognizer];
            break;
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled:
        case UIGestureRecognizerStateFailed:
            [self stopDragging:gestureRecognizer];
            break;
        default:
            break;
    }
}

This will provide 3 entry points. I'd recommend in startDrag: that you remove the view from the scrollview (if it is inside one), and add it to the superview of everything, so that it's on top while dragging. In doDrag: you'll want to move the view's location to the location of the gesture. Finally, in stopDragging: you'll want to add the view to whichever view it is on top of (scrollview or otherwise) at the correct location.

这将提供3个入口点。我建议在startDrag中:你从滚动视图中移除视图(如果它在一个内部),并将其添加到所有内容的超级视图中,以便在拖动时它位于顶部。在doDrag中:您需要将视图的位置移动到手势的位置。最后,在stopDragging中:您需要将视图添加到位于正确位置(scrollview或其他)之上的任何视图。

#1


1  

In essence what you'll want to do it place a UIGestureRecognizer in each view you wish to drag. In the selector the recognizer calls, you need to handle each type of state in a switch statement, similar to the following:

从本质上讲,你想要做的就是在你想要拖动的每个视图中放置一个UIGestureRecognizer。在识别器调用的选择器中,您需要在switch语句中处理每种类型的状态,类似于以下内容:

- (void)handleDragging:(UILongPressGestureRecognizer *)gestureRecognizer
{    
    switch ([gestureRecognizer state]) 
    {
        case UIGestureRecognizerStateBegan:
            [self startDragging:gestureRecognizer];
            break;
        case UIGestureRecognizerStateChanged:
            [self doDrag:gestureRecognizer];
            break;
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled:
        case UIGestureRecognizerStateFailed:
            [self stopDragging:gestureRecognizer];
            break;
        default:
            break;
    }
}

This will provide 3 entry points. I'd recommend in startDrag: that you remove the view from the scrollview (if it is inside one), and add it to the superview of everything, so that it's on top while dragging. In doDrag: you'll want to move the view's location to the location of the gesture. Finally, in stopDragging: you'll want to add the view to whichever view it is on top of (scrollview or otherwise) at the correct location.

这将提供3个入口点。我建议在startDrag中:你从滚动视图中移除视图(如果它在一个内部),并将其添加到所有内容的超级视图中,以便在拖动时它位于顶部。在doDrag中:您需要将视图的位置移动到手势的位置。最后,在stopDragging中:您需要将视图添加到位于正确位置(scrollview或其他)之上的任何视图。