【问题标题】:ScrollView position data / SwiftUiScrollView 位置数据 / SwiftUi
【发布时间】:2021-01-01 08:39:25
【问题描述】:

我是一名学生,刚接触 SwiftUI,还在学习。当我滚动 ScrollView 时,我希望上面的图像随着每一步慢慢增加它的不透明度。我不知道怎么做。你有什么资料吗。我找不到一个例子。你能做到吗?我们如何获取 ScrollView 运动数据?

我试了几次都失败了。

struct Home: View {
    var body: some View {
        TabView{
            NavigationView{
                ZStack {
                    GeometryReader{ geometry in
                        Color.black.edgesIgnoringSafeArea(.all)
                        VStack{//Ana sayfa image animation bölümü
                            Spacer().frame(height:geometry.size.height / 7)
                            Image("HandP")
                                .resizable()
                                .aspectRatio(ContentMode.fit)
                                .frame(width:geometry.size.width,height:geometry.size.height / 3)
                        }
                        ScrollView(.vertical){
                            VStack(spacing: 20) {
                                Spacer().frame(height:geometry.size.height / 2)//ScrollView üst boşluk
                                Button(action: {
                                    //some action
                                }) {
                                    Text("önceki")
                                }
                                
                                HStack(spacing: 10) {
                                    NavigationLink(destination: SendCoffeeFortune()){
                                        Text("Kahve Falı")
                                    }
                                    .navigationBarTitle(Text("Geri"),displayMode: .inline)
                                    .navigationBarHidden(true)
                                    
                                    Button(action: {
                                        
                                    }) {
                                        Text("Astroloji")
                                    }
                                }
                                
                                HStack(spacing: 10) {
                                    NavigationLink(destination: SpecialCoffeeFortune()){
                                        Text("Özel Fal")
                                    }
                                    .navigationBarTitle(Text("Geri"),displayMode: .inline)
                                    .navigationBarHidden(true)
                                    
                                    Button(action: {
                                        
                                    }) {
                                        Text("Tarot")
                                    }
                                }
                                
                                HStack(spacing: 10) {
                                    Button(action: {
                                        //some action
                                    }) {
                                        Text("Uyum")
                                    }
                                    
                                    NavigationLink(destination: SendHandFortune()){
                                        Text("El Falı")
                                    }
                                    .navigationBarTitle(Text("Geri"),displayMode: .inline)
                                    .navigationBarHidden(true)
                                }
                            }
                            .padding()
                            .padding()
                        }
                        
                    }
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
                    
                }
            }
        }
    }
}

【问题讨论】:

  • 请不要粘贴代码墙。将其剥离到您需要证明您的问题的最低限度。它增加了能够帮助您解决问题的可能性。你还需要一个 ScrollViewReader hackingwithswift.com/quick-start/swiftui/…
  • “每一步”滚动是什么意思?
  • 我希望图片向上滑动时不透明。图片在每一步都会变成 1 不透明,所以。 @Asperi
  • 有问题的图片将被留下。当我滚动时,当页面在图像上滚动时,图像应该慢慢变得不透明。@Asperi - @Warren Burton

标签: ios swift swiftui


【解决方案1】:

我确信您可以仅使用 GeometryReader 来抵消 ScrollViews 内容,但我认为这很痛苦,如果您被允许进行学习,您应该使用 ScrollViewProxy package

我稍微简化了你的代码,以便更容易地显示我添加的内容

import ScrollViewProxy

struct Home: View {
    @State var offset: CGPoint = .zero

    var body: some View {
        VStack {
            Image(...)
                .opacity(min(1, offset.y/100)) // You should change this to calculate when you want the opacity to change
            ScrollView { proxy in 
                Color.clear
                    .frame(height: 1000)
                    .onReceive(proxy.offset) { self.offset = $0 }
            }
        }
    }
}

如果您不能使用包,您必须使用Introspect 中的方法获取 UIScrollView,或者使用 UIViewRepresentable 创建您自己的 UIScrollView。然后您可以访问滚动视图 contentOffset。

这是后者https://gist.github.com/jfuellert/67e91df63394d7c9b713419ed8e2beb7的示例

【讨论】:

  • 你有图书馆的链接吗?我无法导入。 @Casper Zandbergen
  • @metaflor22 它在答案中链接,使用 swift 包管理器添加它。 (复制链接,然后在 Xcode 中;File > Swift Package Manager > add package 或类似的东西)
  • 我添加了代码,但它给出了错误。我导入了 ScrollViewProxy。我在上面附上了你的照片。 @Casper Zandbergen
  • @metaflor22 你得说得更具体一点
  • 啊,按 CMD+A 和 CTRL+I 修复缩进,应该有助于识别问题
猜你喜欢
  • 2021-08-01
  • 1970-01-01
  • 2019-10-20
  • 2021-04-08
  • 2020-01-15
  • 2022-12-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多