Created
December 30, 2023 05:49
-
-
Save 1mash0/9999712df79130bceec7d3e27b469e8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct ContentView: View { | |
@State private var initOffset: CGFloat = .zero | |
@State private var offset: CGFloat = .zero | |
@State private var closeButtonYPosition: CGFloat = .zero | |
let headerHeight: CGFloat = 300 | |
var body: some View { | |
ZStack(alignment: .topTrailing) { | |
ScrollView { | |
ZStack { | |
VStack(spacing: 0) { | |
ForEach(0..<50, id: \.self) { i in | |
Text("item \(i)") | |
.frame(maxWidth: .infinity, minHeight: 44) | |
.background(.white) | |
} | |
} | |
.padding(.top, headerHeight) | |
GeometryReader { gr in | |
Color.cyan | |
.frame(height: headerHeight) | |
.overlay { | |
VStack { | |
Text("Header") | |
} | |
} | |
.onAppear { | |
initOffset = gr.frame(in: .global).origin.y | |
} | |
.offset(y: gr.frame(in: .global).origin.y < initOffset | |
? 0 | |
: initOffset - gr.frame(in: .global).origin.y) | |
.onChange(of: gr.frame(in: .global).origin.y, initial: true) { _, newValue in | |
offset = gr.frame(in: .global).minY | |
} | |
} | |
} | |
} | |
.background(.gray) | |
GeometryReader { gr in | |
HStack { | |
Spacer() | |
Button { | |
print("close") | |
} label: { | |
Image(systemName: "xmark.circle.fill") | |
.resizable() | |
.frame(width: 34, height: 34) | |
.foregroundStyle(headerHeight + offset - initOffset + 17 < closeButtonYPosition ? .green : .red) | |
.background(.white) | |
.clipShape(Circle()) | |
} | |
.frame(width: 44, height: 44) | |
.padding() | |
.onAppear { | |
closeButtonYPosition = gr.frame(in: .global).origin.y | |
} | |
} | |
} | |
} | |
} | |
} | |
#Preview { | |
ContentView() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment