在新iOS的音乐应用程序中,我们可以看到一个模糊的视图后面有一个专辑封面。

这样的事情是如何完成的呢?我看了文件,但什么都没发现。


当前回答

简单的答案是添加一个子视图并改变它的alpha。

UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
UIView *subView = [[UIView alloc] initWithFrame:popupView.frame];
UIColor * backImgColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue_Img.png"]];
subView.backgroundColor = backImgColor;
subView.alpha = 0.5;
[mainView addSubview:subView];

其他回答

在Xcode中你可以很容易地做到。遵循xcode中的步骤。在你的uiview或imageview上拖动视觉效果视图。

快乐编码:)

我认为最简单的解决方案是覆盖UIToolbar,它在iOS 7中模糊了它后面的所有东西。这很狡猾,但实现起来非常简单,而且速度很快!

你可以对任何视图这么做,只是让它成为UIToolbar的子类而不是UIView。你甚至可以用UIViewController的view属性来做,例如…

1)创建一个新类,它是UIViewController的“子类”,并选中“With XIB for user interface”。

2)选择View,进入右边面板中的标识检查器(alt-command-3)。将“Class”改为UIToolbar。现在转到属性检查器(alt-command-4),将“背景”颜色改为“清色”。

3)在主视图中添加子视图,并将其连接到接口中的IBOutlet。叫它backgroundColorView。它看起来像这样,是实现(.m)文件中的一个私有类别。

@interface BlurExampleViewController ()
@property (weak, nonatomic) IBOutlet UIView *backgroundColorView;
@end

4)转到视图控制器实现(.m)文件并更改-viewDidLoad方法,如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.barStyle = UIBarStyleBlack; // this will give a black blur as in the original post
    self.backgroundColorView.opaque = NO;
    self.backgroundColorView.alpha = 0.5;
    self.backgroundColorView.backgroundColor = [UIColor colorWithWhite:0.3 alpha:1];
}

这将给你一个深灰色的视图,它模糊了它后面的一切。没有有趣的业务,没有缓慢的核心图像模糊,使用操作系统/SDK提供的一切。

你可以将这个视图控制器的视图添加到另一个视图,如下所示:

[self addChildViewController:self.blurViewController];
[self.view addSubview:self.blurViewController.view];
[self.blurViewController didMoveToParentViewController:self];

// animate the self.blurViewController into view

如果有什么不清楚的地方请告诉我,我很乐意帮忙!


Edit

UIToolbar在7.0.3中被改变,当使用彩色模糊时可能会产生不希望看到的效果。

我们过去能够使用barTintColor设置颜色,但如果你以前这样做,你将需要将alpha组件设置为小于1。否则你的UIToolbar将是完全不透明的颜色-没有模糊。

这可以通过如下方式实现:(记住self是UIToolbar的子类)

UIColor *color = [UIColor blueColor]; // for example
self.barTintColor = [color colorWithAlphaComponent:0.5];

这将给模糊的视图一个蓝色的色调。

下面是Swift中使用CIGaussianBlur的快速实现:

func blur(image image: UIImage) -> UIImage {
    let radius: CGFloat = 20;
    let context = CIContext(options: nil);
    let inputImage = CIImage(CGImage: image.CGImage!);
    let filter = CIFilter(name: "CIGaussianBlur");
    filter?.setValue(inputImage, forKey: kCIInputImageKey);
    filter?.setValue("\(radius)", forKey:kCIInputRadiusKey);
    let result = filter?.valueForKey(kCIOutputImageKey) as! CIImage;
    let rect = CGRectMake(radius * 2, radius * 2, image.size.width - radius * 4, image.size.height - radius * 4)
    let cgImage = context.createCGImage(result, fromRect: rect);
    let returnImage = UIImage(CGImage: cgImage);

    return returnImage;
}

斯威夫特4:

你也可以使用容器视图,它可以让你获得一个免费的视图控制器(你可以从通常的对象面板/库中获得容器视图)。

步骤:

有一个视图(图片中的ViewForContainer)持有这个容器视图,当容器视图的内容显示时,使其暗淡。连接第一个视图控制器内部的出口

当第一个VC加载时隐藏这个视图

当按钮点击此处输入图像描述时取消隐藏

当容器视图内容显示时,要使该视图变暗,将视图背景设置为黑色,不透明度设置为30%

我已经在其他Stackoverflow问题中添加了popview视图创建的答案 https://stackoverflow.com/a/49729431/5438240

为了帮助大家,下面是我根据Jordan h的回答创建的一个快速扩展。它是用swift 5编写的,可以在Objective C中使用。

extension UIView {

    @objc func blurBackground(style: UIBlurEffect.Style, fallbackColor: UIColor) {
        if !UIAccessibility.isReduceTransparencyEnabled {
            self.backgroundColor = .clear

            let blurEffect = UIBlurEffect(style: style)
            let blurEffectView = UIVisualEffectView(effect: blurEffect)
            //always fill the view
            blurEffectView.frame = self.self.bounds
            blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

            self.insertSubview(blurEffectView, at: 0)
        } else {
            self.backgroundColor = fallbackColor
        }
    }

}

注意:如果你想在不影响文本的情况下模糊UILabel的背景,你应该创建一个容器UIView,将UILabel作为子视图添加到容器UIView,将UILabel的backgroundColor设置为UIColor。clear,然后调用blurBackground(style: UIBlurEffect。样式,fallbackColor: UIColor)的容器UIView。下面是用Swift 5写的一个简单的例子:

let frame = CGRect(x: 50, y: 200, width: 200, height: 50)
let containerView = UIView(frame: frame)
let label = UILabel(frame: frame)
label.text = "Some Text"
label.backgroundColor = UIColor.clear
containerView.addSubview(label)
containerView.blurBackground(style: .dark, fallbackColor: UIColor.black)