Created
April 12, 2025 10:59
-
-
Save Koshimizu-Takehito/0ca2b9bd7fa1d68057d459952043f87f to your computer and use it in GitHub Desktop.
visualEffectモディファイア
This file contains hidden or 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 { | |
var body: some View { | |
GeometryReader { geometry in | |
let scrollViewFrame = geometry.frame(in: .local) | |
ScrollView { | |
ForEach(0..<1000) { offset in | |
RowContent(offset: offset, scrollViewFrame: scrollViewFrame) | |
} | |
.padding(.horizontal) | |
} | |
} | |
} | |
} | |
private struct RowContent: View { | |
let offset: Int | |
let scrollViewFrame: CGRect | |
@State var zIndex: Double = 0 | |
var body: some View { | |
RoundedRectangle(cornerRadius: 24) | |
.fill(.blue) | |
.frame(height: 100.0) | |
.onGeometryChange(for: CGRect.self) { $0.frame(in: .scrollView) } action: { newValue in | |
zIndex = min(newValue.minY, min(scrollViewFrame.midY - newValue.midY, 0)) | |
} | |
.zIndex(zIndex * Double(offset)) | |
.visualEffect { content, proxy in | |
let frame = proxy.frame(in: .scrollView(axis: .vertical)) | |
let distance1 = frame.minY | |
let distance2 = scrollViewFrame.maxY - frame.maxY | |
let distance = min(distance1, min(distance2, 0)) | |
return content | |
.hueRotation(.degrees(frame.origin.y / 5)) | |
.scaleEffect(max(1 + distance / 1000, 0)) | |
.offset(y: distance1 < 0 ? -distance : distance) | |
.brightness(distance1 < 0 ? -distance / 500 : -distance / 200) | |
} | |
} | |
} | |
#Preview { | |
ContentView() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for share,
a couple of additional examples, just for the top edge and bottom edge