在 NSCollectionView 中拖放

问题描述 投票:0回答:0

macOS 的新手,我正在尝试实现一个包含两行(与部分相同?)和每行中的单独水平滚动的 NSCollectionView,我需要能够将项目从顶行拖到底行。

更新:所以我设法创建了我想要的集合视图,现在我正在尝试实现拖放。它在大多数情况下都有效,但我得到了一些奇怪的随机行为。

这就是它的样子。

一开始拖放效果很好,当我在顶部向右滚动时,事情变得有点奇怪。特别是当我选择一个图像时,拖动动画开始到窗口的右边?

这就是我实现 NSCollectionViewDelegate 函数的方式。

extension TopMoviewsView: NSCollectionViewDelegate {
//MARK: NSCollectionViewDelegate

func collectionView(_ collectionView: NSCollectionView,
                    pasteboardWriterForItemAt indexPath: IndexPath) -> NSPasteboardWriting? {
    let pasteBoardItem = NSPasteboardItem()
    pasteBoardItem.setString("\(indexPath.item)", forType: .string)
    return pasteBoardItem
}

func collectionView(_ collectionView: NSCollectionView, acceptDrop draggingInfo: NSDraggingInfo,
                    indexPath: IndexPath, dropOperation: NSCollectionView.DropOperation) -> Bool {
    if let draggedItem = draggingItem,
       let fromIndexPath = collectionView.indexPath(for: draggedItem) {
        //Sorting the top movies section not allowed right now
        if fromIndexPath.section == 0 && indexPath.section == 0 {
            return false
        } else {
            return true
        }
    }
    return true
}

func collectionView(_ collectionView: NSCollectionView, validateDrop draggingInfo: NSDraggingInfo,
                       proposedIndexPath proposedDropIndexPath: AutoreleasingUnsafeMutablePointer<NSIndexPath>,
                       dropOperation proposedDropOperation: UnsafeMutablePointer<NSCollectionView.DropOperation>) -> NSDragOperation {
    
    guard let draggedItem = draggingItem,
          let fromIndexPath = collectionView.indexPath(for: draggedItem) else {
        return .move
    }
    let propossedIndexPath = proposedDropIndexPath.pointee as IndexPath
    //Soerting the favorite movies section
    if fromIndexPath.section == 1 && propossedIndexPath.section == 1 {
        dataSource.sortFavoritesArray(from: fromIndexPath.item, to: propossedIndexPath.item)
    }
    //from topMovies section to favorite movies section
    else if fromIndexPath.section == 0 && propossedIndexPath.section == 1 {
        dataSource.moveTopToFavorites(topIndex: fromIndexPath.item, to: propossedIndexPath.item)
    }
    //from favorite moviews section to topmoview section
    else if fromIndexPath.section == 1 && propossedIndexPath.section == 0 {
        dataSource.moveFromFavoritesToTopMoviesSections(favoritesIndex: fromIndexPath.item,
                                                        to: propossedIndexPath.item)
    }
    if fromIndexPath != propossedIndexPath {
        collectionView.animator().moveItem(at: fromIndexPath, to: propossedIndexPath)
    }
    return .move
   }

func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession,
                    willBeginAt screenPoint: NSPoint, forItemsAt indexPaths: Set<IndexPath>) {
    if let indexPath = indexPaths.first,
       let item = collectionView.item(at: indexPath) {
        draggingItem = item
    }
}

func collectionView(_ collectionView: NSCollectionView, draggingSession session: NSDraggingSession,
                    endedAt screenPoint: NSPoint, dragOperation operation: NSDragOperation) {
    draggingItem = nil
}

我特别困惑:

  let pasteBoardSupportedTypes: [NSPasteboard.PasteboardType] = [.string] moviesCollectionView.registerForDraggedTypes(pasteBoardSupportedTypes)

但如果我不处理这些,它根本不起作用。

swift drag-and-drop appkit nscollectionview nscollectionviewitem
© www.soinside.com 2019 - 2024. All rights reserved.