如何使用UIScrollView实现UIPageViewController?

时间:2013-06-18 14:39:32

标签: ios objective-c uiscrollview uipageviewcontroller

我从Apple的网站上获取了Photo Scroller示例,并试图通过复制代码来实现我自己的相册。现在,UIScrollView不可见。如何让它出现?

我所做的唯一代码更改是创建UIPageViewController。在我的情况下,它是UIViewController打开它而不是AppDelegate

@implementation BasePhotoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nil bundle:nibBundleOrNil];
    if (self) {
        // kick things off by making the first page

        PhotoViewController *pageZero = [PhotoViewController photoViewControllerForPageIndex:0];
        if (pageZero != nil)
        {
            // assign the first page to the pageViewController (our rootViewController)
            //UIPageViewController *pageViewController = (UIPageViewController *)    [[UIApplication sharedApplication] keyWindow].rootViewController;
            UIPageViewController *pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:0 navigationOrientation:0 options:nil];
            //UIPageViewController *pageViewController = (UIPageViewController *)self.parentViewController;
            pageViewController.dataSource = self;

            [pageViewController setViewControllers:@[pageZero]
                                     direction:UIPageViewControllerNavigationDirectionForward
                                      animated:NO
                                    completion:NULL];
        }
    }
    return self;
}

1 个答案:

答案 0 :(得分:3)

您没有将pageViewController的视图添加为BasePhotoViewController视图的子视图。你的BasePhotoViewController课应该是这样的。请注意viewDidLoad中的代码。

<强> BasePhotoViewController.h:

@interface BasePhotoViewController : UIViewController <UIPageViewControllerDataSource>
@property (nonatomic, strong) UIPageViewController * pageViewController;
@end

<强> BasePhotoViewController.m:

#import "BasePhotoViewController.h"
#import "PhotoViewController.h"

@implementation BasePhotoViewController

@synthesize pageViewController;

- (id)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        PhotoViewController *pageZero = [PhotoViewController photoViewControllerForPageIndex:0];
        if (pageZero != nil)
        {
            self.pageViewController = [[UIPageViewController alloc] initWithTransitionStyle:0
                                                                      navigationOrientation:0
                                                                                    options:nil];
            self.pageViewController.dataSource = self;

            [self.pageViewController setViewControllers:@[pageZero]
                                              direction:UIPageViewControllerNavigationDirectionForward
                                               animated:NO
                                             completion:NULL];
        }
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    [self.pageViewController didMoveToParentViewController:self];
}

# pragma mark - UIPageViewControllerDataSource

- (UIViewController *)pageViewController:(UIPageViewController *)pvc viewControllerBeforeViewController:(PhotoViewController *)vc
{
    NSUInteger index = vc.pageIndex;
    return [PhotoViewController photoViewControllerForPageIndex:(index - 1)];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pvc viewControllerAfterViewController:(PhotoViewController *)vc
{
    NSUInteger index = vc.pageIndex;
    return [PhotoViewController photoViewControllerForPageIndex:(index + 1)];
}

@end

注意:我已初始化UIPageViewController中的initWithCoder:,因为Apple的Photo Scroller代码使用了故事板。我从故事板中删除了UIPageViewController并在其位置创建了BasePhotoViewController。如果您未从故事板中加载BasePhotoViewController,则应将initWithCoder:中的代码移至相应的初始化程序。

编辑:在github上查看此sample project