Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example on CollectionView Drag & Drop Reordering #341

Open
KeeganFerrett opened this issue Jan 4, 2022 · 2 comments
Open

Example on CollectionView Drag & Drop Reordering #341

KeeganFerrett opened this issue Jan 4, 2022 · 2 comments
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@KeeganFerrett
Copy link

Hey there,

Firstly, love the project, making supporting iOS 13 SwiftUI much more enjoyable.

I realise documentation is currently behind the code base, so figured I should ask here.

Is it possible to enable drag and drop recording in the CollectionView? I have been playing around with the API but struggling to find a way to do it (API seems to imply its possible).

My current code

class DropDelegate: CollectionViewDropDelegate {
    func performDrop(info: CollectionViewDropInfo) -> Bool {
        print("performDrop")

        return true
    }
    
    func validateDrop(info: CollectionViewDropInfo) -> Bool {
        print("validateDrop")

        return true
    }
    
    func dropUpdated(info: CollectionViewDropInfo) -> DropProposal? {
        print("dropUpdated")
        return nil
    }
    
    
}

class Item: Identifiable {
    let id = UUID()
    let text: String
    
    init(_ text: String) {
        self.text = text
    }
}

struct CollectionExampleView: View {
    @State var data: [Item] = (1...10).map({ Item("Item #\($0)") })
    let dragDrop = DropDelegate()
    
    var body: some View {
        CollectionView(data, id: \.id) { element in
            Text(element.text)
                .padding()
                .maxWidth(100)
                .background(RoundedRectangle(cornerRadius: 12).fill(Color.gray))

        }
            .onDrop(delegate: dragDrop)
            .isDragActive(.constant(true))
            // played around with .dragItems too but no luck
    }
}

Happy to contribute to some documentation once I figure it out.

@vmanot
Copy link
Member

vmanot commented Jan 6, 2022

@KeeganFerrett I'll try and get some documentation in this weekend, CollectionView does indeed support drag & drop reordering.

@vmanot vmanot added the documentation Improvements or additions to documentation label Jan 6, 2022
@vmanot vmanot self-assigned this Jan 6, 2022
@hapton
Copy link

hapton commented Apr 28, 2023

struct CollectionExampleView: View {
@State var data: [YMHItem] = (1 ... 100).map { YMHItem("Item #($0)") }

var body: some View {
    CollectionView(data, id: \.id) { element in
        Text(element.text)
            .padding()
            .minWidth(160)
            .background(RoundedRectangle(cornerRadius: 12).fill(Color.gray))
    }
    .onMove { from, to in
        data.move(fromOffsets: from, toOffset: to)
    }
}

}

This code works good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants