本文介绍了获取拖动预览的帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iOS 11中引入的新Apple API在 UICollectionView 上实现拖放。

I'm implementing drag-n-drop on UICollectionView using a new Apple's API introduced in iOS 11.

我需要得到一个拖动预览的框架(请参见下面的红色矩形)

What I need is to get a frame of drag preview (see below, red rectangle)

我尝试获取拖动的位置:

I tried to get drag's location:

extension ViewController: UICollectionViewDropDelegate {
    func collectionView(_ collectionView: UICollectionView, 
         dropSessionDidUpdate session: UIDropSession, 
         withDestinationIndexPath destinationIndexPath: IndexPath?) -> UICollectionViewDropProposal {
        let location = session.location(in: collectionView)
    }

但这仅代表我手指的位置。我需要获取整个框架,或者至少是原始框架。

but it represents my finger's location only. I need to get the entire frame or, at least, it's origin.

我还尝试存储 y 位置触摸单元格中的触摸,然后执行 dragLocation.y-touchLocationInCell.y 之类的操作,但是这种方法并不准确:看起来预览与触摸位置有些偏移。

I also tried to store y position of touch in my cell and then do something like dragLocation.y - touchLocationInCell.y, but this approach is not accurate: looks like the preview gets some offset from the touch location.

推荐答案

我已经研究过类似的功能,并且可以共享一些细节。简而言之,似乎没有允许跟踪预览视图位置的选项。

I have worked on similar functionality and can share some details. To be short, it seems that there is no option that allows position tracking of preview view.

从我的角度来看,我不会依赖于预览视图的框架。在某些情况下,系统可能会使预览视图小于或大于原始视图。例如,当初始拖动的视图尺寸大于其超级视图尺寸时,可能会发生这种情况。在这种情况下,系统将使预览视图更小,以便为正在拖动的内容带来更多可见性。而且这种行为是不可预测的,系统可以基于一些内部逻辑来应用这种转换。

From my perspective, I would not rely on the frame of the preview view. There are some cases when system could make preview view smaller or bigger than original view. For instance, it could happen when initial dragged view size is bigger than it's super view size. In this case system will make preview view smaller in order to bring more visibility about content that is being dragged. And this behavior is not predictable, system could apply such transformation based on some internal logic.

我认为它的工作方式是因为拖放引擎不仅可以在一个特定的应用程序内移动项目,还可以在其他应用程序之间移动项目。从那以后,拖放引擎应该是一个单独的系统过程,处理所有拖放操作,包括预览大小和位置。

I think the way it works is because drag and drop engine allows to move items not only inside one specific app but between other apps. Since that, drag and drop engine should be a separate system process that handles all drag and drop operations including preview sizing and position.

在拖动操作期间,系统会创建两个原始视图从 UIDragItem 的PreviewProvider返回的视图的快照或快照。

During drag operation, system creates either original view snapshot or snapshot of the view that was returned from previewProvider of UIDragItem.

然后,它创建容器视图(实例为私有 UIPortalView 类,并在其下面的 CAPortalLayer ),将快照安装到此容器中并显示容器视图。由于那一刻预览视图的位置是由系统处理的,因此没有公共API可以访问该视图。

Then it creates container view (instance of private UIPortalView class with CAPortalLayer underneath), installs snapshot into this container and displays container view. Since that moment preview view position is handled by system and there is no public API to access this view.

跟踪拖动位置的唯一可能方法是使用 location(在视图中:UIView)函数, UIDragDropSession 协议。

The only possible way to track drag position is to use location(in view: UIView) function of UIDragDropSession protocol.

希望对您有所帮助。

这篇关于获取拖动预览的帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 05:31