Получение значения продолжительности onLongPressGesture - PullRequest
0 голосов
/ 07 апреля 2020

Я хотел бы узнать, как получить продолжительность LongPressGesture.

Моя цель - иметь возможность изменять переменную пропорционально этому LongGesture.

Я попытался ввести переменную @State, чтобы получить значение с помощью метода .onChanged / .onEnded, но этот возвращает логическое значение для LongGesture

Вот мой код (который делает не компилируется):

struct ContentView: View { 
    // Well, this var @State is a CGFloat because I think that the duration is of this type (based on the minimumDuration) 
   @State var timeLongGesture: CGFloat = 0
   @State var value: Int = 1
   var body: some View { 
   // Some stuff here
   Text("Increase the value") 
   .onTapGesture { value += 1 } 
   .gesture(
       LongPressGesture(minimumDuration: 0.4)
           .onEnded { valueLongPress in
              // Here the error, because the value of "valueLongPress" is a Bool (as the doc mentionned)
              timeLongGesture = valueLongPress
            })

Я чувствую, что это сложно. Так что, если у кого-то есть идеи, я возьму их :-) Спасибо за вашу помощь.

1 Ответ

0 голосов
/ 08 апреля 2020

Вот как я мог бы получить продолжительность LongPressGesture:

class InitialTime: ObservableObject {
    @Published var initTime = Date()
}

struct ContentView: View {

@ObservedObject var kickoff = InitialTime()

@State var timeLongGesture: Double = 0.0
@State var value: Int = 1
@GestureState var isDetectingLongPress = false

var body: some View {
    // Some stuff here
    Text("Increase the value")
        .onTapGesture { self.value += 1 }
        .gesture(LongPressGesture(minimumDuration: 3)
            .updating($isDetectingLongPress) { _, _, _ in
                self.kickoff.initTime = Date()
                print("--------> self.kickoff.initTime: \(self.kickoff.initTime)")
        }
        .onEnded { finished in
            self.timeLongGesture = Date().timeIntervalSince(self.kickoff.initTime)
            print("-----> timeLongGesture: \(self.timeLongGesture)")
        })
}
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...