【问题标题】:Neither the view or container of the UITargetedPreview is currently in a windowUITargetedPreview 的视图或容器当前都不在窗口中
【发布时间】:2020-05-19 14:10:10
【问题描述】:

完整的错误状态:

客户端应用程序错误 - 既不是视图也不是容器 UITargetedPreview 当前在一个窗口中。这是违反 UIDragInteraction API 合同并可能导致严重的视觉故障。 这是一个客户端应用程序错误,很快就会成为一个硬断言。请修理我

自从我将 Xcode 升级到 11.4.1 的那一天,这个不错的小警告就出现了 之前,这个警告从未在我的控制台中弹出。

所以我从一个 UIView 拖到另一个 UIView 中,不涉及 UITableViews 或 UICollectionViews。 我在 UIView 中设置了一个 draginteraction,在接收 UIView 上设置了一个 dropinteraction。

这是来自 UIDragInteraction 委托的代码

- (UITargetedDragPreview *)dragInteraction:(UIDragInteraction *)interaction previewForLiftingItem:(UIDragItem *)item session:(id<UIDragSession>)session {
    UIView *view = interaction.view;
    CGPoint point = [session locationInView:interaction.view];
    UIDragPreviewTarget *target = [[UIDragPreviewTarget alloc] initWithContainer:view center:point];

    UIView *previewView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.elementDimension, self.elementDimension)];
    [previewView setBackgroundColor:[UIColor colorWithRed:0.98 green:0.97 blue:0.89 alpha:1.00]];
    //I've removed the filling of previewView because it has nothing to see with the question

    if (@available(iOS 13.0, *)) {
        UIPreviewParameters *previewParameters = [[UIPreviewParameters alloc] init];
        [previewParameters setBackgroundColor:[UIColor clearColor]];

        return [[UITargetedDragPreview alloc] initWithView:previewView
                                                parameters:previewParameters
                                                    target:target];
    } else {
        // Fallback on auto generated versions
        return nil;
    }
}

我不明白这是什么意思:

  • 视图
  • 窗户

在控制台的错误消息中。我应该在初始化 UITargetedDragPreview 之前将 previewView 添加到视图层次结构中吗?

谢谢!

【问题讨论】:

    标签: ios xcode drag-and-drop ios13


    【解决方案1】:

    我已经为此苦苦挣扎了很长时间......我所做的是在拖动开始时将我的自定义预览(您的previewView)添加到集合视图中,如下所示

    - ( NSArray < UIDragItem * > * ) collectionView:( UICollectionView   * ) collectionView
                       itemsForBeginningDragSession:( id < UIDragSession > ) session
                                        atIndexPath:( NSIndexPath        * ) indexPath
    
    {
      // ... make preview
    
      // add to collection view
      [collectionView addSubview:preview];
    
      // retain pointer
      self.preview = preview;
    
      // ...
    }
    

    然后保留指向它的(弱)指针并再次删除它,如下所示

    - ( void ) collectionView:( UICollectionView   * ) collectionView
            dragSessionDidEnd:( id < UIDragSession > ) session
    {
        // overkill I know but whatever
        if ( self.preview )
        {
            self.preview.removeFromSuperview;
            self.preview = nil;
        }
    }
    

    这似乎有效,您将看到视图被添加并随后从集合视图中删除。为了摆脱它,我尝试将其设置为隐藏,但没有奏效,所以我最终做了类似的事情

      preview.frame = CGRectMake( -1000, -1000, preview.frame.size.width, preview.frame.size.height );
    

    相当老套,但到目前为止还不错。虽然我的是用于集合视图,但我认为它很容易转移到您的环境中。

    【讨论】:

    • 感谢您的出色解决方案!这很好用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-29
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多