2012-06-22 2 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 개의 이미지가있는 scrollView를 작성하는 방법을 작성했습니다. 모두 가로 방향 :

당신이 당신의 문제와 당신이 더 정확하게 달성하기 위해 노력하고있다을 설명하는 경우

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

, 사람들은 쉽게 할 수 있습니다.

관련 문제