简体   繁体   English

SwiftUI 拖放错误“NSInternalInconsistencyException”

[英]SwiftUI Drag and Drop error “NSInternalInconsistencyException”

I have a swiftUI app that implements the native drag and drop apis.我有一个实现本机拖放 api 的 swiftUI 应用程序。 Everything works well but I have discovered an edge case that I don't understand how to fix.一切正常,但我发现了一个我不明白如何修复的边缘情况。 Essentially the entire view of the app (the size of the device screen) is a drop target.本质上,应用程序的整个视图(设备屏幕的大小)都是放置目标。 When dragging an object to be dropped, if the user moves the item to the edge of the screen the app will crash throwing the error:当拖动 object 被拖放时,如果用户将项目移动到屏幕边缘,应用程序将崩溃并抛出错误:

'NSInternalInconsistencyException', reason: UIDragPreviewTarget requires that the container view is in a window, but it is not

I assume this has to do with the drop target and trying to move the drag object out of the app's window, but I don't know what to do to handle this edge case.我认为这与放置目标有关,并试图将拖动 object 移出应用程序的 window,但我不知道如何处理这种边缘情况。

Any help would be greatly appreciated任何帮助将不胜感激

EDIT: I commented out the .onDrop() modifier and the crash still persists.编辑:我注释掉了.onDrop()修饰符并且崩溃仍然存在。 I can only guess that the generated UIDragPreviewTarget is tracking its location internally but when it is pulled out of the Frame of the app it throws the error我只能猜测生成的UIDragPreviewTarget正在内部跟踪它的位置,但是当它被拉出应用程序的框架时,它会抛出错误

After digging around a bunch more and creating a new simplified project to demonstrate this wasn't a bug in the Drag and Drop implementation in SwiftUI, I think I found the issue.在挖掘了更多并创建了一个新的简化项目以证明这不是 SwiftUI 中的拖放实现中的错误之后,我想我找到了问题。

In my case I have a view that holds the data I need sitting above the view I want to be able to drag that data into.就我而言,我有一个视图,其中包含我需要的数据,位于我希望能够将数据拖入的视图上方。 While in the drag process, I wanted the top view to dismiss itself so as to no longer obstruct the lower view.在拖动过程中,我希望顶视图自行消失,以便不再阻碍下视图。 I was flipping this switch in one of two ways.我以两种方式之一拨动了这个开关。

  1. when starting the drag process on the original piece of data, I started a timer that would give you 1.5 seconds to cancel the drop before it would hide the view.当开始对原始数据的拖动过程时,我启动了一个计时器,它会给你 1.5 秒的时间来取消拖放,然后它才会隐藏视图。
  2. when entering the lower view's drop target, it would close the top view当进入下视图的放置目标时,它会关闭顶视图

Removing both of these actions eliminated the crash.删除这两个操作消除了崩溃。

My assumption at this point is that hiding the original source of the data is tripping up the dragItem's sense of "home" and when you try and drag it off screen it has no where to return back too.我在这一点上的假设是,隐藏数据的原始来源会破坏 dragItem 的“家”感,当您尝试将其拖出屏幕时,它也没有返回的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM