2012-06-22 44 views
1
-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // add the last image (image4) into the first position 
    [self addImageWithName:@"image4.jpg" atPosition:0]; 

    // add all of the images to the scroll view 
    for (int i = 1; i < 5; i++) 
    { 
     [self addImageWithName:[NSString stringWithFormat:@"image%i.jpg",i] atPosition:i]; 
    } 

    // add the first image (image1) into the last position 
    [self addImageWithName:@"image1.jpg" atPosition:5]; 

    scrollView.contentSize = CGSizeMake(320, 2496);  
    [scrollView scrollRectToVisible:CGRectMake(0,416,320,416) animated:NO]; 
} 

- (void)addImageWithName:(NSString*)imageString atPosition:(int)position { 
    // add image to scroll view 
    UIImage *image = [UIImage imageNamed:imageString]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
    imageView.frame = CGRectMake(0,position*416,320, 416); 
    [scrollView addSubview:imageView]; 
    [imageView release]; 
} 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)sender {  
    NSLog(@"%f",scrollView.contentOffset.y); 
    // The key is repositioning without animation  
    if (scrollView.contentOffset.y == 0) {   
     // user is scrolling to the left from image 1 to image 4   
     // reposition offset to show image 4 that is on the right in the scroll view   
     [scrollView scrollRectToVisible:CGRectMake(0,1664,320,416) animated:NO];  
    }  
    else if (scrollView.contentOffset.y == 2080) {   
     // user is scrolling to the right from image 4 to image 1   
     // reposition offset to show image 1 that is on the left in the scroll view   
     [scrollView scrollRectToVisible:CGRectMake(0,416,320,416) animated:NO];   
    } 
} 
+0

你想要什麼?什麼不行? – Cyrille

+0

什麼讓你不加它們? – Cyrille

+0

好的,我明白了。但是,仍然是什麼阻止你添加兩個滾動視圖到你的XIB?這就是'scrollViewDidEndDecelerating'中'sender'參數的用途:它允許你判斷三者中的哪一個已經完成減速。 – Cyrille

回答

0

我不想重複我的答案,在頁面下面我已經寫了如何創建一個滾動視圖與3張圖片,可在無限滾動兩個水平方向:

https://stackoverflow.com/a/12856174/936957

如果你描述你的問題,你正在努力實現更精確的東西,人們可以幫助更容易。

相關問題