0

我正在使用此代码显示滚动图像,但我只得到一个白屏。我拖放uiscrollview,创建iboutlet,合成它,其余代码在这里

@synthesize scrollView1;

const CGFloat kScrollObjHeight  = 199.0;
const CGFloat kScrollObjWidth   = 280.0;
const NSUInteger kNumImages     = 5;

- (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((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    scrollView1 = [[UIScrollView alloc]init];

    bgArray = [NSArray arrayWithObjects:@"Bg", @"Bg1.png", @"Bg2.png", @"Bg3.png", @"Bg4.png", @"Bg5.png", @"Bg6.png", @"Bg7.png", @"Bg8.png", nil];

    [scrollView1 setBackgroundColor:[UIColor blackColor]];
    [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 = 0; i < [bgArray count]; i++)
    {
        NSString *imageName = [NSString stringWithFormat:@"%@", [bgArray objectAtIndex: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 = 199.0;
        rect.size.width = 280.0;
        imageView.frame = rect;
        imageView.tag = i;  // tag our images for later use when we place them in serial fashion
        [scrollView1 addSubview:imageView];
        [scrollView1 setShowsHorizontalScrollIndicator:NO];
        [scrollView1 setShowsVerticalScrollIndicator:NO];
    }

    [self layoutScrollImages];

}
4

1 回答 1

3

我想如果你是UIScrollView从代码创建的

scrollView1 = [[UIScrollView alloc]init];

然后请在视图中添加 scrollView1 示例:-[self.view addSubview:scrollView1];

如果您是从 nib 创建 UIScrollView 则不要使用此代码

scrollView1 = [[UIScrollView alloc]init];

解决你的问题

于 2012-08-17T08:15:04.647 回答