我正在尝试在斯坦福大学193p 2021上使用EmojiArt应用程序,如果该参考有帮助的话。否则,正如标题所述,我试图在每个表情符号上添加onDrag修饰符,但似乎当我拖动一个时,整个表情符号字符串都会被拖动。如何解决这个问题?
import SwiftUI
struct EmojiArtDocumentView: View {
@ObservedObject var document: EmojiArtDocument
let defaultmojiFontSize: CGFloat = 40
var body: some View {
VStack(spacing: 0) {
documentBody
palette
}
.padding()
}
var documentBody: some View {
GeometryReader { geometry in
ZStack {
Color.yellow
ForEach(document.emojis) { emoji in
Text(emoji.text)
.font(.system(size: fontSize(for: emoji)))
.position(position(for: emoji, in: geometry))
}
}
}
// .onDrop(of: [.plainText], isTargeted: nil) {
// providers, location in
// return false
// }
}
private func drop(providers: [NSItemProvider], at location: CGPoint) -> Bool {
false
}
private func fontSize(for emoji: EmojiArt.Emoji) -> CGFloat {
CGFloat(emoji.size)
}
private func position(for emoji: EmojiArt.Emoji, in geometry: GeometryProxy) -> CGPoint {
convertFromEmojiCoordinates((emoji.x, emoji.y), in: geometry)
}
private func convertFromEmojiCoordinates(_ location: (x: Int, y: Int), in geometry: GeometryProxy) -> CGPoint {
let center = geometry.frame(in: .local).center
return CGPoint(
x: center.x + CGFloat(location.x),
y: center.y + CGFloat(location.y)
)
}
var palette: some View {
ScrollingEmojisView(emojis: testemojis)
.font(.system(size: defaultmojiFontSize))
}
let testemojis = "😀🥰😌😇🤓🤩🤪😤👻"
}
struct ScrollingEmojisView: View {
let emojis: String
var body: some View {
ScrollView(.horizontal) {
HStack {
ForEach(emojis.map {String($0) }, id: \.self) { emoji in
Text(emoji)
.onDrag {
NSItemProvider(object: emoji as NSString)
}
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
EmojiArtDocumentView(document: EmojiArtDocument())
}
}
extension CGRect {
var center: CGPoint {
CGPoint(x: midX, y: midY)
}
}
1条答案
按热度按时间h5qlskok1#
您可以尝试插入ZStack并修改documentBody属性的ForEach,如下所示:
这样,每个ZStack将被识别为一个单独的可拖动视图,您将能够拖动单个表情符号字符。