简体   繁体   中英

Replicating PhotoScroller entirely in code

I've been trying to replicate Apple's PhotoScroller sample app entirely in code but had no success so far. Even if I copy the PhotoViewController, ImageScrollView, and TileView classes over to a new project and create a PhotoViewController instance programmatically, it doesn't quite work like in the PhotoScroller app:

  1. In my replicated app I can move the images up and down while in the PhotoScroller app I can only scroll the images left and right.

  2. The paging scroll view doesn't align the images centered properly so the black padding is visible and some parts of the images are offscreen.

Instead, everything works fine if I also copy the PhotoViewController and MainWindow xib files and set the MainWindow as the main interface in the iPhone deployment info. So there must be some magic in the xib files although they seem basically empty to me.

Here's the code to create the PhotoViewController programmatically in the application:didFinishLaunchingWithOptions: method.

- (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;
)

So what's configured in the PhotoViewController.xib and MainWindow.xib files to make everything work nicely?


To understand what I mean, please follow these easy steps:

  1. Download WWDC 2010 sample code.

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

  2. Open disk image, goto iOS folder, and extract PhotoScroller.zip.

  3. Unzip PhotoScroller.zip, open in Xcode, build, and run in the simulator.

  4. Note that everything works properly as expected.

  5. Now change the application:didFinishLaunchingWithOptions: in AppDelegate.m to the following.

     - (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. Now rebuild and rerun in the simulator.

  7. Note the difference, the images are not centered and you can see the black padding instead.

So what setting is in the PhotoViewController.xib that makes the paging UIScrollView center the ImageScrollViews?

exactly i dont know what is your problem because of incompleted posted code, but below is the my code which 100% works like PhotoScroller sample app. i have given example of 3 images, see this

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];

Perhaps what you should do is:

  1. Create an xib / or storyboard.

  2. Change its class to PhotoViewController.

  3. Change its view owner.

  4. Load it using:

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

    or

     [self.storyboard instantiateViewControllerWithIdentifier:@"photoViewName"] 
  5. And finally add it as a subview:

     [self.view addSubview:self.photoViewController.view] 
  6. Do not use: rootViewController / or push / or modal / or segue.

  7. Do not embed it in a navigation controller.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM