0

我已经设置了一个 UIScrollView 并加载了图片并将其设置为与图片之间有一些偏移的视图相等。有人可以解释我可能做错了什么吗?它可以很好地显示第一张图片,但不会让我左右滚动查看下一张。我需要一个带有新 XCode 的手势识别器来完成这项工作吗?

 - (void)viewDidLoad
    {
        [super viewDidLoad];

        int PageCount = 3;

        UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
        Scroller.backgroundColor = [UIColor clearColor];
        Scroller.pagingEnabled = YES;
        Scroller.contentSize = CGSizeMake(PageCount = Scroller.bounds.size.width, Scroller.bounds.size.height);

        CGRect ViewSize = Scroller.bounds;

        UIImageView *ImgView = [[UIImageView alloc] initWithFrame:ViewSize];
        [ImgView setImage:[UIImage imageNamed:@"1.png"]];
        [Scroller addSubview:ImgView];

        ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);

        UIImageView *ImgView2 = [[UIImageView alloc] initWithFrame:ViewSize];
        [ImgView2 setImage:[UIImage imageNamed:@"2.png"]];
        [Scroller addSubview:ImgView2];

        ViewSize = CGRectOffset(ViewSize, Scroller.bounds.size.width, 0);

        UIImageView *ImgView3 = [[UIImageView alloc] initWithFrame:ViewSize];
        [ImgView3 setImage:[UIImage imageNamed:@"3.png"]];
        [Scroller addSubview:ImgView3];

        [self.view addSubview:Scroller];

    }

    @end
4

2 回答 2

0

尝试这个

int PageCount = 3;
NSMutableArray *arrImageName =[[NSMutableArray alloc]initWithObjects:@"1.png",@"2.png",@"3.png", nil];
UIScrollView *Scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
Scroller.scrollEnabled=YES;
Scroller.backgroundColor = [UIColor clearColor];
Scroller.pagingEnabled = YES;
[self.view addSubview:Scroller];
int width=Scroller.frame.size.width;
int xPos=0;
for (int i=0; i<PageCount; i++)
{
    UIImageView *ImgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPos, 0, Scroller.frame.size.width, Scroller.frame.size.height)];
    [ImgView setImage:[UIImage imageNamed:[arrImageName objectAtIndex:i]]];
    [Scroller addSubview:ImgView];
    Scroller.contentSize = CGSizeMake(width, 0);
    width +=Scroller.frame.size.width;
    xPos  +=Scroller.frame.size.width;
}
于 2013-08-02T05:53:44.983 回答
0

您需要手动将contentSizeScrollView 设置为适合ImageViews您添加的所有内容的最大矩形。例如:假设您想要左右滚动,并且您有 4 个视图,每个视图的宽度为 100,并且您在 x 方向上的偏移量为 10。然后在将所有 4 个视图添加到您的之后scrollView,您必须将 contentSize 设置为如下:

scrollView.contentSize = CGSizeMake( 10 + (100 + 10)*4 , scrollView.contentSize.y );

这将使scrollView可滚动,您将看到所有视图。

于 2013-08-02T05:03:37.380 回答