在新 iOS 的音乐应用程序中,我们可以在模糊的视图后面看到专辑封面。
这样的事情怎么可能完成?我已经阅读了文档,但在那里没有找到任何东西。
你可以使用UIVisualEffectView来达到这个效果。这是一个本机 API,已针对性能和出色的电池寿命进行了微调,而且易于实施。
UIVisualEffectView
迅速:
//only apply the blur if the user hasn't disabled transparency effects if !UIAccessibility.isReduceTransparencyEnabled { view.backgroundColor = .clear let blurEffect = UIBlurEffect(style: .dark) let blurEffectView = UIVisualEffectView(effect: blurEffect) //always fill the view blurEffectView.frame = self.view.bounds blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] view.addSubview(blurEffectView) //if you have more UIViews, use an insertSubview API to place it where needed } else { view.backgroundColor = .black }
目标-C:
//only apply the blur if the user hasn't disabled transparency effects if (!UIAccessibilityIsReduceTransparencyEnabled()) { self.view.backgroundColor = [UIColor clearColor]; UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]; UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect]; //always fill the view blurEffectView.frame = self.view.bounds; blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [self.view addSubview:blurEffectView]; //if you have more UIViews, use an insertSubview API to place it where needed } else { self.view.backgroundColor = [UIColor blackColor]; }
如果您以模态方式呈现此视图控制器以模糊底层内容,则需要将模态呈现样式设置为 Over Current Context 并将背景颜色设置为清除,以确保底层视图控制器在其呈现后仍保持可见。