创建模糊覆盖视图

创建模糊覆盖视图

在新iOS的音乐应用程序中,我们可以看到相册封面背后的一个视图模糊了它。

这样的事情怎么能实现呢?我看过文件,但没有发现任何东西。


呼啦一阵风
浏览 656回答 3
3回答

大话西游666

我决定从被接受的答案中张贴一个书面的客观-C版本,只是为了在这个问题中提供更多的选择。- (UIView *)applyBlurToView:(UIView *)view withEffectStyle:(UIBlurEffectStyle)style andConstraints:(BOOL)addConstraints{   //only apply the blur if the user hasn't disabled transparency effects   if(!UIAccessibilityIsReduceTransparencyEnabled())   {     UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:style];     UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];     blurEffectView.frame = view.bounds;     [view addSubview:blurEffectView];     if(addConstraints)     {       //add auto layout constraints so that the blur fills the screen upon rotating device       [blurEffectView setTranslatesAutoresizingMaskIntoConstraints:NO];       [view addConstraint:[NSLayoutConstraint constraintWithItem:blurEffectView                                                        attribute:NSLayoutAttributeTop                                                        relatedBy:NSLayoutRelationEqual                                                           toItem:view                                                        attribute:NSLayoutAttributeTop                                                       multiplier:1                                                         constant:0]];       [view addConstraint:[NSLayoutConstraint constraintWithItem:blurEffectView                                                        attribute:NSLayoutAttributeBottom                                                        relatedBy:NSLayoutRelationEqual                                                           toItem:view                                                        attribute:NSLayoutAttributeBottom                                                       multiplier:1                                                         constant:0]];       [view addConstraint:[NSLayoutConstraint constraintWithItem:blurEffectView                                                        attribute:NSLayoutAttributeLeading                                                        relatedBy:NSLayoutRelationEqual                                                           toItem:view                                                        attribute:NSLayoutAttributeLeading                                                       multiplier:1                                                         constant:0]];       [view addConstraint:[NSLayoutConstraint constraintWithItem:blurEffectView                                                        attribute:NSLayoutAttributeTrailing                                                        relatedBy:NSLayoutRelationEqual                                                           toItem:view                                                        attribute:NSLayoutAttributeTrailing                                                       multiplier:1                                                         constant:0]];     }   }   else   {     view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];   }   return view;}如果您只支持纵向模式,或者我只是在此函数中添加一个标志来使用这些约束,则可以删除这些约束。
打开App,查看更多内容
随时随地看视频慕课网APP