SwiftUI-我的混合图像视图在iPhone上失败。在预览中工作

时间:2019-11-22 19:16:16

标签: swiftui

我有一个由两个图像组成的背景视图。它们被遮罩以创建透明的渐变以在它们之间交叉淡入淡出。当我在不使用遮罩的情况下运行混合时,它可以在iPhone上运行,但是当我在使用遮罩的情况下进行尝试时,仅显示顶层。我的代码如下:

struct BackgroundImageForBlending: View {
    enum imageSide {
        case left, right
    }
    var side: imageSide

    var imageName: String

    func selectImage() -> some View {
        switch side {
        case .left:
            return Image(imageName)
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: UIScreen.main.bounds.width * 0.75)
                .clipped()
                .mask(Rectangle()
                    .foregroundColor(.clear)
                    .background(LinearGradient(gradient: Gradient(colors: [.clear, .white, .white]), startPoint: .trailing, endPoint: .leading)))
                .padding(.trailing, UIScreen.main.bounds.width * 0.25)
        case .right:
            return Image(imageName)
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: UIScreen.main.bounds.width * 0.75)
                .clipped()
                .mask(Rectangle()
                    .foregroundColor(.clear)
                    .background(LinearGradient(gradient: Gradient(colors: [.clear, .white, .white]), startPoint: .leading, endPoint: .trailing)))
                .padding(.leading, UIScreen.main.bounds.width * 0.25)
        }
    }

    var body: some View {
        selectImage()
    }
}


struct BlendedRaceClassBackgroundImage: View {
    var champion: Champion

    func findRaceImage() -> String {
        return champion.race.race.map {$0.race.lowercased()} ?? ""
    }

    func findClassImage() -> String {
        champion.characterClass.characterClass.map {"\($0.name.lowercased())_\(champion.gender!.rawValue)"} ?? ""
    }

    var body: some View {
        ZStack {
            BackgroundImageForBlending(side: .left, imageName: findRaceImage())
            BackgroundImageForBlending(side: .right, imageName: findClassImage())
                .blendMode(.multiply)
        }.edgesIgnoringSafeArea(.vertical)
    }
}

view in preview view on iPhone

1 个答案:

答案 0 :(得分:1)

好。我解决了自己的问题。造成问题的不是面具。 iPhone处于黑暗模式。黑色背景导致了问题。我在blendedImage后面的ZStack中添加了一个Rectangle,一切正常。