我一直在尝试完全用代码复制Apple的PhotoScroller示例应用程序,但到目前为止没有成功。即使我将PhotoViewController,ImageScrollView和TileView类复制到新项目并以编程方式创建PhotoViewController实例,也无法像在PhotoScroller应用程序中那样工作:


在复制的应用程序中,我可以上下移动图像,而在PhotoScroller应用程序中,我只能向左和向右滚动图像。
分页滚动视图无法将图像正确居中对齐,因此黑色填充可见,并且图像的某些部分不在屏幕上。


相反,如果我还复制PhotoViewController和MainWindow xib文件并将MainWindow设置为iPhone部署信息中的主界面,则一切正常。因此,尽管xib文件在我看来基本上是空的,但在xib文件中必须有一些魔术。

以下是在application:didFinishLaunchingWithOptions:方法中以编程方式创建PhotoViewController的代码。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    // Override point for customization after application launch.
    self.viewController = [[[PhotoViewController alloc] initWithNibName:@"PhotoViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
)


那么,在PhotoViewController.xib和MainWindow.xib文件中进行了哪些配置,以使一切正常运行?



要理解我的意思,请按照以下简单步骤操作:


下载WWDC 2010示例代码。

http://connect.apple.com/cgi-bin/WebObjects/MemberSite.woa/wa/getSoftware?code=y&source=x&bundleID=20645
打开磁盘映像,转到iOS文件夹,然后解压缩PhotoScroller.zip。
解压缩PhotoScroller.zip,在Xcode中打开,构建并在模拟器中运行。
请注意,一切都按预期正常工作。
现在,将AppDelegate.m中的application:didFinishLaunchingWithOptions:更改为以下内容。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    PhotoViewController* vc = [[[PhotoViewController alloc] init] autorelease];
    window.rootViewController = vc;
    [window addSubview:vc.view];
    [window makeKeyAndVisible];
    return YES;
}

现在重建并在模拟器中重新运行。
注意区别,图像未居中,您可以看到黑色填充。


那么,PhotoViewController.xib中有什么设置可以使分页UIScrollView居中于ImageScrollViews?

最佳答案

确切的说,我不知道您的问题是因为邮政编码不完整,但是下面是我的代码,它100%像Photo Scroller示例应用程序一样工作。我给出了3张图片的示例,请参见

UIImage *iPhone = [UIImage imageNamed:@"iPhone.png"];
UIImage *iPad = [UIImage imageNamed:@"iPad.png"];
UIImage *macBookAir = [UIImage imageNamed:@"MacBookAir.png"];

CGRect scrollViewRect = self.view.bounds;
self.myScrollView = [[UIScrollView alloc] initWithFrame:scrollViewRect];
self.myScrollView.pagingEnabled = YES;
self.myScrollView.contentSize = CGSizeMake(scrollViewRect.size.width *scrollViewRect.size.height);
[self.view addSubview:self.myScrollView];

CGRect imageViewRect = self.view.bounds;
UIImageView *iPhoneImageView = [self newImageViewWithImage:iPhone frame:imageViewRect];
[self.myScrollView addSubview:iPhoneImageView];

/* Go to next page by moving the x position of the next image view */
imageViewRect.origin.x += imageViewRect.size.width;
UIImageView *iPadImageView = [self newImageViewWithImage:iPad frame:imageViewRect];
[self.myScrollView addSubview:iPadImageView];

/* Go to next page by moving the x position of the next image view */
imageViewRect.origin.x += imageViewRect.size.width;
UIImageView *macBookAirImageView = [self newImageViewWithImage:macBookAir frame:imageViewRect];
 [self.myScrollView addSubview:macBookAirImageView];

07-27 14:43