简体   繁体   English

如何创建半透明模态UIViewController?

[英]How can I create a translucent modal UIViewController?

I'd like to create a reusable UIViewController subclass that can be shown as a modal view controller over any other view controller. 我想创建一个可重用的UIViewController子类,它可以显示为任何其他视图控制器上的模态视图控制器。 One of the first things this reusable VC needs to do is pop up a UIActionSheet. 这个可重用的VC需要做的第一件事就是弹出一个UIActionSheet。 In order to do this, I create a default (blank) view in my VC to show the action sheet from. 为此,我在VC中创建一个默认(空白)视图以显示来自的操作表。

However, this looks bad because, when the modal vc pops up, the parent vc is hidden. 但是,这看起来很糟糕,因为当弹出模态vc时,隐藏了父vc。 Thus it looks like the action sheet is floating over a blank background. 因此,看起来动作表漂浮在空白背景上。 It'd be better if the action sheet could appear to pop over the original (parent) vc. 如果动作表可能看起来弹出原始(父)vc会更好。

Is there a way to achieve this? 有没有办法实现这个目标? Is it safe to simply grab the parent vc's view and animate the UIActionSheet from that? 简单地抓住父vc的视图并从中激活UIActionSheet是否安全?

After your modal view animates in, it is resized to be equal in size to its parent view. 在您的模态视图动画后,它的大小将调整为与其父视图相等。 What you can do is inside your viewDidAppear:, take a picture of the parentController's view, then insert a UIImageView containing the picture of the parent at the back of your own view's list of subviews: 你可以做的是在viewDidAppear:中,拍摄parentController的视图,然后在你自己的视图的子视图列表后面插入一个包含父图片的UIImageView:

#pragma mark -
#pragma mark Sneaky Background Image
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    // grab an image of our parent view
    UIView *parentView = self.parentViewController.view;

    // For iOS 5 you need to use presentingViewController:
    // UIView *parentView = self.presentingViewController.view;

    UIGraphicsBeginImageContext(parentView.bounds.size);
    [parentView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *parentViewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // insert an image view with a picture of the parent view at the back of our view's subview stack...
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageView.image = parentViewImage;
    [self.view insertSubview:imageView atIndex:0];
    [imageView release];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    // remove our image view containing a picture of the parent view at the back of our view's subview stack...
    [[self.view.subviews objectAtIndex:0] removeFromSuperview];
}

You can simply show it over your parent view controller by inserting view in a parent view. 您可以通过在父视图中插入视图来简单地在父视图控制器上显示它。

Something like this: 像这样的东西:

PseudoModalVC *vc = ...//initialization
vc.view.backgroundColor = [UIColor clearColor]; // like in previous comment, although you can do this in Interface Builder
vc.view.center = CGPointMake(160, -vc.view.bounds.size.height/2);
[parentVC.view addSubView:vc.view];

// animation for pop up from screen bottom
[UIView beginAnimation:nil context:nil];
vc.view.center = CGPointMake(160, vc.view.bounds.size.height/2);
[UIView commitAnimation];

Yep. 是的。 Add it to the current view controller's view (or as a subview of the window) and animate it on-screen like Valerii said. 将其添加到当前视图控制器的视图(或作为窗口的子视图)并在屏幕上为其设置动画,就像Valerii所说。

To remove it with an animation, do this (I'm assuming the modal view is 320 x 460 and it will slide down off the screen): 要用动画删除它,请执行此操作(我假设模态视图为320 x 460,它将从屏幕滑下):

- (void)dismissModal
{
    // animate off-screen
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDuration:0.50];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

    self.view.frame = CGRectMake( 0, 480, 320, 460 );

    [UIView commitAnimations];
}

- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    // don't remove until animation is complete. otherwise, the view will simply disappear
    [self.view removeFromSuperview];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM