1

我从 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;
}
4

1 回答 1

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

注意:我已经初始化了UIPageViewControllerinitWithCoder:因为 Apple 的Photo Scroller代码使用了故事板。我UIPageViewController从情节提要中删除了 并BasePhotoViewController在其位置创建了一个。如果不是BasePhotoViewController从情节提要加载,则应将代码移入initWithCoder:适当的初始化程序。

编辑:在 github 上查看这个示例项目。

于 2013-06-20T06:53:31.243 回答