Search code examples
iosswiftswiftuidraggesture

SwiftUI DragGestures not updating until different DragGesture triggered


this gif should pretty clearly show my issue.

enter image description here

In words, I have a DragGesture for the background color, and TapGesture to add Text() views. Each Text View has a DragGesture on it to update its location as its dragged. This works, but the visually the Text Views position don't update until I activate the background color DragGesture. I've confirmed that the Text Views position values update, they just don't visually update.

Also, I have a long press gesture which 'locks' the background color by preventing a call to moodColor(). When this is locked, the Text Views will not move until I perform another long press to unlock it, and then drag the background color around.

I have a feeling I'm misusing DragGestures, but it seemed obvious that each view should have it's own gesture.

Here is the code.

class Note : Identifiable{
    var id = UUID()
    var text: Text
    var dragOffset : CGPoint
     @GestureState private var location: CGPoint = .zero
    var drag : some Gesture{ DragGesture(minimumDistance: 0.5, coordinateSpace: .global).onChanged{ value in self.dragOffset = value.location }
        .updating($location){ (value, state, transaction) in state = value.location }
    }

    init(textVal: String){
        self.text = Text(textVal).font(.largeTitle)
        self.dragOffset = CGPoint(x: 50, y: 50)
    }
}

struct Col{
    var red: Double = 1.0
    var green: Double = 1.0
    var blue: Double = 1.0
}

struct ContentView: View {
    @State var brightness = 0.9
    @State var color = Col()        // Color Grid Style, RGB
    @GestureState var dragInfo = CGSize.zero
    @State private var myviews: [Note] = []
    @State private var colorLocked = false

    var body: some View {
        return GeometryReader { geo in
            Color.init( red: self.color.red * self.brightness, green: self.color.green * self.brightness, blue: self.color.blue * self.brightness)

                .edgesIgnoringSafeArea(.all)
                .gesture( TapGesture().onEnded{ value in self.myviews.append(Note(textVal: "A Note")) })
                .gesture( LongPressGesture(minimumDuration: 0.5).onEnded{
                            _ in UIImpactFeedbackGenerator(style: .heavy).impactOccurred()
                            self.colorLocked = !self.colorLocked })
                .gesture( DragGesture().onChanged{
                            if !self.colorLocked { self.moodColor(data: $0, width: geo.size.width, height: geo.size.height) }}) 
                                                  // moodColor() does math to change the background
                .gesture( MagnificationGesture().onChanged{
                            self.brightness = Double($0.magnitude + 0.5)
                })

            ForEach(self.myviews) { note in
                note.text.gesture(note.drag).position(note.dragOffset) }}
    }

Solution

  • Keep your Views is Views not in Model class

    import SwiftUI
    class Note : Identifiable{
        var id = UUID()
        var dragOffset : CGSize
        var textVal: String
        init(textVal: String){
            self.textVal = textVal
            self.dragOffset = .zero
        }
    }
    
    struct Col{
        var red: Double = 1.0
        var green: Double = 1.0
        var blue: Double = 1.0
    }
    struct AnimatedText: View {
        var note: Note
        @GestureState private var location: CGPoint = .zero
        @State private var offset: CGSize = .zero
        var drag : some Gesture{ DragGesture()
            .onChanged {
                self.offset.height = self.note.dragOffset.height + $0.translation.height
                self.offset.width = self.note.dragOffset.width + $0.translation.width
        }
        .onEnded { _ in
            self.note.dragOffset = self.offset//self.offset = .zero
            }
        }
    
        var body: some View {
            Text(note.textVal)
                .font(.largeTitle)
                .offset(x: offset.width, y: offset.height)
                .gesture(drag)
        }
    }
    struct DragGestureNotMoving: View {
        @State var brightness = 0.9
        @State var color = Col()        // Color Grid Style, RGB
        @GestureState var dragInfo = CGSize.zero
        @State private var myNotes: [Note] = []
        @State private var colorLocked = false
        var body: some View {
            return GeometryReader { geo in
                Color.init( red: self.color.red * self.brightness, green: self.color.green * self.brightness, blue: self.color.blue * self.brightness)
    
                    .edgesIgnoringSafeArea(.all)
                    .gesture( TapGesture().onEnded{ value in self.myNotes.append(Note(textVal: "A Note")) })
                    .gesture( LongPressGesture(minimumDuration: 0.5).onEnded{
                        _ in UIImpactFeedbackGenerator(style: .heavy).impactOccurred()
                        self.colorLocked = !self.colorLocked })
                    //.gesture( DragGesture().onChanged{_ in if !self.colorLocked {self.moodColor(data: $0, width: geo.size.width, height: geo.size.height) }})// moodColor() does math to change the background
                    .gesture( MagnificationGesture().onChanged{
                        self.brightness = Double($0.magnitude + 0.5)
                    })
    
                ForEach(self.myNotes) { note in
                    AnimatedText(note: note)
    
                }
    
            }
        }
    }
    

    I commented the moodColor() since I don't have the extra code but the movement works with the changes. Look into SimultaneosGesture if you are having other issues.

    https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/coordinating_multiple_gesture_recognizers/allowing_the_simultaneous_recognition_of_multiple_gestures