2013-08-02 151 views
0

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

- (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 
+1

如果你有'页页次= Scroller.bounds.size.width',你可能是打算'页页次* Scroller.bounds.size.width' – Rob

+0

哎呦!对不起,谢谢你! –

回答

0

您需要手动将滚动型的contentSize设定为适合所有ImageViews添加的最大矩形。例如:假设你想要向左和向右滚动,并且你有4个视图,每个视图的宽度为100,你在x方向上的偏移量为10。然后将所有的4次到您的scrollView后,你将不得不作出的contentSize如下:

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

这将使scrollView滚动,你会看到所有的意见。

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; 
} 
+0

你能帮我吗?它不是为我工作,我得到一个崩溃。我完全按照你输入的那样完成它 –

+0

没关系谢谢你,我明白了! :)行数更小! –