IOS:将imageview从scrollview拖放到视图中

时间:2011-11-04 15:38:27

标签: ios xcode uiscrollview drag-and-drop

  

可能重复:
  how to drag an uiimage from scrollview to another uiimageview in iphone sdk

在我的iPad中我有一个视图,在里面,在左边,我有一个带有10个imageViews的滚动视图;所以我应该将这些图像从scrollview拖放到我的大子视图中;我怎么能这样做?

2 个答案:

答案 0 :(得分:4)

我做了类似于你所描述的事情。我记得把它拉下来我创建了一个我称之为UIGestureRecgonizer的新UIDownwardDragGestureRecognizer。我想我然后遍历scrollviews手势识别器,要求他们等待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,您可能希望将其从superview(scrollview)中删除,并将其作为子视图添加到其他容器中。如果执行此操作,则需要将该点转换为新的坐标空间。如果您希望原始图像保留在滚动视图中,请复制它并将其添加到外部容器中。

要尝试上面的示例并看到它正常工作,您可能需要关闭scrollview上的剪辑,因为我上面给出的示例是从scrollview中拖动UIImageView(尽管通常你会将它添加到某个包含视图)。

答案 1 :(得分:1)

在途中我看到这样做是通过使用覆盖层捕获触摸并传递它们。这允许您在UIScrollView使用它们之前拦截单击。

我在尝试构建一个UITableView(在滚动视图中)可能会将单元格拖放到它上面时看到的

Here is the demo video

此外,我不确定这是多么适用,但我在几天前制作了here is a similar post

在回顾了这个问题后,我构建了一些东西,允许你在UITableViews之间拖放单元格,类似于我之前提到的视频。我的教程可以找到here,结果可以看到in this YouTube video。此方法使用iOS 5中的最新手势功能,并使用长按弹出单元格。

希望你能在这里找到有帮助的东西。