如何使用UIVisualEffectView?

cNo*_*oob 207 objective-c uikit uiview ios uivisualeffectview

有人可以给出一个将模糊应用于图像的小例子吗?我一直试图找出代码一段时间了:(仍然是obj c的新东西!

UIVisualEffectView提供了对复杂视觉效果的简单抽象.根据所需的效果,结果可能会影响视图后面的内容或添加到视图的contentView的内容.

应用UIVisualEffectView现有视图以将模糊或活力效果应用于退出视图.将UIVisualEffectView添加到视图层次结构后,将任何子视图添加到的contentView UIVisualEffectView.不要将子视图直接添加到UIVisualEffectView自身.

https://developer.apple.com/documentation/uikit/uivisualeffectview#//apple_ref/occ/instp/UIVisualEffectView/contentView

B.S*_*.S. 411

只需将此模糊视图放在imageView上即可.这是Objective-C中的一个例子:

UIVisualEffect *blurEffect;
blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];

UIVisualEffectView *visualEffectView;
visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];

visualEffectView.frame = imageView.bounds;
[imageView addSubview:visualEffectView];
Run Code Online (Sandbox Code Playgroud)

和斯威夫特:

var visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .Light))    

visualEffectView.frame = imageView.bounds

imageView.addSubview(visualEffectView)
Run Code Online (Sandbox Code Playgroud)

  • 反正是否有动画模糊进/出? (5认同)
  • 非常感谢!我忘了设置框架:(谷歌还没有任何视觉效果,所以这应该是第一个搜索词:) (3认同)
  • @BS您可以为视觉效果视图的alpha设置动画.繁荣. (3认同)
  • UIBlurEffect很棒,但请注意它不适用于像iPad2这样的旧设备 - 我花了几个小时试图找出为什么它只是在我的屏幕上放置半透明层而不是模糊 - 这都是因为我在旧设备上测试 (2认同)

Mat*_*dle 134

以下是如何在UIVisualEffectView中使用UIVibrancyEffect和UIBlurEffect

Objective-C的:

// Blur effect
UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
[blurEffectView setFrame:self.view.bounds];
[self.view addSubview:blurEffectView];

// Vibrancy effect
UIVibrancyEffect *vibrancyEffect = [UIVibrancyEffect effectForBlurEffect:blurEffect];
UIVisualEffectView *vibrancyEffectView = [[UIVisualEffectView alloc] initWithEffect:vibrancyEffect];
[vibrancyEffectView setFrame:self.view.bounds];

// Label for vibrant text
UILabel *vibrantLabel = [[UILabel alloc] init];
[vibrantLabel setText:@"Vibrant"];
[vibrantLabel setFont:[UIFont systemFontOfSize:72.0f]];
[vibrantLabel sizeToFit];
[vibrantLabel setCenter: self.view.center];

// Add label to the vibrancy view
[[vibrancyEffectView contentView] addSubview:vibrantLabel];

// Add the vibrancy view to the blur view
[[blurEffectView contentView] addSubview:vibrancyEffectView];
Run Code Online (Sandbox Code Playgroud)

斯威夫特4:

    // Blur Effect
    let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    blurEffectView.frame = view.bounds
    view.addSubview(blurEffectView)

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

    // Label for vibrant text
    let vibrantLabel = UILabel()
    vibrantLabel.text = "Vibrant"
    vibrantLabel.font = UIFont.systemFont(ofSize: 72.0)
    vibrantLabel.sizeToFit()
    vibrantLabel.center = view.center

    // Add label to the vibrancy view
    vibrancyEffectView.contentView.addSubview(vibrantLabel)

    // Add the vibrancy view to the blur view
    blurEffectView.contentView.addSubview(vibrancyEffectView)
Run Code Online (Sandbox Code Playgroud)

  • 谢谢.我已经测试并确认了错误修复并编辑了答案. (2认同)

ohh*_*hhh 45

您还可以使用界面构建器轻松地为简单情况创建这些效果.由于视图的z值将取决于它们在"文档大纲"中列出的顺序,因此您可以UIVisualEffectView在要模糊的视图之前将文档轮廓拖到文档轮廓上.这会自动创建一个嵌套的UIView,这是contentView给定的属性UIVisualEffectView.在此视图中嵌套要显示在模糊之上的内容.

XCode6 beta5

您还可以轻松利用活力UIVisualEffect,这将自动创建另一个嵌套UIVisualEffectView在文档大纲中,默认情况下启用活力.然后,您可以将标签或文本视图添加到嵌套UIView(再次,contentView属性UIVisualEffectView),以实现与">滑动解锁"UI元素相同的效果.

在此输入图像描述


Ami*_*lra 9

如果有人想在Swift中得到答案:

var blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark) // Change .Dark into .Light if you'd like.

var blurView = UIVisualEffectView(effect: blurEffect)

blurView.frame = theImage.bounds // 'theImage' is an image. I think you can apply this to the view too!
Run Code Online (Sandbox Code Playgroud)

更新:

截至目前,它已在IB下可用,因此您无需为其编写任何代码:)


Pat*_*rer 7

我更喜欢通过Storyboard创建视觉效果 - 没有用于创建或维护UI元素的代码.它也给了我完整的景观支持.我做了一个使用UIVisualEffects with Blur和Vibrancy的小演示.

在Github上演示