import UniformTypeIdentifiers
UIPasteboard.general.items = [
[UTType.text.identifier: "Meet Transferable"],
[UTType.image.identifier: swiftUILogo]
]
UTType.text // public.text
UTType.image // public.image
UTType.data // public.data
import CoreTransferable
extension UTType {
static let myColor = UTType(exportedAs: "ru.cleverpumpkin.Meet.mycolor")
}
struct MyColor: Codable {
let name: String
let red: Double
let green: Double
let blue: Double
}
extension MyColor: Transferable {
static var transferRepresentation: some TransferRepresentation {
// Репрезентация
}
}
extension MyColor: Transferable {
static var transferRepresentation: some TransferRepresentation {
CodableRepresentation(contentType: .myColor)
}
}
extension MyColor: Transferable {
static var transferRepresentation: some TransferRepresentation {
DataRepresentation(contentType: .myColor) { myColor in
myColor.convertToData()
} importing: { data in
MyColor(data: data)
}
}
}
extension MyColor: Transferable {
static var transferRepresentation: some TransferRepresentation {
FileRepresentation(contentType: .myColor) { myColor in
SentTransferredFile(myColor.saveAndReturnURL())
} importing: { receivedTransferredFile in
MyColor(url: receivedTransferredFile.file)
}
}
}
extension MyColor: Transferable {
static var transferRepresentation: some TransferRepresentation {
CodableRepresentation(contentType: .myColor)
ProxyRepresentation(exporting: \\.name)
}
}
struct DraggableColor: View {
let myColor: MyColor
var body: some View {
Color(myColor: myColor)
.draggable(myColor)
.frame(width: 50, height: 50)
.cornerRadius(8)
}
}
struct DropRectangle: View {
@Binding var draggedMyColor: MyColor?
var body: some View {
VStack {
RoundedRectangle(cornerRadius: 8)
.foregroundColor(Color(maybeMyColor: draggedMyColor) ?? .gray.opacity(0.4))
.frame(width: 200, height: 130)
.dropDestination(for: MyColor.self) { items, location in
withAnimation(.easeInOut(duration: 0.15)) {
draggedMyColor = items.first
}
return true // Allow to drop
}
if let colorName = draggedMyColor?.name {
Text(colorName)
}
}
}
}
.dropDestination(for: MyColor.self) { items, location in
withAnimation(.easeInOut(duration: 0.15)) {
draggedMyColor = items.first
}
return true // Allow to drop
}