【问题标题】:blurred background in swift with clear subview快速模糊的背景与清晰的子视图
【发布时间】:2017-10-31 19:06:37
【问题描述】:

我想要一个模糊的背景。我有一个主视图的子视图。我希望子视图不受模糊的影响。我该怎么办?

代码:

let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
        let blurEffectView = UIVisualEffectView(effect: blurEffect)

        blurEffectView.frame = view.bounds


 let vibrancyEffect = UIVibrancyEffect(blurEffect: blurEffect)
   let vibrancyEffectView = UIVisualEffectView(effect: vibrancyEffect)
        vibrancyEffectView.frame = view.bounds

       view.addSubview(blurEffectView)

        view.addSubview(vibrancyEffectView)


        FrameView = UIView()


        if let FrameView = FrameView {

            FrameView.layer.borderColor = UIColor.white.cgColor
            FrameView.layer.borderWidth = 2
 FrameView.frame=CGRect(x:30,y:(view.frame.height/2-50),width:view.frame.width-60,height:100)
            blurEffectView.contentView.addSubview(FrameView)


   }

所以我希望视图模糊,FrameView 清晰。

这是我的应用目前的样子。

【问题讨论】:

  • 创建一个 UIView -- 将你的 blurEffectView 添加到自定义 UIVIew 将 FrameView 添加到 mainUIVIewcontroller.view,更改视图层次结构肯定有效

标签: ios swift uiblureffect


【解决方案1】:

您必须在 hiearchy 中添加框架上部,如您所见,BlurEffectView 模糊了其中的每个子视图,因此请检查您是否将子视图添加到 viewController 的主视图并使用 func bringSubView to front...: )

【讨论】:

  • 我做到了。 view.bringSubview(toFront: FrameView) 但没有变化。我将框架视图添加到主视图。 view.addSubview(FrameView)。我什至试过 view.insertSubview(blureffectview,at: #someindex) 这也没有用
  • 您确定将子视图添加到主视图而不是模糊视图吗?
  • 是的,我将框架视图更改为主视图的子视图
【解决方案2】:

我尝试了一个测试项目,我先将模糊添加到mainView,然后我将frameView 添加到mainView,我看起来不错,frameView 不模糊。

    let mainView = UIImageView.init(frame: self.view.bounds);
    mainView.image = UIImage.init(named: "a.png");
    let blurEffect = UIBlurEffect.init(style: .dark);
    let effectView = UIVisualEffectView.init(effect: blurEffect);
    effectView.frame = mainView.bounds;
    effectView.alpha = 1;

    let frameView = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: 200, height: 50));
    frameView.image = UIImage.init(named: "b.png");
    frameView.center = self.view.center;
    //add the blur view first
    mainView.addSubview(effectView);
    self.view.addSubview(mainView);
    self.view.addSubview(frameView);

【讨论】:

  • @Savitha Suresh 检查一下
  • 我在主视图上有一个 videopreviewlayer。我该怎么做呢
  • 您希望在 FrameView 存在时播放 MainView 的 videoPlayer 吗?如果没有,你可以将 MainView 截图为 Image 并使用此功能模糊它,将其设置为 imageView 回到 FrameView ,它不会使 FrameView 模糊。
  • 是的,我希望它正在播放
  • 我测试了一个项目,对你有帮助吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-25
  • 1970-01-01
  • 2015-09-06
  • 1970-01-01
  • 2016-04-22
相关资源
最近更新 更多