Swift 需要帮助将customEditMode设置为环境吗
我喜欢重新构建一个自定义的编辑模式,就像SwiftUI中相同的编辑模式一样,用于学习目的,我可以尽可能地编写我的代码,直到出现完整的错误代码,这不是我的计划!然而,这里是我一直尝试到现在,需要帮助,使这个代码的工作。谢谢 更新: 为什么这个圆圈的颜色不变Swift 需要帮助将customEditMode设置为环境吗,swift,swiftui,Swift,Swiftui,我喜欢重新构建一个自定义的编辑模式,就像SwiftUI中相同的编辑模式一样,用于学习目的,我可以尽可能地编写我的代码,直到出现完整的错误代码,这不是我的计划!然而,这里是我一直尝试到现在,需要帮助,使这个代码的工作。谢谢 更新: 为什么这个圆圈的颜色不变 struct ContentView: View { @Environment(\.customEditMode) var customEditMode var body: some View {
struct ContentView: View {
@Environment(\.customEditMode) var customEditMode
var body: some View {
CircleView()
VStack {
Button("active") { customEditMode?.wrappedValue = CustomEditMode.active }.padding()
Button("inactive") { customEditMode?.wrappedValue = CustomEditMode.inactive }.padding()
Button("none") { customEditMode?.wrappedValue = CustomEditMode.none }.padding()
}
.onChange(of: customEditMode?.wrappedValue) { newValue in
if newValue == CustomEditMode.active {
print("customEditMode is active!")
}
else if newValue == CustomEditMode.inactive {
print("customEditMode is inactive!")
}
else if newValue == CustomEditMode.none {
print("customEditMode is none!")
}
}
}
}
struct CircleView: View {
@Environment(\.customEditMode) var customEditMode
var body: some View {
Circle()
.fill(customEditMode?.wrappedValue == CustomEditMode.active ? Color.green : Color.red)
.frame(width: 150, height: 150, alignment: .center)
}
}
如果仔细查看
编辑模式
:
@available(macOS, unavailable)
@available(watchOS, unavailable)
public var editMode: Binding<EditMode>?
下面是一个演示:
struct ContentView: View {
@State private var customEditMode = CustomEditMode.none
var body: some View {
TestView()
.environment(\.customEditMode, $customEditMode)
}
}
struct TestView: View {
@Environment(\.customEditMode) var customEditMode
var body: some View {
Circle()
.fill(customEditMode?.wrappedValue == .active ? Color.green : Color.red)
.frame(width: 150, height: 150, alignment: .center)
VStack {
Button("active") { customEditMode?.wrappedValue = .active }.padding()
Button("inactive") { customEditMode?.wrappedValue = .inactive }.padding()
Button("none") { customEditMode?.wrappedValue = .none }.padding()
}
.onChange(of: customEditMode?.wrappedValue) { newValue in
print(String(describing: newValue))
}
}
}
实际上,我想在CustomEditMode中讨论均衡问题,这就是为什么我使用Bool?在我的枚举中,有您的答案,我还有一个问题,我应该在哪里以及如何将equalable协议导入CustomEditMode?谢谢,所以帖子得到了更新。@swiftPunk您需要在
onChange
中使用customEditMode?.wrappedValue
。我更新了我的答案,并添加了一个演示。我就是不能围绕这个话题来思考!越来越难了!您刚才在演示中编写代码的方式是一种环境对象,您正在为其提供辅助视图,就像您为TestView所做的那样,尝试使用和定制一个类似customEditMode的环境的所有要点是摆脱辅助视图,例如,我们不提供colorScheme或ScenePase,我们只是访问它们,我在你代码的帮助下更新了我的代码,但仍然没有达到我希望的效果,请查看我的代码,我知道这是一个糟糕的编码情况,但我喜欢学习和理解它。@swiftPunk我们不提供配色方案或场景库-但我们也不会更改它们。如果不将值注入环境,则该值不会更改的原因是我使用了常量绑定:static let defaultValue:binding?=。常数(.none)
。相反,它可以是static let defaultValue:Binding?=。init(get:{customEditMode},set:{customEditMode=$0})
withstatic private var customEditMode=customEditMode.none
。但这不会刷新视图,因为绑定保持不变-为此需要状态。
struct ContentView: View {
@State private var customEditMode = CustomEditMode.none
var body: some View {
TestView()
.environment(\.customEditMode, $customEditMode)
}
}
struct TestView: View {
@Environment(\.customEditMode) var customEditMode
var body: some View {
Circle()
.fill(customEditMode?.wrappedValue == .active ? Color.green : Color.red)
.frame(width: 150, height: 150, alignment: .center)
VStack {
Button("active") { customEditMode?.wrappedValue = .active }.padding()
Button("inactive") { customEditMode?.wrappedValue = .inactive }.padding()
Button("none") { customEditMode?.wrappedValue = .none }.padding()
}
.onChange(of: customEditMode?.wrappedValue) { newValue in
print(String(describing: newValue))
}
}
}