iOS(iPad)在UISplitViewController中拖放

Reu*_*ven 5 ipad uisplitviewcontroller ios

我正在使用UISplitViewController(基于Xcode模板项目)进行D&D,从MasterViewController到DetailViewController.

基本上,我正在做的是创建一个UILongPressGestureRecognizer并将其放在MasterViewController的self.tableview属性上.

下面是我的手势识别器.

- (void)gestureHandler:(UIGestureRecognizer*)gesture
{
    CGPoint location;
    NSIndexPath* indexPath = [self.tableView indexPathForRowAtPoint:[gesture locationInView:self.tableView]];
    if (gesture.state == UIGestureRecognizerStateBegan) {
        // Create draggable view
        NSString* imageCanvasName = [self imageNameByIndexPath:indexPath isDetail:YES];
        UIImage* imageCanvas = [UIImage imageNamed:imageCanvasName];
        _draggedView = [[UIImageView alloc] initWithImage:imageCanvas];

        // Create drag-feedback window, add the drag-view and make the drag-window visible
        CGRect windowFrame = self.view.window.frame;
        _dragFeedbackWindow = [[UIWindow alloc] initWithFrame:windowFrame];
        location = [gesture locationInView:gesture.view.window];
        [_draggedView setCenter:location];
        [_dragFeedbackWindow addSubview:_draggedView];
        [_dragFeedbackWindow setHidden:NO];     
    }
    else if (gesture.state == UIGestureRecognizerStateChanged) {
        // Update drag-view location
        location = [gesture locationInView:gesture.view.window];
        [_draggedView setCenter:location];
    }
    else if (gesture.state == UIGestureRecognizerStateEnded)
    {
        // Disconnect drag-view and hide drag-feedback window
        [_draggedView removeFromSuperview];     
        [_dragFeedbackWindow setHidden:YES];

        // If drop is in a valid location...
        if ([self.tableView pointInside:_draggedView.center withEvent:nil] == NO)
        {
            // Get final location in detailViewController coordinates
            location = [gesture locationInView:self.detailViewController.view];
            [_draggedView setCenter:location];
            [self.detailViewController.view addSubview:_draggedView];
        }
    }
    else {
        NSLog(@"%s unrecognized gesture %d", __FUNCTION__, gesture.state);
    }
}
Run Code Online (Sandbox Code Playgroud)

当iPad处于纵向模式时 - 一切都非常好 - 拖动,下降 - 工作.

如果iPad被旋转,我的问题就开始了...在这种情况下,我的_draggedView显示为"反向旋转" - 它将"反映"iPad的旋转 - 直到掉线.

这就像我必须对_dragFeedbackWindow应用一些旋转 - 但我尝试了很多东西,失败了......

任何的想法?

谢谢!

Reu*_*ven 4

好的 - 我弄清楚了发生这种情况的“原因”,以及要修复的“如何”(并将附加处理程序代码以正确执行此操作,全部如下。

这是代码...“只需”将其添加到您的 MAsterViewController (如果 - 像我一样 - 您想从主视图拖动到细节...)

// A simple UIView extension to rotate it to a given orientation
@interface UIView(oriented)
- (void)rotateToOrientation:(UIInterfaceOrientation)orientation;
@end

@implementation UIView(oriented)
- (void)rotateToOrientation:(UIInterfaceOrientation)orientation {
    CGFloat angle = 0.0;    
    switch (orientation) { 
        case UIInterfaceOrientationPortraitUpsideDown:
            angle = M_PI; 
            break;
        case UIInterfaceOrientationLandscapeLeft:
            angle = - M_PI / 2.0f;
            break;
        case UIInterfaceOrientationLandscapeRight:
            angle = M_PI / 2.0f;
            break;
        default: // as UIInterfaceOrientationPortrait
            angle = 0.0;
            break;
    } 

    self.transform = CGAffineTransformMakeRotation(angle);
}
Run Code Online (Sandbox Code Playgroud)

现在,LongPress 手势处理程序...

- (void)gestureHandler:(UIGestureRecognizer*)gesture
{
    CGPoint location;
    UIWindow* dragFeedback = [UIApplication sharedApplication].delegate.window;
    if (gesture.state == UIGestureRecognizerStateBegan) {
        // Create draggable view
        _draggedView = [[UIImageView alloc] initWithImage:@"someImage.png"];

        // Required to adapt orientation... WORKS, BUT WHY NEEDED???
        [_draggedView rotateToOrientation:[[UIApplication sharedApplication] statusBarOrientation]];

        // Create drag-feedback window, add the drag-view and make the drag-window visible
        location = [gesture locationInView:dragFeedback];
        [_draggedView setCenter:location];
        [dragFeedback addSubview:_draggedView];
    }
    else if (gesture.state == UIGestureRecognizerStateChanged) {
        // Update drag-view location
        location = [gesture locationInView:dragFeedback];
        [_draggedView setCenter:location];
    }
    else if (gesture.state == UIGestureRecognizerStateEnded)
    {
        // Disconnect drag-view and hide drag-feedback window
        [_draggedView removeFromSuperview];     

        // Get final location in detailViewController coordinates
        location = [gesture locationInView:self.detailViewController.view];
        [_draggedView setCenter:location];
        // "Noramlize" orientation... WORKS, BUT WHY NEEDED???
        [_draggedView rotateToOrientation:UIInterfaceOrientationPortrait];
        [self.detailViewController.view addSubview:_draggedView];
    }
    else {
        NSLog(@"%s unrecognized gesture %d", __FUNCTION__, gesture.state);
    }
}
Run Code Online (Sandbox Code Playgroud)

这就像一个魅力 - 让我知道它如何为您工作(如果您需要示例项目,请告诉我......)