首页 > 解决方案 > 如何在 SwiftUI 中模糊视图的一部分

问题描述

我想在 SwiftUI 中模糊相机视图的一部分。像一个模糊的框架。

我的想法是为相机视图创建一个修改器,如下面的代码中的修改器。但是,当我应用修改器时,模糊的内容content.blur(radius: 7)也会被剪裁

你能帮我理解为什么这不起作用吗?

struct MaskedCameraBlur: ViewModifier {
    // This is just the store in which the cropArea is saved
    @EnvironmentObject var dataStore : MyDataStore

    func body(content: Content) -> some View {
         ​​ZStack {
            ​​content.blur(radius: 7) // this should not be clipped 
            ​​content.clipShape(Rectangle().path(in: dataStore.cropArea))
         ​​}
    }
}
extension View {
  func maskedCameraBlur() -> some View {
    self.modifier(MaskedCameraBlur())
  }
}

编辑:

我想补充一点,我能够通过使用UIVisualEffectView. 在我的 github 中查看解决方案

但是,我仍然希望仅使用纯 SwiftUI 来实现相同的目标,如果您能指出正确的方向,我将不胜感激。谢谢你。

标签: iosswiftuiviewmodifier

解决方案


推荐阅读