[英]How can I create a translucent modal UIViewController?
我想創建一個可重用的UIViewController子類,它可以顯示為任何其他視圖控制器上的模態視圖控制器。 這個可重用的VC需要做的第一件事就是彈出一個UIActionSheet。 為此,我在VC中創建一個默認(空白)視圖以顯示來自的操作表。
但是,這看起來很糟糕,因為當彈出模態vc時,隱藏了父vc。 因此,看起來動作表漂浮在空白背景上。 如果動作表可能看起來彈出原始(父)vc會更好。
有沒有辦法實現這個目標? 簡單地抓住父vc的視圖並從中激活UIActionSheet是否安全?
在您的模態視圖動畫后,它的大小將調整為與其父視圖相等。 你可以做的是在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];
}
您可以通過在父視圖中插入視圖來簡單地在父視圖控制器上顯示它。
像這樣的東西:
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];
是的。 將其添加到當前視圖控制器的視圖(或作為窗口的子視圖)並在屏幕上為其設置動畫,就像Valerii所說。
要用動畫刪除它,請執行此操作(我假設模態視圖為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.