通过实时预览 (SwiftUI) 执行操作/使用手势
Performing actions / working with gestures via Live Preview (SwiftUI)
我有 CustomToggle
,我想通过 Live Preview
测试它。问题是通过 Live Preview
轻敲切换没有任何作用,但在 运行 Simulator
.
时效果很好
struct CustomToggle: View {
@Binding var isOn: Bool
// ... displaying CustomToggle view
// ... and toggling isON onTapGesture
}
struct CustomToggle_Previews: PreviewProvider {
@State static var isOn = true
static var previews: some View {
CustomToggle(isOn: $isOn)
}
}
您可以为此定义一个结构:
struct StatefulPreviewWrapper<Value, Content: View>: View {
@State var value: Value
var content: (Binding<Value>) -> Content
var body: some View {
content($value)
}
init(_ value: Value, content: @escaping (Binding<Value>) -> Content) {
self._value = State(wrappedValue: value)
self.content = content
}
}
然后像这样在预览中使用它:
struct CustomToggle_Previews: PreviewProvider {
static var previews: some View {
StatefulPreviewWrapper(false) { CustomToggle(isOn: [=11=]) }
}
}
我有 CustomToggle
,我想通过 Live Preview
测试它。问题是通过 Live Preview
轻敲切换没有任何作用,但在 运行 Simulator
.
struct CustomToggle: View {
@Binding var isOn: Bool
// ... displaying CustomToggle view
// ... and toggling isON onTapGesture
}
struct CustomToggle_Previews: PreviewProvider {
@State static var isOn = true
static var previews: some View {
CustomToggle(isOn: $isOn)
}
}
您可以为此定义一个结构:
struct StatefulPreviewWrapper<Value, Content: View>: View {
@State var value: Value
var content: (Binding<Value>) -> Content
var body: some View {
content($value)
}
init(_ value: Value, content: @escaping (Binding<Value>) -> Content) {
self._value = State(wrappedValue: value)
self.content = content
}
}
然后像这样在预览中使用它:
struct CustomToggle_Previews: PreviewProvider {
static var previews: some View {
StatefulPreviewWrapper(false) { CustomToggle(isOn: [=11=]) }
}
}