Ios 通过分页将图像水平添加到UIScrollView

Ios 通过分页将图像水平添加到UIScrollView,ios,iphone,Ios,Iphone,我尝试使用页面在水平行中显示图像,每个页面包含4个图像,可能被一行的小空间隔开,或者没有空间。当用户水平滚动时,接下来的四幅图像显示在视图中心。但使用此代码: UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, screenHeight/4, screenWidth, screenHeight/4)]; scroll.showsHorizontalScrollIndicator = YES;

我尝试使用页面在水平行中显示图像,每个页面包含4个图像,可能被一行的小空间隔开,或者没有空间。当用户水平滚动时,接下来的四幅图像显示在视图中心。但使用此代码:

 UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, screenHeight/4, screenWidth, screenHeight/4)];
    scroll.showsHorizontalScrollIndicator = YES;
    scroll.showsVerticalScrollIndicator=NO;
    scroll.pagingEnabled = YES;
    NSInteger numberOfViews = 3;
    int k=1;
    for (int i = 0; i < numberOfViews; i++) {
        CGFloat xOrigin = i * self.view.frame.size.width;
        UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
        awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
        [scroll addSubview:awesomeView];
        for (int j=0; j<4; j++) {
            NSString *imageName = [NSString stringWithFormat:@"image%d.png", k];
            UIImage *image = [UIImage imageNamed:imageName];
            UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
            CGRect imageFrame=CGRectMake(0, k*screenHeight/4, screenWidth/5, screenHeight/5);
            imageView.frame=imageFrame;
            [awesomeView addSubview:imageView];
            k++;
        }
UIScrollView*scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0,屏幕高度/4,屏幕宽度,屏幕高度/4)];
scroll.showrizontalscrolindicator=YES;
scroll.showsVerticalScrollIndicator=否;
scroll.paginEnabled=是;
NSInteger numberOfViews=3;
int k=1;
对于(int i=0;i
CGRect imageFrame=CGRectMake(0, k*screenHeight/4, screenWidth/5, screenHeight/5);
您正在弄乱您的imageView框架(在UIView-awesomeView内部)

您的
k
变量是1,2,3,4

所以我假设你有4个垂直的图像视图,下一个“列”有四个图像在左边,右边

另外,您没有在任何地方为
滚动视图设置
contentSize
,因此,您可能看不到此行中的第二个“列”:

CGRect imageFrame=CGRectMake(0, k*screenHeight/4, screenWidth/5, screenHeight/5);
您正在弄乱您的imageView框架(在UIView-awesomeView内部)

您的
k
变量是1,2,3,4

所以我假设你有4个垂直的图像视图,下一个“列”有四个图像在左边,右边

另外,您没有在任何地方为
滚动视图设置
contentSize
,因此您可能看不到第二个“列”