func beginMoveAnimation(from fromValue: CGRect, to toValue: CGRect) {
let animation = CABasicAnimation(keyPath: "position")
animation.duration = 0.3
animation.isRemovedOnCompletion = false
animation.fillMode = .forwards
animation.fromValue = fromValue.origin
animation.toValue = toValue.origin
view.layer?.add(animation, forKey: "position")
}
func removeMoveAnimation() {
view.layer?.removeAnimation(forKey: "position")
}
public func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
view.layer?.removeAnimation(forKey: "position")
}
}
debugPrint("", indexPaths, highlightState.rawValue)
debugPrint("didChangeItemsAt -- ", draggingItem?.title)
guard let indexPath = indexPaths.first else { return }
guard let shouldItem = collectionView.item(at: indexPath) else {return }
if let replaceItem = replaceItem, replaceItem != shouldItem {
replaceItem.removeMoveAnimation()
}
if let draggingItem = draggingItem {
if draggingItem != shouldItem && shouldItem != replaceItem {
shouldItem.beginMoveAnimation(from: shouldItem.view.frame, to:
draggingItem.view.frame)
}
}
replaceItem = shouldItem
if let windowPoint = view.window?.convertPoint(fromScreen: screenPoint) {
let draggingPoint = collectionView.convert(windowPoint, from: nil)
if let index = collectionView.indexPathForItem(at: draggingPoint) {
if let item = collectionView.item(at: index),
item != draggingItem {
draggingItem?.beginMoveAnimation(from: item.view.frame, to:
item.view.frame)
}
}
}
draggingItem = nil
replaceItem = nil
replaceItemFrame = nil