我拿了照片滚动来自苹果网站的示例,并尝试通过复制代码来实现我自己的专辑。现在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;
}
您没有添加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
Note:我已经初始化了UIPageViewController
in initWithCoder:
因为苹果的照片滚动代码使用故事板。我删除了UIPageViewController
从故事板中创建了一个BasePhotoViewController
在它的位置。如果您没有加载BasePhotoViewController
从故事板中,您应该将代码移入initWithCoder:
到适当的初始化程序。
EDIT:看到这个示例项目在 github 上。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)