【问题标题】:uiscrollview not switching image subviewsuiscrollview 不切换图像子视图
【发布时间】:2010-03-17 03:51:53
【问题描述】:

我正在构建一个漫画查看器应用程序,它由两个视图控制器组成,根视图控制器基本上显示一个视图,用户可以通过按下按钮来决定他们想要阅读的漫画。第二个 viewController 实际上将漫画显示为带有工具栏和顶部标题的 uiscrollview。

所以我遇到的问题是,如果您在查看第一部漫画后选择另一部漫画,漫画图像面板本身不会与您进入的第一部漫画不同。我设置它的方式,我承认它不完全是 mvc,所以请不要讨厌,无论如何我设置它的方式是每个漫画 uiscrollview 包含 x 个 jpg 图像,其中每个漫画集的图像名称都有一个共同的前缀然后是“funny1.jpg”、“funny2.jpg”、“funny3.jpg”和“soda1.jpg”、“soda2.jpg”、“soda3.jpg”等数字......

因此,当用户选择要在根控制器中查看的漫画时,它会调用委托并在属于委托的漫画视图控制器的实例上设置 ivars (mainDelegate.comicViewController.property) 我设置了面板的数量该漫画、标题标签的漫画名称和图像前缀。

图片的数量发生了变化(或者至少是您可以滚动浏览的数量),并且标题也发生了变化,但出于某种原因,这些图片与您最初点击的任何漫画相同。

我的整个应用程序都基于来自苹果的“滚动”代码示例。

我想如果我每次用户单击可以修复它的按钮时都添加一个 viewWillAppear:(BOOL) 动画调用到comicViewController,但它没有,毕竟这是滚动视图的布局。

这里是两个控制器各自的一些代码:

根控制器:

-(IBAction) launchComic2{

  AppDelegate *mainDelegate = [(AppDelegate *) [UIApplication sharedApplication] delegate];
  mainDelegate.myViewController.comicPageCount = 3;
   mainDelegate.myViewController.comicTitle.text = @"\"Death by ETOH\"";
   mainDelegate.myViewController.comicImagePrefix = @"etoh";

   [mainDelegate.myViewController viewWillAppear:YES];
   [mainDelegate.window addSubview: mainDelegate.myViewController.view];

comicViewController:

   -(void) viewWillAppear:(BOOL)animated {


   self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];

   // 1. setup the scrollview for multiple images and add it to the view controller
   //
   // note: the following can be done in Interface Builder, but we show this in code for       clarity
   [scrollView1 setBackgroundColor:[UIColor whiteColor]];
   [scrollView1 setCanCancelContentTouches:NO];
   scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
   scrollView1.clipsToBounds = YES;  // default is NO, we want to restrict drawing       within our scrollview
   scrollView1.scrollEnabled = YES;

   // pagingEnabled property default is NO, if set the scroller will stop or snap at       each photo
   // if you want free-flowing scroll, don't set this property.
   scrollView1.pagingEnabled = YES;

   // load all the images from our bundle and add them to the scroll view
   NSUInteger i;
   for (i = 1; i <= self.comicPageCount; i++)
   {

    NSString *imageName = [NSString stringWithFormat:@"%@%d.jpg", self.comicImagePrefix, i];
    NSLog(@"%@%d.jpg", self.comicImagePrefix, i);
    UIImage *image = [UIImage imageNamed:imageName];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

    // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
    CGRect rect = imageView.frame;
    rect.size.height = kScrollObjHeight;
    rect.size.width = kScrollObjWidth;
    imageView.frame = rect;
    imageView.tag = i; // tag our images for later use when we place them in serial fashion
    [scrollView1 addSubview:imageView];
    [imageView release];
   }

   [self layoutScrollImages]; // now place the photos in serial layout within the scrollview




       }


  - (void)layoutScrollImages
       {
   UIImageView *view = nil;
   NSArray *subviews = [scrollView1 subviews];

   // reposition all image subviews in a horizontal serial fashion
   CGFloat curXLoc = 0;
   for (view in subviews)
   {
    if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
    {
     CGRect frame = view.frame;
     frame.origin = CGPointMake(curXLoc, 0);
     view.frame = frame;

     curXLoc += (kScrollObjWidth);
    }
   }

   // set the content size so it can be scrollable
   [scrollView1 setContentSize:CGSizeMake((self.comicPageCount * kScrollObjWidth),           [scrollView1 bounds].size.height)];
       }

对此的任何帮助将不胜感激。

尼克

【问题讨论】:

    标签: iphone objective-c xcode uiscrollview uiimageview


    【解决方案1】:

    所以我最终做的只是在委托中创建多个漫画控制器实例,而不是仅仅重新使用一个。如果我遇到问题会更新这个。

    尼克

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多