gpt4 book ai didi

swiftui - 在 macOS 上的 SwiftUI 中将颜色从源拖放到目标

转载 作者:行者123 更新时间:2023-12-05 02:54:36 25 4
gpt4 key购买 nike

如何将颜色从来源拖放到目的地并保留下来?我目前有代码可以临时执行此操作(从源拖动时更改目标颜色),但它会在鼠标抬起时迅速变回。那么,我怎样才能让颜色变化保持在鼠标悬停在特定单元格上,并且能够在以后将鼠标悬停在该特定单元格上时再次更改该单元格?我当前的代码基于 here .

drawing

import SwiftUI

struct ContentView: View {
var body: some View {
HStack {
ColorSource(color: .purple, color2: .orange)
Spacer()
ColorSource(color: .blue, color2: .pink)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}

struct DestinationDataKey: PreferenceKey {
typealias Value = [DestinationData]

static var defaultValue: [DestinationData] = []

static func reduce(value: inout [DestinationData], nextValue: () -> [DestinationData]) {
value.append(contentsOf: nextValue())
}
}

struct DestinationData: Equatable {
let destination: Int
let frame: CGRect
}

struct DestinationDataSetter: View {
let destination: Int

var body: some View {
GeometryReader { geometry in
Rectangle()
.fill(Color.clear)
.preference(key: DestinationDataKey.self,
value: [DestinationData(destination: self.destination, frame: geometry.frame(in: .global))])
}
}
}

struct DestinationView: View {
@Binding var active: Int
let label: String
let id: Int
let color: Color

var body: some View {
Text(label).padding(10).background(self.active == id ? color : Color.green)
.background(DestinationDataSetter(destination: id))
}
}

struct ColorSource: View {
@State var active = 0
@State var destinations: [Int: CGRect] = [:]
@State var color:Color
@State var color2:Color
@State var draggingFirstColor:Bool = false
@State var draggingSecondColor:Bool = false

var body: some View {
VStack {

Text("Drag From Here").padding().background(color)
.gesture(DragGesture(minimumDistance: 0.1, coordinateSpace: .global)
.onChanged { value in
self.draggingFirstColor = true
self.active = 0
for (id, frame) in self.destinations {
if frame.contains(value.location) {
self.active = id
}
}
}
.onEnded { value in
// do something on drop
self.active = 0
self.draggingFirstColor = false

}
)

Text("Drag From Here").padding().background(color2)
.gesture(DragGesture(minimumDistance: 0.1, coordinateSpace: .global)
.onChanged { value in
self.draggingSecondColor = true
self.active = 0
for (id, frame) in self.destinations {
if frame.contains(value.location) {
self.active = id
}
}
}
.onEnded { value in
// do something on drop
self.active = 0
self.draggingSecondColor = false
}
)

Divider()
DestinationView(active: $active, label: "Drag Over Me", id: 1, color: (draggingFirstColor ? color : (draggingSecondColor ? color2 : Color.green)))
DestinationView(active: $active, label: "Drag Over Me", id: 2, color: (draggingFirstColor ? color : (draggingSecondColor ? color2 : Color.green)))
DestinationView(active: $active, label: "Drag Over Me", id: 3, color: (draggingFirstColor ? color : (draggingSecondColor ? color2 : Color.green)))
DestinationView(active: $active, label: "Drag Over Me", id: 4, color: (draggingFirstColor ? color : (draggingSecondColor ? color2 : Color.green)))
}.onPreferenceChange(DestinationDataKey.self) { preferences in
for p in preferences {
self.destinations[p.destination] = p.frame
}
}
}
}

最佳答案

只是不要在结束时重置拖动颜色...或将其存储在某个地方(比如在模型中并在目标中使用)

demo

.onEnded { value in
// do something on drop
// self.active = 0
// self.draggingFirstColor = false // same for second

}

关于swiftui - 在 macOS 上的 SwiftUI 中将颜色从源拖放到目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61716755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com