使用拖动API根据移动位置编辑拖动视图

时间:2018-07-29 08:35:37

标签: swift drag-and-drop

我正在使用拖动API。现在,我可以创建一个视图,并在其中显示一些文本。我想根据用户的移动来更改文本。但是由于某种原因,我找不到路。

这是在开始拖动时创建视图的方式:

    func dragInteraction(_ interaction: UIDragInteraction, previewForLifting item: UIDragItem, session: UIDragSession) -> UITargetedDragPreview? {

        let location = session.location(in: self.dayCollectionView)

        let dayNumberOfItems: CGFloat = CGFloat(viewModel.getCollectionConstants().DayNumOfItems)

        let scale = (self.dayCollectionView.contentSize.width) / dayNumberOfItems

        let day = Int(location.x / scale)
    print(day)
        let centerX = CGFloat(day)*scale + scale*0.5 + (viewModel.orientation.isLandscape ? 0 : 50)

        let target = UIDragPreviewTarget(container: self.dayCollectionView, center: CGPoint(x: centerX, y: location.y))


        let prev = DragAddEventView(frame: CGRect(x: 0, y: 0, width: scale, height: 50))
//THIS IS THE UPDATE I WANT TO TRIGGER WHILE MOVING THIS VIEW AROUND.
        prev.timeLabel.text = viewModel.textForDragView(y: location.y)

        return UITargetedDragPreview(view: prev, parameters: UIDragPreviewParameters(), target: target)

}

如代码所示,我正在初始化DragAndDropEventView。我想在用户移动时使用以下代码对其进行更新:

prev.timeLabel.text = viewModel.textForDragView(y: location.y)

我了解到,有可能需要为每个移动创建另一个视图实例。但是我不确定什么是正确的做法。

0 个答案:

没有答案