HomeiOS Developmentios - dragging on one TextView drags total assortment in SwiftUI with...

ios – dragging on one TextView drags total assortment in SwiftUI with onDrag modifier


I am making an attempt to work by the EmojiArt app on Stanford 193p 2021, if that reference helps. In any other case, because the title says, I am making an attempt so as to add an onDrag modifier on every of the emoji chars, but it surely seems as if after I drag on one, all the string of emoji chars will get dragged. how one can repair this?

import SwiftUI

struct EmojiArtDocumentView: View {
    @ObservedObject var doc: EmojiArtDocument
    let defaultmojiFontSize: CGFloat = 40
    var physique: some View {
        VStack(spacing: 0) {
            documentBody
            palette
        }
        .padding()
    }
    
    var documentBody: some View {
        GeometryReader { geometry in
            ZStack {
                Colour.yellow
                ForEach(doc.emojis) { emoji in
                    Textual content(emoji.textual content)
                        .font(.system(dimension: fontSize(for: emoji)))
                        .place(place(for: emoji, in: geometry))
                }
            }
        }
//        .onDrop(of: [.plainText], isTargeted: nil) {
//            suppliers, location in
//            return false
//        }
    }
    
    non-public func drop(suppliers: [NSItemProvider], at location: CGPoint) -> Bool {
        false
    }
    
    non-public func fontSize(for emoji: EmojiArt.Emoji) -> CGFloat {
        CGFloat(emoji.dimension)
    }
    
    non-public func place(for emoji: EmojiArt.Emoji, in geometry: GeometryProxy) -> CGPoint {
        convertFromEmojiCoordinates((emoji.x, emoji.y), in: geometry)
    }
    
    non-public func convertFromEmojiCoordinates(_ location: (x: Int, y: Int), in geometry: GeometryProxy) -> CGPoint {
        let heart = geometry.body(in: .native).heart
        return CGPoint(
            x: heart.x + CGFloat(location.x),
            y: heart.y + CGFloat(location.y)
        )
    }
    
    var palette: some View {
        ScrollingEmojisView(emojis: testemojis)
            .font(.system(dimension: defaultmojiFontSize))
    }
    
    let testemojis = "😀🥰😌😇🤓🤩🤪😤👻"
}

struct ScrollingEmojisView: View {
    let emojis: String
    
    var physique: some View {
        ScrollView(.horizontal) {
            HStack {
                ForEach(emojis.map {String($0) }, id: .self) { emoji in
                    Textual content(emoji)
                        .onDrag {
                            NSItemProvider(object: emoji as NSString)
                        }
                }
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        EmojiArtDocumentView(doc: EmojiArtDocument())
    }
}

extension CGRect {
    var heart: CGPoint {
        CGPoint(x: midX, y: midY)
    }
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments