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 に答える