从UIPopoverController拖放到其他UIView [英] Drag-n-Drop from UIPopoverController to other UIView

查看:88
本文介绍了从UIPopoverController拖放到其他UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何实现将UIView从 UIPopoverController 拖放到后面 UIView

How would I go about implementing dragging and dropping a UIView from UIPopoverController into the back UIView.

这是Pages在其插入媒体弹出窗口中提供的功能,您可以在其中从 UIPopoverController 中拖出一个形状并删除它进入主文档。

This is the functionality that Pages provide in their insert media popover, where you can drag a shape out from the UIPopoverController and drop it into the main document.

我实际上对pan UIGestureRecognizers 感到困惑,以及它们将在何处实现。

I am actually confused with the pan UIGestureRecognizers and where they will be implemented.

谢谢,

Umer

推荐答案

你必须处理两个视图控制器,一个在后台,名为mainController,使用名为popoverController的 UIPopoverViewController 。您的popoverController可以向视图添加 UIPanGestureRecognizer ,用户可以拖动。 gestureRecognizer的操作目标可以是popoverController上的一个方法。

You have to deal with two view controllers one that's in the background called mainController one that presented using a UIPopoverViewController called popoverController. Your popoverController could add a UIPanGestureRecognizer to the views, that the user can drag. The action target of the gestureRecognizer could be a method on the popoverController.

一旦用户开始拖动操作,就会使用gestureRecognizer作为参数调用action方法, gestureRecognizer的状态是 UIGestureRecognizerStateBegan 。您可以保存视图的当前帧,以便在丢弃失败时能够为其设置动画。可能有必要将视图移动到另一个superview(例如窗口),因为我不确定UIPopoverViewController是否会剪切其视图。

Once the user starts a dragging operation your action method is called with the gestureRecognizer as an argument, were the state of the gestureRecognizer is UIGestureRecognizerStateBegan. You could than save the current frame of the view somewere to be able to animate it back, when the dropping fails. It might be necessary to move the view to an other superview (the window for example), because I'm not sure if UIPopoverViewController clipsToBounds its view.

当用户拖动时,使用状态 UIGestureRecognizerStateChanged 中的gestureRecognizer一遍又一遍地调用您的action方法。使用 UIPanGestureRecognizer 上的 translationInView:方法来确定用户拖动和更新拖动视图中心/帧/转换的程度相应。

As the user draggs, your action method is called over and over with the gestureRecognizer in the state UIGestureRecognizerStateChanged. Use the translationInView: method on UIPanGestureRecognizer to determine how much the user dragged and update the dragged views center/frame/transform accordingly.

一旦用户抬起手指,最后一次调用action方法,并将gestureRecoginzers状态设置为 UIGestureRecognizerStateEnded 。现在是时候找出拖动是否成功了。例如,popoverController可以通过委托询问mainController是否在视图当前位置下有一个放置目标,如果是这样,mainController可以采取行动,否则popoverController会将拖动的视图设置为动画,如果它来自,并将其添加回作为subview to it view。

Once the user lifts his finger the action method is called for a last time with the gestureRecoginzers state set to UIGestureRecognizerStateEnded. Now it's time to find out if the drag was successful. For example the popoverController could ask the mainController via delegation if there's a drop target under the views current position, if so the mainController can take action, else the popoverController would animate the dragged view back to were it came from, and add it back as a subview to it's view.

我希望这在某种程度上是可以理解和有用的。

I hope this is somehow comprehensible and helpful.

这篇关于从UIPopoverController拖放到其他UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆