Drag-n-Drop from UIPopoverController to other UIView

前端 未结 2 663
执笔经年
执笔经年 2020-12-13 16:32

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

This is the functionality t

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-13 17:06

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

提交回复
热议问题