编辑后:
做你想做的事,我认为最好将下一个控制器添加为页面视图控制器的子视图控制器(即DataViewController中的self.parentViewController)。
#import "DataViewController.h"
#import "NextController.h"
#import "ModelController.h"
@implementation DataViewController {
BOOL viewRevealed;
NextController *next;
}
- (void)viewDidLoad {
[super viewDidLoad];
viewRevealed = NO;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.dataLabel.text = [self.dataObject description];
}
-(IBAction)slideInController:(UIButton *) sender {
if (viewRevealed == NO) {
next = [self.storyboard instantiateViewControllerWithIdentifier:@"Blue"];
next.view.frame = self.parentViewController.view.frame;
[self.parentViewController addChildViewController:next];
[self.parentViewController.view insertSubview:next.view belowSubview:self.view];
[next didMoveToParentViewController:self.parentViewController];
[UIView animateWithDuration:.6 animations:^{
self.view.frame = CGRectMake(self.view.frame.origin.x + self.view.frame.size.width/2, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
} completion:^(BOOL finished) {
viewRevealed = YES;
[(ModelController *)[(UIPageViewController *)self.parentViewController dataSource] setSlidOff:TRUE];
}];
}else{
[UIView animateWithDuration:.6 animations:^{
self.view.frame = CGRectMake(self.view.frame.origin.x - self.view.frame.size.width/2 , self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
} completion:^(BOOL finished) {
[next.view removeFromSuperview];
[next removeFromParentViewController];
viewRevealed = NO;
[(ModelController *)[(UIPageViewController *)self.parentViewController dataSource] setSlidOff:FALSE];
}];
}
}
这是DataViewController 中的所有代码。我添加了一些必要的 ivars。在我的测试应用程序中,我将 slideInController 方法连接到双击手势识别器。如果在 DataView 部分显示在屏幕上时单击或滑动会发生奇怪的事情,因此我在 ModelController.h 文件 (slidOff) 中添加了一个 BOOL 属性,并在该类的 init 方法中将其设置为 FALSE。然后,我在两个页面视图数据源方法中的 if 语句中添加了另一个条件来检查该属性。当 slidOff 为 TRUE 时,滑动手势识别器不会做任何事情。这是 ModelController 中的这 2 个方法,因为我对其进行了修改:
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(DataViewController *)viewController];
if ((index == 0) || (index == NSNotFound) || _slidOff) {
return nil;
}
index--;
return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(DataViewController *)viewController];
if (index == NSNotFound || _slidOff) {
return nil;
}
index++;
if (index == [self.pageData count]) {
return nil;
}
return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}