1

这个 gif 应该很清楚地显示我的问题。

在此处输入图像描述

换句话说,我有一个背景颜色的 DragGesture 和 TapGesture 来添加 Text() 视图。每个文本视图上都有一个 DragGesture 以在拖动时更新其位置。这可行,但在我激活背景颜色 DragGesture 之前,文本视图位置的视觉效果不会更新。我已经确认文本视图位置值会更新,它们只是不会在视觉上更新。

另外,我有一个长按手势,通过阻止调用 moodColor() 来“锁定”背景颜色。当它被锁定时,文本视图不会移动,直到我再次长按解锁它,然后拖动背景颜色。

我有一种误用 DragGestures 的感觉,但似乎每个视图都应该有自己的手势。

这是代码。

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

1 回答 1

-1

保持你不在模型ViewsViewsclass

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)

            }

        }
    }
}

我评论了,moodColor()因为我没有额外的代码,但运动与变化一起工作。如果您有其他问题,请查看 SimultaneosGesture。

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

于 2020-02-08T22:30:51.673 回答