有没有一种方法可以模糊SwiftUI中被轻击的元素的背景?

时间:2020-05-21 01:59:19

标签: ios swift swiftui

假设我在VStack中有图像。

VStack {
    Image1
    Image2
    Image3
    Image4
}

我知道如何检测点击手势。有没有一种方法可以使当点击图像时其他图像变得模糊?

我曾考虑过将.blur(radius:radius:3)应用于VStack,然后将.blur(radius:0)应用于所选图像,但是它看起来像是模糊复合,因此无济于事。

2 个答案:

答案 0 :(得分:3)

这里是一种可能方法的演示(由于所需行为尚不完全清楚)。

通过Xcode 11.4 / iOS 13.4测试

demo

struct DemoBlurImages: View {
    let images = ["sun.max", "moon", "cloud"]

    @State private var selected: String? = nil

    var body: some View {
        VStack {
            ForEach(images, id: \.self) { name in
                Image(systemName: name).resizable()
                    .onTapGesture {
                        if self.selected == name {
                            self.selected = nil
                        } else {
                            self.selected = name
                        }
                    }
                    .blur(radius: self.selected != nil && self.selected != name ? 10 : 0)
                    .scaleEffect(self.selected == name ? 1.2 : 1)
            }
        }
        .animation(.spring())
        .scaledToFit()
        .frame(width: 100)
    }
}

答案 1 :(得分:0)

我尝试并确定是否需要。让我们来看看。 我将使用@State变量来达到您的想法。

我们可以更改State变量的状态,以控制是否需要模糊背景堆栈(VStack,HStack或ZStack)。

这是我的代码:

enter image description here

完成所有代码后,我们实现了该实现

enter image description here