简体   繁体   English

SwiftUI DragGestures 在触发不同的 DragGesture 之前不会更新

[英]SwiftUI DragGestures not updating until different DragGesture triggered

this gif should pretty clearly show my issue.这个 gif 应该很清楚地显示了我的问题。

在此处输入图片说明

In words, I have a DragGesture for the background color, and TapGesture to add Text() views.换句话说,我有一个用于背景颜色的 DragGesture,还有一个用于添加 Text() 视图的 TapGesture。 Each Text View has a DragGesture on it to update its location as its dragged.每个文本视图上都有一个 DragGesture 以在拖动时更新其位置。 This works, but the visually the Text Views position don't update until I activate the background color DragGesture.这有效,但在我激活背景颜色 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.我有一种感觉我在滥用 DragGestures,但很明显每个视图都应该有它自己的手势。

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) }}
    }

Keep your Views is Views not in Model class保持您的Views是不在模型class Views

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.我评论了moodColor()因为我没有额外的代码,但运动会随着变化而工作。 Look into SimultaneosGesture if you are having other issues.如果您遇到其他问题,请查看 SimultaneosGesture。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM