[英]ScrollView position data / SwiftUi
I am a student, new to SwiftUI and still learning.我是一名学生,刚接触 SwiftUI 并且还在学习。 When I scroll the ScrollView, I want the above image to slowly increase its opacity with each step.
当我滚动 ScrollView 时,我希望上面的图像随着每一步慢慢增加它的不透明度。 I don't know how.
我不知道怎么做。 Do you have any information.
你有什么资料吗。 I could not find an example.
我找不到一个例子。 Can you do that How do we get ScrollView motion data?
你能做到吗?我们如何获取 ScrollView 运动数据?
I tried a few times but failed.我尝试了几次但失败了。
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)
}
}
}
}
}
I'm sure you can get the ScrollViews content offset by only using GeometryReader but in my opinion that's a pain and if you're allowed to for your study you should use this ScrollViewProxy package .我确定您可以仅使用 GeometryReader 来获得 ScrollViews 内容偏移量,但在我看来这很痛苦,如果您被允许进行学习,您应该使用此ScrollViewProxy package 。
I simplified your code a little to make it easier to show what I added我稍微简化了您的代码,以便更容易地显示我添加的内容
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 }
}
}
}
}
If you cannot use a package you have to either get the UIScrollView using a method like in Introspect or use a UIViewRepresentable to create your own UIScrollView.如果您不能使用 package,您必须使用Introspect中的方法获得 UIScrollView 或使用 UIViewRepresentable 创建您自己的 UIScrollView。 Then you can access the scroll views contentOffset.
然后您可以访问滚动视图 contentOffset。
Here is an example of the latter https://gist.github.com/jfuellert/67e91df63394d7c9b713419ed8e2beb7这是后者的示例https://gist.github.com/jfuellert/67e91df63394d7c9b713419ed8e2beb7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.