滑块在SwiftUI中的选项卡视图上似乎不滑动
如何使滑块正常滑动?现在他们被锁定了 我认为问题是因为视图的绑定,其中的滑块在其中,但是这些绑定与滑块无关,并且这些滑块对于工作状态有自己的价值!但我不知道为什么会有这样的问题。我尝试在没有绑定的选项卡视图中使用滑块来查看它的工作情况,但没有绑定 我的代码:滑块在SwiftUI中的选项卡视图上似乎不滑动,swiftui,Swiftui,如何使滑块正常滑动?现在他们被锁定了 我认为问题是因为视图的绑定,其中的滑块在其中,但是这些绑定与滑块无关,并且这些滑块对于工作状态有自己的价值!但我不知道为什么会有这样的问题。我尝试在没有绑定的选项卡视图中使用滑块来查看它的工作情况,但没有绑定 我的代码: import SwiftUI struct ContentView: View { @State var currentPage: Int = 1 @State var animationIsEnable
import SwiftUI
struct ContentView: View {
@State var currentPage: Int = 1
@State var animationIsEnable : Bool = true
@State var shadowIsEnable : Bool = true
var body: some View {
ZStack
{
Color.gray.ignoresSafeArea()
TabView(selection: $currentPage)
{
Text("Hi").tag(0)
SliderView(animationIsEnable: $animationIsEnable, shadowIsEnable: $shadowIsEnable).tag(1)
}
.indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .never))
.tabViewStyle(PageTabViewStyle())
}
}
}
struct SliderView: View {
@Binding var animationIsEnable : Bool
@Binding var shadowIsEnable : Bool
@State var volum1: Float = 50
@State var volum2: Float = 50
var body: some View {
VStack {
HStack { Text("03:58"); Slider(value: $volum1, in: 0...100, step: 1); Text("23:51") }.padding(.vertical)
HStack {
Button(action: {print("speaker.1")}) { Image(systemName: "speaker.1").font(Font.body.bold()) }
Slider(value: $volum2, in: 0...100, step: 1)
Button(action: {print("speaker.3")}) { Image(systemName: "speaker.3").font(Font.body.bold()) }
}.padding(.vertical)
}
.padding(.horizontal)
.foregroundColor(Color.black)
.shadow(color: shadowIsEnable ? Color.black.opacity(0.7) : Color.clear, radius: 10, x: -8, y: 8)
}
}
您的问题在
.shadow
中
解决方案是使用合成组。使用Xcode 12.1/iOS 14.1进行测试
.foregroundColor(Color.black)
.compositingGroup() // << here !!
.shadow(color: shadowIsEnable ? Color.black.opacity(0.7) : Color.clear, radius: 10, x: -8, y: 8)
.foregroundColor(颜色.黑色)
.compositingGroup()//有另一种不用.compositingGroup()解决问题的方法。只需将.shadow移动到ZStack下的ContentView即可