【发布时间】:2011-11-04 15:38:26
【问题描述】:
可能重复:
how to drag an uiimage from scrollview to another uiimageview in iphone sdk
在我的 iPad 中,我有一个视图,在里面,在左侧,我有一个带有 10 个图像视图的滚动视图;所以我应该将这些图像从滚动视图拖放到我的大子视图中;我该怎么做?
【问题讨论】:
标签: ios xcode uiscrollview drag-and-drop
可能重复:
how to drag an uiimage from scrollview to another uiimageview in iphone sdk
在我的 iPad 中,我有一个视图,在里面,在左侧,我有一个带有 10 个图像视图的滚动视图;所以我应该将这些图像从滚动视图拖放到我的大子视图中;我该怎么做?
【问题讨论】:
标签: ios xcode uiscrollview drag-and-drop
我曾经做过你所描述的事情。我记得我创建了一个新的UIGestureRecgonizer,并命名为UIDownwardDragGestureRecognizer。我想我随后遍历了滚动视图手势识别器,要求它们等待 UIDownwardGestureRecognizer 失败,例如:
UIDownwardDragGestureRecognizer *downwardGesture = [UIDownwardGestureRecognizer alloc] initWithTarget:self action:@selector(downwardGestureChanged:)];
[myScrollview addGestureRecognizer:downwardGesture];
for (UIGestureRecognizer *gestureRecognizer in myScrollview.gestureRecognizers)
{
[gestureRecognizer requireGestureRecognizerToFail:myDownwardGesture];
}
完成此设置后,您应该能够执行类似的操作:
- (void) downwardGestureChanged:(UIDownwardDragGestureRecognizer*)gesture
{
CGPoint point = [gesture locationInView:myScrollView];
if (gesture.state == UIGestureRecognizerStateBegan)
{
UIView *draggedView = [myScrollView hitTest:point withEvent:nil];
if ([draggedView isTypeOfClass:[UIImageView class]])
{
self.imageBeingDragged = (UIImageView*)draggedView;
}
}
else if (gesture.state == UIGestureRecognizerStateChanged)
{
self.imageBeingDragged.center = point;
}
else if (gesture.state == UIGestureRecognizerStateEnded ||
gesture.state == UIGestureRecognizerStateCancelled ||
gesture.state == UIGestureRecognizerStateFailed)
{
// Determine if dragged view is in an OK drop zone
// If so, then do the drop action, if not, return it to original location
self.imageBeingDragged = nil;
}
}
在 UIGestureRecognizerStateBegan 中,一旦找到 UIImageView,您可能希望将其从父视图(滚动视图)中移除,并将其作为子视图添加到其他容器中。如果您这样做,您将需要将该点转换到新的坐标空间中。如果您希望原始图像保留在滚动视图中,请对其进行复制并将其添加到外部容器中。
要试用上面的示例并查看它是否正常工作,您可能需要关闭滚动视图上的剪辑,因为我上面给出的示例是从滚动视图内部拖动 UIImageView(尽管通常您会将其添加到一些包含视图)。
【讨论】:
我已经看到这样做是通过使用覆盖层来捕获触摸并传递它们。这允许您在 UIScrollView 使用它们之前拦截点击。
Here is the demo video 我在尝试构建一个 UITableView(在滚动视图中)时看到的,它可以将单元格拖放到上面。
我也不确定这有多适用,但here is a similar post我几天前做了。
在对这个问题进行了更多审查后,我构建了一些东西,允许您在 UITableView 之间拖放单元格,类似于我之前提到的视频。我的教程可以在here 找到,结果可以在in this YouTube video 看到。此方法使用 iOS 5 中最新的手势功能,并使用长按弹出单元格。
希望你能在这里找到一些有用的东西。
【讨论】: