UIScrollView的循环滚动

- (void)viewDidLoad {      

    scrollView = [[UIScrollView alloc] init];  

    CGRect scrollFrame;  

    scrollFrame.origin.x = 0;       

    scrollFrame.origin.y = 0;        

    scrollFrame.size.width = WIDTH_OF_SCROLL_PAGE;      

    scrollFrame.size.height = HEIGHT_OF_SCROLL_PAGE;      

    scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];      

    scrollView.bounces = YES;       

    scrollView.pagingEnabled = YES;       

    scrollView.delegate = self;       

    scrollView.userInteractionEnabled = YES;       

    slideImages = [[NSMutableArray alloc] init];      

    [slideImages addObject:@"IMG_0116.PNG"];      

    [slideImages addObject:@"IMG_0118.PNG"];      

    [slideImages addObject:@"IMG_0119.PNG"];      

    [slideImages addObject:@"main_bg.png"];      

     //add the last image first       

     UIImageView *imageView = [[UIImageView alloc]  

     initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:([slideImages.count]-1)]]]       

     imageView.frame = CGRectMake(LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);      

     [scrollView addSubview:imageView];         

     for (int i = 0;i<[slideImages.count];i++) {   

            //loop this bit          

            UIImageView *imageView = [[UIImageView alloc]  

            initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:i]]];          

            imageView.frame = CGRectMake((WIDTH_OF_IMAGE * i) + LEFT_EDGE_OFSET + 320, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);          

            [scrollView addSubview:imageView];          

      }      

       //add the first image at the end      

       imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:0]]];      

       imageView.frame = CGRectMake((WIDTH_OF_IMAGE * ([slideImages count] + 1)) + LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);  

        [scrollView addSubview:imageView];       

        [scrollView setContentSize:CGSizeMake(WIDTH_OF_SCROLL_PAGE * ([slideImages count] + 2), HEIGHT_OF_IMAGE)];     

         [scrollView setContentOffset:CGPointMake(0, 0)];      

         [self.view addSubview:scrollView];     

          [self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];       

          [super viewDidLoad];} - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {   

      int currentPage = floor((self.scrollView.contentOffset.x - self.scrollView.frame.size.width 

                             / ([slideImages count]+2)) / self.scrollView.frame.size.width) + 1;     

       if (currentPage==0) {    

              //go last but 1 page          

   [self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE * [slideImages count],0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];  

 } else  

 if (currentPage==([slideImages count]+1)) {  

 //如果是最后+1,也就是要开始循环的第一个          

 [self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];  

}   

}   

你可能感兴趣的:(UIScrollView的循环滚动)