xcode IOS:将图像视图从滚动视图拖放到视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8011849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:20:03  来源:igfitidea点击:

IOS: drag and drop imageview from a scrollview to a view

iosxcodeuiscrollviewdrag-and-drop

提问by cyclingIsBetter

Possible Duplicate:
how to drag an uiimage from scrollview to another uiimageview in iphone sdk

可能的重复:
如何将 uiimage 从滚动视图拖动到 iphone sdk 中的另一个 uiimageview

In my iPad I have a view and inside it, on the left, I have a scrollview with 10 imageViews; so I should drag and drop these images from scrollview to my big subview; how can I do it?

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

回答by Sam

I did something like what you are describing once. I remember to pull it off I created a new UIGestureRecgonizerthat I dubbed UIDownwardDragGestureRecognizer. I think I then iterated through the scrollviews gesture recognizers to require them to wait for the UIDownwardGestureRecognizerto fail like:

我曾经做过类似你所描述的事情。我记得把它拉下来,我创建了一个新的UIGestureRecgonizer,我称之为UIDownwardDragGestureRecognizer. 我想我然后遍历滚动视图手势识别器以要求他们等待UIDownwardGestureRecognizer失败,例如:

UIDownwardDragGestureRecognizer *downwardGesture = [UIDownwardGestureRecognizer alloc] initWithTarget:self action:@selector(downwardGestureChanged:)];
[myScrollview addGestureRecognizer:downwardGesture];
for (UIGestureRecognizer *gestureRecognizer in myScrollview.gestureRecognizers)
{
   [gestureRecognizer requireGestureRecognizerToFail:myDownwardGesture];
}

Once you have this setup, you should be able to do something like:

一旦你有这样的设置,你应该能够做一些类似

- (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;
   }
}

In the UIGestureRecognizerStateBegan, once you find a UIImageView you may want to remove it from the superview (scrollview) and add it as a subview to some other container. You will need to translate the point into the new coordinate space if you do this. If you want the original image to stay in the scrollview, then make a copy of it and add that to an outside container.

在 UIGestureRecognizerStateBegan 中,一旦找到 UIImageView,您可能希望将其从超级视图 (scrollview) 中删除,并将其作为子视图添加到其他容器中。如果您这样做,您将需要将点转换到新的坐标空间中。如果您希望原始图像保留在滚动视图中,请复制它并将其添加到外部容器中。

To try out the above example and see it working, you may need to turn off clipping on the scrollview since the example I gave above is dragging the UIImageView from inside the scrollview (though normally you'd add it to some containing view).

要试用上面的示例并查看它是否有效,您可能需要关闭滚动视图上的剪辑,因为我上面给出的示例是从滚动视图内部拖动 UIImageView(尽管通常您会将其添加到某些包含视图中)。

回答by Scott Sherwood

On way I have seen this done is by using an overlay layer to capture the touches and pass them through. This allows you to intercept the clicks before the UIScrollView consumes them.

我已经看到这样做的方法是使用覆盖层来捕获触摸并传递它们。这允许您在 UIScrollView 使用它们之前拦截点击。

Here is the demo videothat I looked at when trying to construct a UITableView (inside a scroll view) that could have cells dragged and dropped on to it.

这是我在尝试构建可以将单元格拖放到其上的 UITableView(在滚动视图内)时查看的演示视频

Also I am not sure how applicable this is but here is a similar postI made a few days ago.

此外,我不确定这有多适用,但这是我几天前发表的类似帖子

Having reviewed this issue some more I built something that allows you to drag and drop cells between UITableViews similar to the video I mentioned previously. My tutorial can be found hereand the result can be seen in this YouTube video. This method uses the latest gesture features in iOS 5 and pops cells off using a long press.

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

Hope you can find something in here that helps.

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