【问题标题】:UIPanGestureRecognizer global translate coordinateUIPanGestureRecognizer 全局平移坐标
【发布时间】:2018-04-20 16:24:40
【问题描述】:

我正在使用UIPanGestureRecognizer 将图像拖出UICollectionViewCell,然后查看它是否与屏幕上其他位置的另一个图像发生冲突。 为此,我需要能够匹配他们的翻译坐标。我的理解是,由于图像位于 collectionView 单元格内,因此它被跟踪相对单元格坐标,我需要成为全局跟踪器。

|---------------------------------------|
|        _______                        |
|       |_image_|                       |
|                                       |
|---------------------------------------|
|       |image|     Collection View     |
|---------------------------------------|

到目前为止,当我将图像拖到另一个图像上时,我将其作为日志:

moved imageView center: (-44.0, -143.5)
destination imageView center: (440.0, 175.0)

let translation = panGesture.translation(in: self.view.superview)
self.view.center = CGPoint(x: self.initialLocation.x + translation.x, y: self.initialLocation.y + translation.y)

我也尝试将self.view.superview 替换为mainView,但无济于事:

let window = UIWindow(frame: UIScreen.main.bounds)
let mainView = window.rootViewController?.view

我做错了什么,如何在全局范围内跟踪拖动的图像?

【问题讨论】:

    标签: swift uigesturerecognizer uipangesturerecognizer


    【解决方案1】:

    为此,您必须遵循程序。

    1. 首先,当panGesture 调用时,您必须创建一个snapshotview/imageview(无论怎样)。

    2. 将该快照添加为self.view 的子视图(VC 主视图)

    3. 要获得视图 w.r.t 主视图的位置,您必须使用 convert(_:to:)。现在根据这个设置快照帧。

    4. 您可以在此之后隐藏您的view/imageview,以便在用户交互中看起来像用户在移动相同的view/Imgview

    5. 现在匹配你的图像,做你想做的事。

    6. 如果您想将该图像放在其原始空间,则在用户从panGesture 结束时取消隐藏该图像。

    希望现在你会更清楚你实际要做的事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-23
      • 2011-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多