iOS代码显示白色字段而不是图像系列

时间:2012-02-26 02:28:41

标签: iphone ios xcode

下面的代码应该在无限连接循环中显示一系列图像。相反,它只显示一个完全白色的字段。感谢Aaron Hayman让我走到这一步。

- (void)layoutScrollImages
{
    //Arrangement
    NSArray *imageViews = [NSArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png", nil];
    CGRect cRect = scrollView1.bounds;
    UIImageView *cView; 
    for (int i = 0; i < imageViews.count; i++)
    {
        cView = [imageViews objectAtIndex:i];
        cView.frame = cRect;
        [scrollView1 addSubview:cView];
        cRect.origin.x += cRect.size.width;
    }
    scrollView1.contentSize = CGSizeMake(cRect.origin.x, scrollView1.bounds.size.height);
    scrollView1.contentOffset = CGPointMake(scrollView1.bounds.size.width, 0); //should be the center page in a 3 page setup

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    scrollView1.contentOffset = CGPointMake(scrollView.bounds.size.width, 0);
}

关注:

我所做的一切都没有任何区别。目前,我的代码看起来像这样,但我正在改变我能想到的一切无济于事。

- (void)layoutScrollImages
{
    //Horizontal arrangement
    NSObject *image1 = @"image1.png";
    NSObject *image2 = @"image2.png";
    NSObject *image3 = @"image3.png";
    NSArray *imageViews = [NSArray arrayWithObjects:image1, image2, image3, nil];
    CGRect cRect = scrollView1.bounds;
    UIImageView *cView; 
    for (int i = 0; i < imageViews.count; i++)
    {
        cView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[imageViews objectAtIndex:i]]];
        cView.frame = cRect;
        [scrollView1 addSubview:cView];
        cRect.origin.x += cRect.size.width;
    }
    scrollView1.contentSize = CGSizeMake(cRect.origin.x, scrollView1.bounds.size.height);
    scrollView1.contentOffset = CGPointMake(scrollView1.bounds.size.width, 0); //should be the center page in a 3 page setup

    [self layoutScrollImages];

}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    scrollView1.contentOffset = CGPointMake(scrollView.bounds.size.width, 0);
}

1 个答案:

答案 0 :(得分:1)

你永远不会发起UIImageView。你想做这样的事情:

        NSArray *imageViews = [NSArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png", nil];
        CGRect cRect;
        UIImageView *cView; 
        for (int i = 0; i < imageViews.count; i++)
        {
            cView = [[UIImageView alloc] initWithImage:[UIImage imageName:[imageViews objectAtIndex:i]]];
            cRect = cView.frame;
            cRect.origin.x += scrollView1.frame.size.width;
            cView.frame = cRect;
            [scrollView1 addSubview:cView];
            NSLog(@"imageView bounds: %@", NSStringFromCGRect(cView.frame));
        }

编辑: 尝试以上操作,看看是否有任何显示。以下是一些想法:

  1. 您确定没有将imageView设置为0高度/宽度吗?我向循环中输入了一个NSLog语句。它将打印imageView的框架。
  2. 您是否尝试过将图像添加到self.view而不是scrollView1?这将是确保问题与scrollView1的框架无关的好方法。或使用以下内容: NSLog(@“imageView bounds:%@”,NSStringFromCGRect(scrollView1.frame));