我正在尝试使用 UIVibrancyEffect
添加一个模糊的充满活力的叠加层,类似于 iOS 7/8 中的控制中心。我尝试使用 UIVibrancyEffect
创建一个 UIVisualEffectView
,然后将其添加到我的 View 中,但结果并没有模糊,只是变得充满活力。
为了让它变得模糊,我似乎必须将充满活力的效果 View 添加到模糊效果 View 的内容 View 中:
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]];
是这样吗?如果是这样,为什么我在实例化 UIVibrancyEffect
时必须传递模糊效果?我们是否能够拥有不模糊的充满活力的观点?如果是这样,我似乎无法让它工作,我也不明白你为什么会模糊不清。
最佳答案
按顺序回答最后一段中的问题:是;因为这就是它的目的;没有。
vibrancy 效果的唯一目的是与底层模糊效果协调一致,因此您必须使用模糊视觉效果 View 的模糊效果初始化 vibrancy 效果在它后面分层。有关更多信息,请参阅 WWDC 2014 视频 221 以及 UIVisualEffectView.h header 。
关于ios - 为了使模糊的 UIVibrancyEffect 起作用,我们是否必须将它作为单独的 View 添加到 UIBlurEffect contentView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25190984/