我想在可见屏幕之外创建一个视图并将其推入(如默认的 pushViewController 动画),但我无法在外部创建 UIView。我在这里尝试了这段代码,但它不起作用。视图总是被创建并显示在当前 UIScreen 边界中。这意味着不是两个视图,一个被推出,一个被推入,只有出去的视图“移动”,新的视图就在它的位置。
在视图的 .m 中显示:
- (void)viewDidLoad
{
[super viewDidLoad];
// set the views frame off the screen
self.view.frame = CGRectMake(320, 0, 320, 460);
}
在实际进行转换的方法中:
-(void)showOverview:(UIViewController *)sender //sender = mapviewController
{
NSLog(@"overview");
// current view (frame) = mapviewCOntroller.view
CGRect outFrame = self.view.frame;
if (!self.overviewViewController) {
self.overviewViewController = [[UCOverviewViewController alloc] init];
self.overviewViewController.transitionDelegate = self;
// create a new View for the overview
[self.overviewViewController.view setCenter:self.view.center];
[self.view.window addSubview:self.overviewViewController.view];
[self.view.window bringSubviewToFront:self.mapViewController.view];
}
CGRect inFrame = self.overviewViewController.view.frame;
outFrame.origin.x = outFrame.origin.x-outFrame.size.width;
inFrame.origin.x = self.view.frame.origin.x;
[UIView animateWithDuration:0.5f animations: ^{
[self.view setFrame:outFrame];
}];
}
编辑:这是我的最终代码,至少是重要的部分。现在,当前可见的视图在屏幕外视图滑入的同时滑出屏幕。
-(void)showOverview
{
if (!self.overviewViewController) {
NSLog(@"OverviewViewController created!");
self.overviewViewController = [[UCOverviewViewController alloc] init];
self.overviewViewController.transitionDelegate = self;
// add the subView
[self.view addSubview:self.overviewViewController.view];
[self.view sendSubviewToBack:self.overviewViewController.view];
}
CGRect outFrame = self.mapViewController.view.frame;
CGRect inFrame = self.overviewViewController.view.frame;
outFrame.origin.x -= outFrame.size.width;
inFrame.origin.x = self.view.frame.origin.x;
self.isOverviewViewVisible = YES;
[UIView animateWithDuration:0.5f animations: ^{
[self.mapViewController.view setFrame:outFrame];
[self.overviewViewController.view setFrame:inFrame];
}];
}