【问题标题】:Replicating PhotoScroller entirely in code完全在代码中复制 PhotoScroller
【发布时间】:2013-02-09 05:19:15
【问题描述】:

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

  1. 在我复制的应用程序中,我可以上下移动图像,而在 PhotoScroller 应用程序中,我只能左右滚动图像。

  2. 分页滚动视图未正确对齐居中的图像,因此黑色填充可见并且图像的某些部分不在屏幕上。

相反,如果我还复制 PhotoViewController 和 MainWindow xib 文件并将 MainWindow 设置为 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. 现在将 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;
    }
    
  6. 现在在模拟器中重建并重新运行。

  7. 注意区别,图像没有居中,您可以看到黑色填充。

那么 PhotoViewController.xib 中的什么设置使分页 UIScrollView 以 ImageScrollViews 为中心?

【问题讨论】:

    标签: iphone ios ios6 uiscrollview xib


    【解决方案1】:

    也许你应该做的是:

    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 / 或 push / 或 modal / 或 segue。

    7. 请勿将其嵌入导航控制器中。

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      由于发布的代码不完整,我不知道您的问题是什么,但下面是我的代码,它 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];
      

      【讨论】:

      • 不敢相信没有人回复这个,非常感谢老兄。救世主:D
      猜你喜欢
      • 2010-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-16
      • 2021-02-08
      相关资源
      最近更新 更多