編輯後:
做你想做的事,我認爲這是最好的下一個控制器添加爲頁面視圖控制器的子視圖控制器(這是self.parentViewController在DataViewController)。
#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文件(slidingOff)添加了BOOL屬性,並在該類的init方法中將其設置爲FALSE。然後,我在兩個頁面視圖數據源方法中的if語句中添加了另一個條件來檢查該屬性。當slidingOff爲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];
}
感謝您的幫助rdelmar,非常感謝。但是,我並不確定如何將其納入默認的基於頁面的應用程序。我試圖找到任何可以提及的案例,但找不到任何案例。 – user1947410
@ user1947410,我編輯了我的答案,告訴你如何將它添加到基於Apple頁面的應用程序模板代碼中。 – rdelmar
非常感謝rdelmar,我想知道的最後一件事是,當下一個視圖滑入(我將它設置爲僅滑動三分之一的屏幕寬度)時,DataViewController會在NextController前彈出。有沒有辦法讓DataView留在NextView的下面?再次感謝你的幫助! – user1947410