gpt4 book ai didi

events - 如何使用 SwiftUI 和 Combine 检测 Datepicker 的值变化?

转载 作者:行者123 更新时间:2023-12-03 16:54:57 29 4
gpt4 key购买 nike

在使用 SwiftUI 和 Combine 时,您将如何检测 Datepicker 值的变化?
每当移动日期选择器轮时,我都需要调用一个方法来更新文本和 slider 。

我已经寻找特定方法来识别值更改(使用 UIKit 可以将操作与事件相关联),但显然我在文档中没有发现任何有用的东西(我尝试过 onTapGesture 方法,但那不是我想要什么,因为它强制用户点击选择器来更新其他 View ,而我希望在用户移动滚轮时自动更新)。

import SwiftUI

struct ContentView: View {

private var calendar = Calendar.current

@State private var date = Date()
@State private var weekOfYear = Double(Calendar.current.component(.weekOfYear, from: Date()) )
@State private var lastWeekOfThisYear = 53.0
@State private var weekDay: String = { () -> String in
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE"
let weekDay = dateFormatter.string(from: Date())
return weekDay
}()

var body: some View {

VStack {

// Date Picker
DatePicker(selection: $date, displayedComponents: .date, label:{ Text("Please enter a date") }
)
.labelsHidden()
.datePickerStyle(WheelDatePickerStyle())
.onTapGesture {
self.updateWeekAndDayFromDate()
}

// Week number and day
Text("Week \(Int(weekOfYear.rounded()))")
Text("\(weekDay)")

// Slider
Slider(value: $weekOfYear, in: 1...lastWeekOfThisYear, onEditingChanged: { _ in
self.updateDateFromWeek()
})
}

}

func updateWeekAndDayFromDate() {
// To do
}

func updateDateFromWeek() {
// To do
}

func setToday() {
// To do
}

func getWeekDay(_ date: Date) -> String {
//To do
}
}

我想这可以使用Combine(observableobject,published,sink等)解决,但我还没有使用Combine的经验,因此我想寻求一些帮助......有什么想法吗? :)

非常感谢!

最佳答案

请在下面找到一种可能的方法(稍微修改您的代码)。这是一个让源状态透明以在其中激活依赖的想法。希望它会有所帮助。

这是演示

dependent binding of DatePicker

这是代码

struct TestDatePicker: View {

private static func weekOfYear(for date: Date) -> Double {
Double(Calendar.current.component(.weekOfYear, from: date))
}

private static func weekDay(for date: Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "EEEE"
let weekDay = dateFormatter.string(from: date)
return weekDay
}

@State private var date: Date
@State private var weekOfYear: Double
@State private var weekDay: String
@State private var lastWeekOfThisYear = 53.0

private var dateProxy:Binding<Date> {
Binding<Date>(get: {self.date }, set: {
self.date = $0
self.updateWeekAndDayFromDate()
})
}

init() {
let now = Date()
self._date = State<Date>(initialValue: now)
self._weekOfYear = State<Double>(initialValue: Self.weekOfYear(for: now))
self._weekDay = State<String>(initialValue: Self.weekDay(for: now))
}

var body: some View {

VStack {

// Date Picker
DatePicker(selection: dateProxy, displayedComponents: .date, label:{ Text("Please enter a date") }
)
.labelsHidden()
.datePickerStyle(WheelDatePickerStyle())

// Week number and day
Text("Week \(Int(weekOfYear.rounded()))")
Text("\(weekDay)")

// Slider
Slider(value: $weekOfYear, in: 1...lastWeekOfThisYear, onEditingChanged: { _ in
self.updateDateFromWeek()
})
}

}

func updateWeekAndDayFromDate() {
self.weekOfYear = Self.weekOfYear(for: self.date)
self.weekDay = Self.weekDay(for: self.date)
}

func updateDateFromWeek() {
// To do
}

func setToday() {
// To do
}

func getWeekDay(_ date: Date) -> String {
""
}
}

struct TestDatePicker_Previews: PreviewProvider {
static var previews: some View {
TestDatePicker()
}
}

关于events - 如何使用 SwiftUI 和 Combine 检测 Datepicker 的值变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58603932/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com