要查看示例,请打开 App Store 应用并按右上角的按钮。
iOS 8.0发布后,不再需要获取图像并对其进行模糊处理。正如Andrew Plummer所指出的那样,您可以使用UIVisualEffectView和UIBlurEffect。
UIViewController * contributeViewController = [[UIViewController alloc] init];
UIBlurEffect * blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *beView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
beView.frame = self.view.bounds;
contributeViewController.view.frame = self.view.bounds;
contributeViewController.view.backgroundColor = [UIColor clearColor];
[contributeViewController.view insertSubview:beView atIndex:0];
contributeViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:contributeViewController animated:YES completion:nil];
在 iOS 8 之前有效的解决方案
我想在 rckoenes 的答案上进行拓展:
正如强调的那样,您可以通过以下方式创建这种效果:
听起来很麻烦,但实际上非常简单:
1. 创建一个 UIView 类别并添加以下方法:
-(UIImage *)convertViewToImage
{
UIGraphicsBeginImageContext(self.bounds.size);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
2. 使用苹果的图像效果类别 (下载) 对当前视图进行截图并模糊处理
UIImage* imageOfUnderlyingView = [self.view convertViewToImage];
imageOfUnderlyingView = [imageOfUnderlyingView applyBlurWithRadius:20
tintColor:[UIColor colorWithWhite:1.0 alpha:0.2]
saturationDeltaFactor:1.3
maskImage:nil];
3. 将其设置为覆盖层的背景。
-(void)viewDidLoad
{
self.view.backgroundColor = [UIColor clearColor];
UIImageView* backView = [[UIImageView alloc] initWithFrame:self.view.frame];
backView.image = imageOfUnderlyingView;
backView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
[self.view addSubview:backView];
}
UIModalPresentationOverCurrentContext
实现此效果。请参见下面Andrew Plummer,mxcl和Andrey Konstantinov的答案。 - Renfei Song我刚用Swift重新实现了Sebastian Hojas的解决方案:
1. 创建一个UIView扩展并添加以下方法:
extension UIView {
func convertViewToImage() -> UIImage{
UIGraphicsBeginImageContext(self.bounds.size);
self.drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext();
return image;
}
}
2. 使用苹果的图像效果(我在这里找到了它的Swift重新实现:SwiftUIImageEffects),创建当前视图的图像并将其模糊。
var imageOfUnderlyingView = self.view.convertViewToImage()
imageOfUnderlyingView = imageOfUnderlyingView.applyBlurWithRadius(2, tintColor: UIColor(white: 0.0, alpha: 0.5), saturationDeltaFactor: 1.0, maskImage: nil)!
3. 将其设置为覆盖层的背景。
let backView = UIImageView(frame: self.view.frame)
backView.image = imageOfUnderlyingView
backView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.5)
view.addSubview(backView)
我认为这是一个在iOS8中覆盖了所有内容并且带有漂亮模糊效果的模态视图控制器的最简单解决方案。
UIViewController * contributeViewController = [[UIViewController alloc] init];
UIBlurEffect * blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
UIVisualEffectView *beView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
beView.frame = self.view.bounds;
contributeViewController.view.frame = self.view.bounds;
contributeViewController.view.backgroundColor = [UIColor clearColor];
[contributeViewController.view insertSubview:beView atIndex:0];
contributeViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:contributeViewController animated:YES completion:nil];
iOS 7 SDK中没有提供可让您“冰冻”底层视图控制器的API。
我的解决方法是将底层视图渲染为图像,然后将其冰冻并将其设置为正在呈现的视图的背景。
苹果公司为此提供了一个很好的示例:https://developer.apple.com/downloads/index.action?name=WWDC%202013
您需要的项目名为iOS_RunningWithASnap
一个更简单的方法来实现这个功能(基于Andrew Plummer的回答),使用Interface Builder(同时它也消除了在Andrew的答案中出现的副作用):
UIViewController *fancyViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"yourStoryboardIDFOrViewController"];
fancyViewController.view.backgroundColor = [UIColor clearColor];
fancyViewController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:fancyViewController
animated:YES
completion:nil];
实际上,第二行和第三行非常重要-否则控制器会闪烁然后变黑。 let vc = UIViewController()
vc.view = UIVisualEffectView(effect: UIBlurEffect(style: .Light))
vc.modalPresentationStyle = .OverFullScreen
let nc = UINavigationController(rootViewController: vc)
nc.modalPresentationStyle = .OverFullScreen
presentViewController(nc, animated: true, completion: nil)
.OverFullScreen
标志以及确保 viewControllers 具有模糊的 UIVisualEffectView,这是第一个可见的视图。正如@rckoenes所说,苹果没有提供相关的框架来实现这个效果。但是有些人已经构建了很好的替代方案,例如这个:
还有另外几种适用于 iOS 5 和 6 的替代方案:
FXBlurView:https://github.com/nicklockwood/FXBlurView
iOS RealtimeBlur:https://github.com/alexdrone/ios-realtimeblur
不要将viewController以modalView的形式呈现,而是可以将其作为子viewController添加,并创建自定义动画。然后只需要在viewDidLoad
中将viewController的默认视图更改为UIToolBar即可。
这样可以尽可能地模仿App Store的模糊模态视图。