用UIScrollView实现UIPageViewControl

用UIScrollView实现UIPageViewControl

本文介绍了如何使用UIScrollView实现UIPageViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I took the Photo Scroller example from Apple's website and tried to implement my own Album by copying the code. Now, the UIScrollView is not visible. How do I make it appear?

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

The only code change that I made was in creating the UIPageViewController. In my case, it's a UIViewController that is opening it and not the 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;
}


推荐答案

您没有添加 pageViewController 的视图作为 BasePhotoViewController 视图的子视图。你的 BasePhotoViewController 类看起来应该是这样的。请注意 viewDidLoad 中的代码。

You are not adding the pageViewController's view as a subview of BasePhotoViewController's view. Your BasePhotoViewController class should look something like this. Note the code in 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

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

Note: I've initialised the UIPageViewController in initWithCoder: because Apple's Photo Scroller code uses a storyboard. I removed the UIPageViewController from the storyboard and created a BasePhotoViewController in its place. If you are not loading BasePhotoViewController from a storyboard, you should move the code in initWithCoder: to the appropriate initialiser.

编辑:请参阅此在github上。

See this sample project on github.

这篇关于如何使用UIScrollView实现UIPageViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:04