[转]iPhone开发应用中UIScrollView代码实现循环滚动

iPhone开发应用中循环滚动一个UIScrollView代码实现是本文要介绍的内容,主要是介绍UIScrollView来实现循环滚动的案例,来看详细内容。

  
  
  
  
  1. //  testScrollViewViewController.m   
  2. //  testScrollView   
  3. //  Created by cash on 11-7-4.   
  4. //  Copyright 2011年 [email protected]. All rights reserved.    
  5. #import "testScrollViewViewController.h"   
  6. @implementation testScrollViewViewController    
  7. @synthesize scrollView, slideImages;   
  8. #define WIDTH_OF_SCROLL_PAGE 320   
  9. #define HEIGHT_OF_SCROLL_PAGE 460   
  10. #define WIDTH_OF_IMAGE 320 #define HEIGHT_OF_IMAGE 460   
  11. #define LEFT_EDGE_OFSET 0    
  12. - (void)viewDidLoad {       
  13.     scrollView = [[UIScrollView alloc] init];   
  14.     CGRect scrollFrame;   
  15.     scrollFrame.origin.x = 0;       
  16.     scrollFrame.origin.y = 0;         
  17.     scrollFrame.size.width = WIDTH_OF_SCROLL_PAGE;       
  18.     scrollFrame.size.height = HEIGHT_OF_SCROLL_PAGE;       
  19.     scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];       
  20.     scrollView.bounces = YES;       
  21.     scrollView.pagingEnabled = YES;       
  22.     scrollView.delegate = self;       
  23.     scrollView.userInteractionEnabled = YES;       
  24.     slideImages = [[NSMutableArray alloc] init];       
  25.     [slideImages addObject:@"IMG_0116.PNG"];       
  26.     [slideImages addObject:@"IMG_0118.PNG"];       
  27.     [slideImages addObject:@"IMG_0119.PNG"];       
  28.     [slideImages addObject:@"main_bg.png"];       
  29.      //add the last image first        
  30.      UIImageView *imageView = [[UIImageView alloc]   
  31.      initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:([slideImages count]-1)]]]        
  32.      imageView.frame = CGRectMake(LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);       
  33.      [scrollView addSubview:imageView];       
  34.      [imageView release];      
  35.      for (int i = 0;i<[slideImages count];i++) {    
  36.             //loop this bit           
  37.             UIImageView *imageView = [[UIImageView alloc]   
  38.             initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:i]]];           
  39.             imageView.frame = CGRectMake((WIDTH_OF_IMAGE * i) + LEFT_EDGE_OFSET + 320, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);           
  40.             [scrollView addSubview:imageView];           
  41.             [imageView release];   
  42.       }       
  43.        //add the first image at the end       
  44.        imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[slideImages objectAtIndex:0]]];       
  45.        imageView.frame = CGRectMake((WIDTH_OF_IMAGE * ([slideImages count] + 1)) + LEFT_EDGE_OFSET, 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);   
  46.         [scrollView addSubview:imageView];      
  47.         [imageView release];       
  48.         [scrollView setContentSize:CGSizeMake(WIDTH_OF_SCROLL_PAGE * ([slideImages count] + 2), HEIGHT_OF_IMAGE)];      
  49.          [scrollView setContentOffset:CGPointMake(0, 0)];       
  50.          [self.view addSubview:scrollView];      
  51.           [self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];        
  52.           [super viewDidLoad];} - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {    
  53.       int currentPage = floor((self.scrollView.contentOffset.x - self.scrollView.frame.size.width 
  54. / ([slideImages count]+2)) / self.scrollView.frame.size.width) + 1;      
  55.        if (currentPage==0) {    
  56.               //go last but 1 page           
  57.    [self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE * [slideImages count],0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];   
  58.  } else   
  59.  if (currentPage==([slideImages count]+1)) {   
  60.  //如果是最后+1,也就是要开始循环的第一个           
  61.  [self.scrollView scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];   
  62. }   
  63. }   
  64. - (void)didReceiveMemoryWarning {     
  65.   // Releases the view if it doesn't have a superview.       
  66.   [super didReceiveMemoryWarning];       
  67.   // Release any cached data, images, etc that aren't in use.   
  68.   }   
  69.  - (void)viewDidUnload {     
  70.    // Release any retained subviews of the main view.      
  71.     // e.g. self.myOutlet = nil;   
  72.  }    
  73.  - (void)dealloc {      
  74.   [scrollView release];       
  75.   [slideImages release];       
  76.   [super dealloc];  
  77. }  
  78. @end

你可能感兴趣的:([转]iPhone开发应用中UIScrollView代码实现循环滚动)