SwiftUI 如何从底部边缘淡出图像

Jus*_*ion 4 swiftui swiftui-layout

基本上我想要实现的是: 在此输入图像描述

从上到下淡出图像。我尝试用叠加来做到这一点,但它看起来不太好

gee*_*sg7 5

这是一个从上到下淡出图像的工作视图。如果有效的话,LMK!

源代码

struct ContentView: View {
    var body: some View {
        VStack {
            Image("Explore")//Your Image
                .resizable()
        }
        //We can use the LinearGradient in the mask modifier to fade it top to bottom
        .mask(LinearGradient(gradient: Gradient(stops: [
            .init(color: .black, location: 0),
            .init(color: .clear, location: 1),
            .init(color: .black, location: 1),
            .init(color: .clear, location: 1)
        ]), startPoint: .top, endPoint: .bottom))
        .padding()
        .frame(width: 400, height: 400)
    }
}
Run Code Online (Sandbox Code Playgroud)

预览

在此输入图像描述