简体   繁体   English

SwiftUI:滑块位于导航栏项目的前导/尾随时的奇怪行为

[英]SwiftUI: Weird behaviour of Slider when it is in the leading/trailing of navigation bar items

I'm having trouble placing a Slider as a view in the trailing of navigation bar.我无法将 Slider 作为视图放置在导航栏的尾部。 Since it's hooked to a state, changing the state of it is very jumpy and not smooth at all.由于它与一个状态挂钩,因此更改它的状态非常不稳定,一点也不流畅。

Here's my sample code:这是我的示例代码:

struct ContentView: View {
    @State var opacityValue: Double = 1
    
    var body: some View {
        NavigationView {
            GeometryReader { geometry in
                VStack {
                    Slider(value: self.$opacityValue, in: 0...100, step: 5) { changed in
                        
                    }
                    Image(systemName: "photo.fill")
                        .foregroundColor(.blue)
                        .padding()
                        .opacity(opacityValue / 100)

                }.navigationBarItems(trailing: HStack(spacing: 20) {
                    Slider(value: self.$opacityValue, in: 0...100, step: 5) { _ in
                        
                    }.frame(width: 100)
                }).frame(width: geometry.size.width / 2)
            }
        }
    }
}

The slider that I have in the regular VStack changes the opacity very smooth (and it even moves the slider that is in the nav bar.) But on the other hand, moving the slider that is in the nav bar is very glitchy/jumpy.我在常规 VStack 中的滑块非常平滑地改变了不透明度(它甚至移动了导航栏中的滑块。)但另一方面,移动导航栏中的滑块非常有问题/跳跃。

Is there a way to make this work?有没有办法使这项工作?

You can make new View model for Image with @Binding property and pass your opacity through it, which will stop glitching movement on slider when @State property changes and View have to be refreshed您可以使用@Binding属性为 Image 创建新的 View 模型并通过它传递不透明度,这@State属性更改且 View 必须刷新时停止滑块上的@State移动

struct ContentView: View {
    @State var opacityValue: Double = 1
    var body: some View {
        NavigationView {
            GeometryReader { geometry in
                VStack {
                    
                    Slider(value: self.$opacityValue, in: 0...100, step: 5) { _ in
            
                    }
                    
                    ImageOpacityView(opacity: $opacityValue)

                }.navigationBarItems(trailing: HStack(spacing: 20) {
                    
                    Slider(value: self.$opacityValue, in: 0...100, step: 5) { _ in
                        
                    }.frame(width: 100)
                    
                }).frame(width: geometry.size.width / 2)
            }
        }
    }
    
    struct ImageOpacityView: View {
        @Binding var opacity: Double
        
        var body: some View {
            Image(systemName: "photo.fill")
                .foregroundColor(.blue)
                .padding()
                .opacity(opacity / 100)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM