I want when I scroll up the Text ("Hello") will stay fixed, and the text "Welcome " will move up to be on the same line as Text("Hello").
Hope you can give me some solution.Thanks
struct ContentView: View {
@State var didScrollUp: Bool = false
@State var Ishstacked: Bool
var body: some View {
NavigationView {
ScrollView {
ZStack {
VStack(alignment:.leading) {
VStack {
TagLineView(isHStacked: $Ishstacked)
}
.padding()
ZStack(alignment:.leading) {
Color(#colorLiteral(red: 0.937254902, green: 0.937254902, blue: 0.937254902, alpha: 1))
.edgesIgnoringSafeArea(.all)
.cornerRadius(30.0)
VStack(alignment: .leading) {
Text("Popular")
.font(.custom("PlayfairDisplay-Bold", size: 24))
.padding(.leading,27)
.padding(.top,20)
ScrollView (.horizontal, showsIndicators: false) {
HStack (spacing: 0) {
ForEach(0 ..< 4) { i in
ProductCardView(image: Image("chair_\(4-i)"), size: 180)
}
.padding(.leading)
}
}
.padding(.bottom)
Text("Best")
.font(.custom("PlayfairDisplay-Bold", size: 24))
.padding(.horizontal)
ScrollView (.horizontal, showsIndicators: false) {
HStack (spacing: 0) {
ForEach(0 ..< 4) { i in
ProductCardView(image: Image("chair_\(4-i)"), size: 180)
}
.padding(.leading)
}
}
Text("Best")
.font(.custom("PlayfairDisplay-Bold", size: 24))
.padding(.horizontal)
ScrollView (.horizontal, showsIndicators: false) {
HStack (spacing: 0) {
ForEach(0 ..< 4) { i in
ProductCardView(image: Image("chair_\(4-i)"), size: 180)
}
.padding(.leading)
}
}
}
}.background(GeometryReader {
Color.clear.preference(key: ViewOffsetKey.self,
value: -$0.frame(in: .named("scroll")).origin.y)
})
.onPreferenceChange(ViewOffsetKey.self) { didScrollUp = $0 > 5 ? true : false }
}
}
.coordinateSpace(name: "scroll")
}
}
}
}
struct TagLineView: View {
@Binding var isHStacked: Bool
var body: some View {
Text("Hello")
.font(.title)
.fontWeight(.bold) + Text(isHStacked ? " " : "\n") + // Adding a newline or adding a space based on the orientation of the stack
Text("Welcome!")
.font(.title)
}
}
struct ViewOffsetKey: PreferenceKey {
typealias Value = CGFloat
static var defaultValue = CGFloat.zero
static func reduce(value: inout Value, nextValue: () -> Value) {
value += nextValue()
}
}
I want when I scroll up the Text ("Hello") will stay fixed, and the text "Welcome " will move up to be on the same line as Text("Hello").