如何取消特定视图的通知中心视觉效果 (iOS)?

How can I cancel Notification Center Visual Effect (iOS) for an specific view?

我已将我的函数 applyVibrancy 应用到我的 mainViewControllerviewDidLoad 方法上,用于我的 Today Widget 应用程序。

override func viewDidLoad() {
   applyVibrancy()
} 

func applyVibrancy()
{
    let oldView = self.view
    var effectView = UIVisualEffectView(effect: UIVibrancyEffect.notificationCenterVibrancyEffect())

    effectView.frame = oldView.bounds
    effectView.autoresizingMask = oldView.autoresizingMask;

    effectView.userInteractionEnabled = true    
    effectView.contentView.addSubview(oldView)        
    self.view.tintColor = UIColor.clearColor()

    self.view = effectView
 }

这成功地将这种视觉效果应用到我的整个小部件中。但我希望我的一些嵌套视图(标签、按钮、图像等)不会受此影响。

我怎样才能做到这一点?

为了达到你想要的效果,对于你想要有这个效果的视图,将它们添加到UIVisualEffectViewcontentView,然后添加为[=13]的子视图=].其他不受影响的view,直接加到self.view即可。

当我 运行 你的代码时,我得到一个黑屏。

UIVibrancyEffect amplifies and adjusts the color of content layered behind the view, allowing content placed inside the contentView to become more vivid. It is intended to be placed over, or as a subview of, a UIVisualEffectView that has been configured with a UIBlurEffect. This effect only affects content added to the contentView.

notificationCenterVibrancyEffect 是一种 UIVibrancyEffect,但在您的代码中没有配置了 UIBlurEffect 的 UIVisualEffectView,您应该创建一个,并将 effectView 放在该视图上或添加 effectView 作为该视图的 contentView 的子视图。否则你将看不到任何活力。

这是一些测试代码。

let label = UILabel()
label.frame = CGRectMake(0, 0, 130, 30)
label.text = "Has Vibracy!"

let effectView = UIVisualEffectView(effect: UIVibrancyEffect.notificationCenterVibrancyEffect())

effectView.frame = CGRectMake(0, 0, 130, 30)
effectView.backgroundColor = UIColor.clearColor()
effectView.userInteractionEnabled = true

effectView.contentView.addSubview(label)

let blurView = UIVisualEffectView(effect: UIBlurEffect(style: .Dark))
blurView.contentView.addSubview(effectView)
blurView.frame = CGRectMake(80, 20, 130, 30)

self.view.tintColor = UIColor.clearColor()

let label1 = UILabel()
label1.frame = CGRectMake(80, 60, 130, 30)
label1.text = "No Vibracy!"

self.view.addSubview(blurView)
self.view.addSubview(label1)