iOS 8 UIVisualEffect 视图与 UIBlurEffect 变得不透明

iOS 8 UIVisualEffect View with UIBlurEffect becomes opaque

我正在尝试在现有视图上呈现具有模糊透明背景的视图。我可以在演示动画期间获得所需的效果,但是一旦模糊视图完全呈现,它就会变得不透明。

以下是我的介绍方式:

if (!UIAccessibilityIsReduceTransparencyEnabled()) {
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    blurEffectView.frame = self.view.bounds;
    [blurEffectView setOpaque:NO];
    [self.view insertSubview:blurEffectView atIndex:0];
    [blurEffectView setTranslatesAutoresizingMaskIntoConstraints:false];
}

在我的模糊视图中,我将背景色设置为透明色,将不透明设置为否。有什么想法吗?

我最终编写了一个自定义表示控制器。从未找到使用标准演示文稿执行此操作的方法。

在我想要效果的视图viewDidLoad:

//Custom Modal Presentation
[self setModalPresentationStyle:UIModalPresentationCustom];

//blur the controlContainerView
if (!UIAccessibilityIsReduceTransparencyEnabled()) {

    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:[self blurStyle]];
    UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    blurEffectView.frame = self.controlContainerView.bounds;
    [blurEffectView setOpaque:NO];
    [self.controlContainerView insertSubview:blurEffectView atIndex:0];
    [blurEffectView setTranslatesAutoresizingMaskIntoConstraints:false];
}

调用上面的视图:

//in .h
@property (nonatomic) id<UIViewControllerTransitioningDelegate> transitioningDelegate;

//In .m
BlurViewController *blurVC = [self.storyboard instantiateViewControllerWithIdentifier:@"blurView"];
_transitioningDelegate = [[MyCustomOverlayDelegate alloc] init];
[blurView setModalPresentationStyle:UIModalPresentationCustom];
[blurView setTransitioningDelegate:[self transitioningDelegate]];
[blurView setBlurStyle:UIBlurEffectStyleLight];
[self presentViewController: blurView animated:YES completion:nil];

您还需要导入整个演示控制器堆栈 类。对我来说,它们是 OverlayDelegate、OverlayTransitionAnimator 和 OverlayPresentationController。这才是真正的工作所在。

如果可以,请查看 WWDC 2014 session 228 "A look inside presentation controllers"。自定义演示文稿涵盖了大约 half-way 到。这就是我用来建立自定义堆栈的基础并且相当简单。

来自文档:

A UIBlurEffect object applies a blurring effect to the content layered behind a UIVisualEffectView.

所以我认为问题出在您清晰的背景颜色上。如果您的 UIVisualEffectView 背后有某种内容或颜色,它应该可以工作。

UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
UIVisualEffectView *blurredView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];

这个简单的代码片段对我有用。在 ViewController 中我想显示我刚刚调用的模糊效果 [[self view] addSubView:blurredView]

请注意,如果 "Reduce Transparency" 可访问性设置打开,任何带有 UIBlurEffectUIVisualEffectView 都将是不透明的。

(设置 > 通用 > 辅助功能 > 增加对比度 > 降低透明度)

(这是您实际使用 if (!UIAccessibilityIsReduceTransparencyEnabled()) 语句检查的内容。)

只需像这样设置包含模糊视图的视图控制器: myViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;

它阻止了从视图层次结构中删除视图的优化,这反过来又使模糊视图首先呈现,从而使其不透明。