UIView 不像预期的那样模糊

问题描述

嗨,我在模糊 UIView 时遇到了麻烦。我能够让 te UIView 显示一个半透明的,并让用户能够更改它,但无法弄清楚如何模糊它。我很确定我需要使用 UIVisualEffectView 和 UIBlurEffect 的视图,但不知道如何让我的视图使用 setBackgroundColor 代替。

self.diaryPlayerView = [UIView new];
[[self diaryPlayerView] setBackgroundColor:[UIColor colorWithRed:0.10 green:0.10 blue:0.10 alpha:[mediaPlayerBackgroundamountValue doubleValue]]];
[[self diaryPlayerView] setHidden:YES];
[self addSubview:[self diaryPlayerView]];

[[self diaryPlayerView] setTranslatesAutoresizingMaskIntoConstraints:NO];
[NSLayoutConstraint activateConstraints:@[
    [self.diaryPlayerView.topAnchor constraintEqualToAnchor:self.topAnchor],[self.diaryPlayerView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],[self.diaryPlayerView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],[self.diaryPlayerView.heightAnchor constraintEqualToConstant:140 + [mediaPlayerOffsetValue doubleValue]],]];

我做了以下操作,视图只是半透明的,不像预期的那样模糊

self.diaryPlayerView = [UIView new];
self.MediaPlayerblurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleSystemUltraThinMaterialDark];
self.MediaPlayerEffectView = [[UIVisualEffectView alloc] initWithEffect:[self MediaPlayerblurEffect]];    
[[self diaryPlayerView] setBackgroundColor:[UIColor colorWithRed:0.10 green:0.10 blue:0.10 alpha:[mediaPlayerBackgroundamountValue doubleValue]]];
[[self diaryPlayerView] setHidden:YES];
[self addSubview:[self diaryPlayerView]];
[self addSubview:[self MediaPlayerEffectView]];

[[self diaryPlayerView] setTranslatesAutoresizingMaskIntoConstraints:NO];
[NSLayoutConstraint activateConstraints:@[
    [self.diaryPlayerView.topAnchor constraintEqualToAnchor:self.topAnchor],]];

解决方法

终于看到了模糊我的期望。

// player view
self.diaryPlayerView = [UIView new];
self.MediaPlayerblurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
self.MediaPlayerEffectView = [[UIVisualEffectView alloc] initWithEffect:[self MediaPlayerblurEffect]];
//[[self diaryPlayerView] setBackgroundColor:[UIColor colorWithRed:0.10 green:0.10 blue:0.10 alpha:[mediaPlayerBackgroundAmountValue doubleValue]]];
[[self diaryPlayerView] setHidden:YES];
[[self MediaPlayerEffectView] setHidden:YES];
[self addSubview:[self MediaPlayerEffectView]];
[self addSubview:[self diaryPlayerView]];

[[self diaryPlayerView] setTranslatesAutoresizingMaskIntoConstraints:NO];
[NSLayoutConstraint activateConstraints:@[
    [self.diaryPlayerView.topAnchor constraintEqualToAnchor:self.topAnchor],[self.diaryPlayerView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor],[self.diaryPlayerView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor],[self.diaryPlayerView.heightAnchor constraintEqualToConstant:140 + [mediaPlayerOffsetValue doubleValue]],]];

[[self MediaPlayerEffectView] setTranslatesAutoresizingMaskIntoConstraints:NO];
[NSLayoutConstraint activateConstraints:@[
    [self.MediaPlayerEffectView.topAnchor constraintEqualToAnchor:self.diaryPlayerView.topAnchor],[self.MediaPlayerEffectView.leadingAnchor constraintEqualToAnchor:self.diaryPlayerView.leadingAnchor],[self.MediaPlayerEffectView.trailingAnchor constraintEqualToAnchor:self.diaryPlayerView.trailingAnchor],[self.MediaPlayerEffectView.heightAnchor constraintEqualToConstant:140 + [mediaPlayerOffsetValue doubleValue]],]];