views:

439

answers:

1

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

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.

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

Thanks,

Umer

+2  A: 

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.

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.

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.

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.

tonklon
Thanks a lot for the detailed answer. I think i understand it now. At UIGestureRecognizerStateBegan I'll add the a new UIView to the window and finally adjusting everything in UIGestureRecognizerStateEnded and removing the UIView from the window would be the way to go for me.Thanks again.
umerh