如何在iOS中的UIScrollView中显示多个图像

Kas*_*hif 6 iphone scrollview imageview

我正在制作一个iPhone应用程序,我需要在UIScrollView中显示一些图像,左右两侧将有两个按钮,用户可以点击按钮,它将显示下一个或上一个图像.同样在底部有一个按钮,当用户点击该按钮时,它将显示我们在scrollview中选择的图像.我想知道如何在滚动视图内显示多个图像,同时选择底部按钮,如何在scrollview中找到哪个图像.

tny*_*lee 5

danielbeard.wordpress.com的一个例子

如果您的图像名称不仅仅是数字:

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    [scrollView setPagingEnabled:YES];
    [scrollView setAlwaysBounceVertical:NO];

    NSArray *imagesArray = [NSArray arrayWithObjects:@"img1.png", @"img2.png", @"img3.png", nil];

    for (int i = 0; i < [imagesArray count]; i++)
    {
        CGFloat xOrigin = i * scrollView.frame.size.width;

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, scrollView.frame.size.width, scrollView.frame.size.height)];
        [imageView setImage:[UIImage imageNamed:[imagesArray objectAtIndex:i]]];
        [imageView setContentMode:UIViewContentModeScaleAspectFit];

        [scrollView addSubview:imageView];
    }

    [scrollView setContentSize:CGSizeMake(scrollView.frame.size.width * [imagesArray count], scrollView.frame.size.height)];
Run Code Online (Sandbox Code Playgroud)