ios – 如何使用UIBlurEffect与模态视图控制器?

前端之家收集整理的这篇文章主要介绍了ios – 如何使用UIBlurEffect与模态视图控制器?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个UIViewController,以模态方式呈现另一个UIViewController.我想要模态视图控制器具有iOS 7引入的模糊/透明度.我尝试使用新的UIVisualEffect,但它似乎只适用于UIViews,而不是UIViewControllers?

这是我写的代码,我添加为子视图的所有视图都是我想要在用户界面上方的模糊视图下方的.

在演示视图控制器中,我将屏幕截图显示给模糊视图控制器,然后再应用模糊.

在呈现视图控制器:

- (UIImage *)viewImage {
    CGSize size = CGSizeMake(self.view.frame.size.width,self.view.frame.size.height);
    UIGraphicsBeginImageContext(size);
    [self.view drawViewHierarchyInRect:(CGRect){CGPointZero,self.view.frame.size.width,self.view.frame.size.height} afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

在模态视图控制器:

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:[[UIImageView alloc] initWithImage:self.backgroundImage]];
    //self.backgroundImage is the image that the method above returns,it's a screenshot of the presenting view controller.
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    [blurEffectView setFrame:self.view.bounds];
    [self.view addSubview:blurEffectView];

    [self.view bringSubviewToFront:self.cancelButton];
    [self.view bringSubviewToFront:self.titleLabel];
    [self.view bringSubviewToFront:self.tableView];
}

解决方法

使用UIVisualEffectView时,不需要生成快照.尝试删除“ – (UIImage *)viewImage”,并在模型视图控制器中添加一个与视图控制器视图大小匹配的UIVisualEffectView,并将所有“控制”视图添加到UIVisualEffectView的contentView中.
- (void)viewDidLoad {
    [super viewDidLoad];

    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    [blurEffectView setFrame:self.view.bounds];
    [self.view addSubview:blurEffectView];

    [blurEffectView.contentView addSubview:self.cancelButton];
    [blurEffectView.contentView addSubview:self.titleLabel];
    [blurEffectView.contentView addSubview:self.tableView];
}

https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIVisualEffectView/index.html

我没有尝试过故事板,但这是一个经过测试的工作片段.可能是一个开始的好地方.翻译成objc应该是挺直的

这是演示视图控制器

import UIKit

class BaseViewController: UIViewController {

init(nibName nibNameOrNil: String?,bundle nibBundleOrNil: NSBundle?) {
    super.init(nibName: nibNameOrNil,bundle: nibBundleOrNil)
}

override func viewDidLoad() {
    super.viewDidLoad()

    var backgroundImage = UIImageView(image: UIImage(named: "image"))
    self.view.addSubview(backgroundImage)

    var button = UIButton(frame: CGRectMake(0,100,320,50))
    button.setTitle("Lorem Ipsum",forState: UIControlState.Normal)
    button.backgroundColor = UIColor.redColor()
    button.addTarget(self,action: "onButtonTapped:",forControlEvents: UIControlEvents.TouchUpInside)

    self.view.addSubview(button)
}

func onButtonTapped(sender: UIButton?) {
    var modal = ModalViewController(nibName: nil,bundle: nil)
    modal.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
    self.presentViewController(modal,animated: true,completion: {})
}
}

这是模态

import UIKit

class ModalViewController: UIViewController {

init(nibName nibNameOrNil: String?,bundle: nibBundleOrNil)
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = UIColor.clearColor()

    let effect = UIBlurEffect(style: UIBlurEffectStyle.Light)
    let blurView = UIVisualEffectView(effect: effect)

    blurView.frame = self.view.bounds

    self.view.addSubview(blurView)
}
}

猜你在找的iOS相关文章