2013-02-23 24 views
0

我一直試圖完全在代碼中複製Apple的PhotoScroller示例應用程序,但目前爲止沒有成功。即使我在複製PhotoViewController,ImageScrollView和TileView類新項目和編程方式創建一個PhotoViewController情況下,它確實像PhotoScroller應用不太工作:完全在代碼中複製PhotoScroller

  1. 在我的複製軟件,我可以移動在PhotoScroller應用程序中,我只能向左和向右滾動圖像。

  2. 分頁滾動視圖無法正確對齊圖像居中,因此黑色填充可見並且圖像的某些部分不在屏幕上。

相反,一切工作正常,如果我還複製PhotoViewController和主窗口XIB文件,並設置主窗口作爲iPhone部署信息的主界面。所以在xib文件中必須有一些魔力,儘管它們對我來說基本上是空的。

下面是在應用程序中以編程方式創建PhotoViewController的代碼:didFinishLaunchingWithOptions:方法。

- (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文件中配置了什麼使一切正常工作?


要明白我的意思,請按照以下簡單步驟:

  1. 下載WWDC 2010的示例代碼。

    http://connect.apple.com/cgi-bin/WebObjects/MemberSite.woa/wa/getSoftware?code=y&source=x&bundleID=20645

  2. 打開磁盤映像,轉到iOS的文件夾,並提取PhotoScroller.zip。

  3. 解壓縮PhotoScroller.zip,在Xcode中打開,編譯並在模擬器中運行。

  4. 請注意,一切工作正常如預期。

  5. 現在將應用程序:didFinishLaunchingWithOptions:在AppDelegate.m中更改爲以下內容。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    { 
        PhotoViewController* vc = [[[PhotoViewController alloc] init] autorelease]; 
        window.rootViewController = vc; 
        [window addSubview:vc.view]; 
        [window makeKeyAndVisible]; 
        return YES; 
    } 
    
  6. 現在重建並在模擬器中重新運行。

  7. 請注意差異,圖像不居中,您可以看到黑色填充。

那麼在PhotoViewController.xib中,什麼設置使分頁UIScrollView居中放置ImageScrollViews?

回答

1

正是我不知道你的問題是因爲不完整的發佈代碼,但下面是我的代碼,100%的作品像PhotoScroller示例應用程序。我已經給了例子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]; 
+0

不敢相信沒有人回覆這個,感謝很多老兄。救世主:D – Ben 2015-01-23 11:42:33

0

也許你應該做的是:

  1. 創建XIB /或故事板。

  2. 將其類更改爲PhotoViewController。

  3. 更改其視圖所有者。它使用

  4. 負載:

    initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
    

    [self.storyboard instantiateViewControllerWithIdentifier:@"photoViewName"] 
    
  5. 最後將其添加爲一個子視圖:

    [self.view addSubview:self.photoViewController.view] 
    
  6. 不要使用:RootViewController的/或推/或模態/或繼續。

  7. 請勿將其嵌入到導航控制器中。

希望這會有所幫助。