1

我已将我的功能应用于我的 Today Widget 应用程序applyVibrancyviewDidLoad方法。mainViewController

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
 }

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

我怎样才能做到这一点?

4

1 回答 1

2

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

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

UIVibrancyEffect 放大和调整 view 后面分层内容的颜色,让放置在 contentView 内的内容变得更加生动。它旨在放置在已配置 UIBlurEffect 的 UIVisualEffectView 之上或作为其子视图。此效果仅影响添加到 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)
于 2015-02-09T03:56:39.530 回答