iOS 7 半透明模态视图控制器

编程入门 行业动态 更新时间:2024-10-28 16:27:46
本文介绍了iOS 7 半透明模态视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

iOS 7 上的 App Store 应用程序使用磨砂玻璃效果,可以看到后面的景色.这是使用内置于 iOS 7 的 API 还是自定义代码.我希望它是前者,但我在文档中看不到任何明显的参考资料.明显的事情(比如在模态视图上设置 alpha 属性)似乎没有任何效果.

要查看示例,请打开 App Store 应用并按下右上角的按钮.

解决方案

随着 iOS 8.0 的发布,不再需要获取图像并对其进行模糊处理.正如 (下载)

UIImage* imageOfUnderlyingView = [self.view convertViewToImage];imageOfUnderlyingView = [imageOfUnderlyingView applyBlurWithRadius:20tintColor:[UIColor colorWithWhite:1.0 alpha:0.2]饱和度DeltaFactor:1.3掩码图像:无];

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];}

The App Store app on iOS 7 uses a frosted glass-type effect where it is possible to see the view behind. Is this using an API built into iOS 7 or is it custom code. I was hoping it would be the former but I can't see any obvious references in the documentation. Obvious things like (like setting the alpha property on the modal view) don't seem to have any effect.

To see an example, open the App Store app and press the button at the top-right.

解决方案

With the release of iOS 8.0, there is no need for getting an image and blurring it anymore. As Andrew Plummer pointed out, you can use UIVisualEffectView with 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];

Solution that works before iOS 8

I would like to extend on rckoenes' answer:

As emphasised, you can create this effect by:

  • Convert the underlying UIView to an UIImage
  • Blur the UIImage
  • Set the UIImage as background of your view.
  • Sounds like a lot of work, but is actually done pretty straight-forward:

    1. Create a category of UIView and add the following method:

    -(UIImage *)convertViewToImage { UIGraphicsBeginImageContext(self.bounds.size); [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }

    2. Make an image of the current view and blur it by using Apple's Image Effect category (download)

    UIImage* imageOfUnderlyingView = [self.view convertViewToImage]; imageOfUnderlyingView = [imageOfUnderlyingView applyBlurWithRadius:20 tintColor:[UIColor colorWithWhite:1.0 alpha:0.2] saturationDeltaFactor:1.3 maskImage:nil];

    3. Set it as background of your overlay.

    -(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]; }

    更多推荐

    iOS 7 半透明模态视图控制器

    本文发布于:2023-11-05 10:53:38,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1560625.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:视图   控制器   模态   iOS

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!