Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

iphone - Drag-n-Drop from UIPopoverController to other UIView

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

According to the documentation on UIPopoverController, when the popover is presented, it is presented on a special "window". Because of this, simply adding a subview to the popover view controller's content view controller is not sufficient to be able to drag a view outside of the popover view controller's view.

The easiest solution here is to create your own window, add your drag-able view to the window when dragging occurs. Make the window visible for the duration of the drag/drop, and then release your window when complete.

As mentioned above, gesture recognizers (GR) are best suited for Drag/Drop functionality. Once the GR's state has changed to "Began" the GR will control all touches until the "Ended" or "Cancelled" state is achieved which makes it ideal for dragging views between view controllers as well as windows :)

Example:

@interface MySplitViewController : UISplitViewController {

    UIView *dragView;
    UIWindow *dragWindow;
}

Implementation: NOTE we do not need to call "makeKeyAndVisible" on our window. We just need to set its "Hidden" property

From Apple in regards to the makeKeyAndVisible method: // convenience. most apps call this to show the main window and also make it key. otherwise use view hidden property

-(void)dragBegan{

    self.dragWindow = [[UIWindow alloc] initWithFrame:self.view.window.frame];
    [self.dragWindow addSubview:self.dragView];
    [self.dragWindow setHidden:NO];
}

Here we handle the Gesture Recognizer's "Ended" or "Cancelled" state. NOTE: It is important to remove the window when the Drag/Drop is complete or you will lose user interactiveness with the views below.

-(void)dragEnded{

    [self.dragView removeFromSuperview];

    [self.dragWindow setHidden:YES];
    [self.dragWindow release];

    [self.view addSubview:self.dragView];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...