为了使模糊的UIVibrancyEffect正常工作,我们是否必须将其作为单独的视图添加到UIBlurEffect的内容View中?



我试图使用UIVibrancyEffect添加一个模糊的充满活力的覆盖,类似于iOS 7/8中的控制中心。我试着用UIVibrancyEffect创建一个UIVisualEffectView,然后将其添加到我的视图中,但结果并不模糊,只是使其充满活力。

为了使它模糊,我似乎必须在模糊效果视图的内容视图中添加动态效果视图:

UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
effectView.translatesAutoresizingMaskIntoConstraints = NO;
// Add the blur effect to an image view.
[self.imageView addSubview:effectView];
[self.imageView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[v]|" options:0 metrics:nil views: @{ @"v" : effectView }]];
[self.imageView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[v]|" options:0 metrics:nil views: @{ @"v" : effectView }]];
UIVibrancyEffect *vibrance = [UIVibrancyEffect effectForBlurEffect:blurEffect];
UIVisualEffectView *effectView2 = [[UIVisualEffectView alloc] initWithEffect:vibrance];
effectView2.translatesAutoresizingMaskIntoConstraints = NO;
// Add the vibrance effect to our blur effect view 
[effectView.contentView addSubview:effectView2];
[effectView.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[v]|" options:0 metrics:nil views: @{ @"v" : effectView2 }]];
[effectView.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[v]|" options:0 metrics:nil views: @{ @"v" : effectView2 }]];
self.test = [[UILabel alloc] initWithFrame:CGRectZero];
self.test.translatesAutoresizingMaskIntoConstraints = NO;
self.test.text = @"TEST";
self.test.textColor = [UIColor whiteColor];
self.test.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:30];
self.test.textAlignment = NSTextAlignmentCenter;
// Add label to the vibrance content view.
[effectView2.contentView addSubview:self.test];
[effectView2.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.test attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:effectView2.contentView attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0.0]];
[effectView2.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.test attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:effectView2.contentView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.0]];

(来源:Justin Williams)

是这样吗?如果是这样,为什么我必须在实例化UIVibrancyEffect时传递模糊效果?我们是否能够拥有清晰的动态视图?如果是这样的话,我似乎不能使它工作,我也不明白为什么你要传递一个模糊

按顺序回答最后一段的问题:是;因为这就是它的作用;没有。

振动效果的唯一目的是与底层模糊效果协调一致,所以这就是为什么你必须初始化振动效果与模糊视觉效果视图的模糊效果,将被分层在它后面。更多信息请参见WWDC 2014视频221,加上UIVisualEffectView.h标题。

最新更新