[英]Drag a UIView from one UIScrollView to another
我的屏幕上有兩個UIScrollViews,我需要能夠將UIView從一個滾動視圖拖到另一個滾動視圖。
目前,我要移動的UIView上有一個UILongGestureRecognizer,這樣,當用戶開始拖動它時,便使視圖跟隨觸摸:
- (void)dragChild:(UILongPressGestureRecognizer *)longPress {
[[longPress view] setCenter:[longPress locationInView:[[longPress view] superview]]];
}
但是,當我獲得起始UIScrollView的邊界時,該視圖消失了,因為它已鎖定在該滾動視圖的范圍內。
當我開始拖動時,是否有辦法將其從彈出視圖中“彈出”,以便可以將其帶到另一個滾動視圖中?
另外,如何測試“放置”位置? 我想知道是否已將其丟棄在其他某個視圖上。
還是我會以錯誤的方式進行處理?
多謝你們
如果您需要將其從滾動視圖拖動到另一個視圖,請執行以下操作(偽代碼)
當您開始拖動時,請執行以下操作
//scrollView1 is the scroll view that currently contains the view
//Your view is the view you want to move
[scrollView1.superView addSubView:yourView];
現在,當放置視圖時,您將需要知道它是否在另一個scrollview中
//scrollView2 is the second scroll view that you want to move it too
//Your view is the view you want to move
CGPoint point = yourView.center;
CGRect rect = scrollView2.frame;
if(CGRectContainsPoint(rect, point))
{
//Yes Point is inside add it to the new scroll view
[scrollView2 addSubView:yourView];
}
else
{
//Point is outside, return it to the original view
[scrollView1 addSubView:yourView];
}
這是一個未經測試的想法:
您可能必須小心坐標系,在移動事物時使用[UIView convertPoint:toView:]
東西在視圖的不同視角之間進行轉換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.