如何使用UIScrollView在滚动时显示不同的图像

时间:2012-04-18 10:47:29

标签: iphone

我正在使用滚动视图创建一个小应用程序。我已将图像加载到我想要显示的数据库中。我只能显示最后一张图像,而且图像也没有水平滚动。每个图像都有一个图像名称,我想在标签中显示,因为图像在滚动时会发生变化,但我不能。

//Code to display images in imageview placed on scrollview
-(void)imageDisplay
{
    //setup the scrollview for multiple images and add it to the view controller
    [scrollView setBackgroundColor:[UIColor blackColor]];
    [scrollView setCanCancelContentTouches:NO];
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;

    scrollView.clipsToBounds = YES;  //default is NO, we want to restrict drawing within our scrollview
    scrollView.scrollEnabled = YES;
    scrollView.pagingEnabled = YES;

    //load all images from database and add them to scroll view
    NSUInteger imageCount;
    int counter = 0;
    for(imageCount = 0; imageCount < fetchedImageCount; imageCount++)
    {
        dictResults = [fetchedObjects objectAtIndex: imageCount];

        imgPath = [dictResults valueForKey:@"imageData"];
        NSLog(@"image path%@", imgPath);
        imgData = [NSData dataWithContentsOfFile: imgPath];
        image1 = [UIImage imageWithData:imgData];

        imageView = [[UIImageView alloc] initWithImage:image1];

        if(imageView == nil)
            break;

        CGRect rect = imageView.frame;
        rect.size.height =199.0;
        rect.size.width = 280.0;
        imageView.frame = rect;
        counter = counter + 1;
        imageView.tag = counter;

        lblImageTitle.text = [dictResults valueForKey:@"imageName"];
        [scrollView addSubview:imageView];
//        [imageView release];
    }
    [self layoutScrollImages];

}


//Code to assign imageview to scroll view and setup
-(void)layoutScrollImages
{
    UIImageView *view = nil;
    NSArray *subViews = [scrollView subviews];

    //reposition all image subviews in a horizontal serial fashion;
    CGFloat currentXLocation = 0;
    for(view in subViews)
    {
        NSLog(@"subview%d", [[scrollView subviews] count]);
        if([view isKindOfClass:[UIImageView class]] && view.tag > 0)
        {
            NSLog(@"view.tag %d", view.tag );
            CGRect frame = view.frame;
            frame.origin = CGPointMake(currentXLocation, 0);
            view.frame = frame;
            currentXLocation += 280.0;
        }
    }
    //set content size so it can be scrollable
    [scrollView setContentSize:CGSizeMake(fetchedImageCount*280.0, [scrollView bounds].size.height)];
}

提前感谢您的帮助

0 个答案:

没有答案