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

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

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

  1. //  testScrollViewViewController.m   
  2. //  testScrollView   
  3. //  Created by cash on 11-7-4.   
  4. //  Copyright 2011年 xbiii3s@gmail.com. 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代码实现的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2013-03-29 11:06:24

iOS开发滚动视图UIScrol

2011-08-03 17:27:40

iPhone UIScrollVi

2011-08-15 15:44:46

iPhone开发PDF

2011-08-18 16:24:44

iPhone开发图片

2011-07-25 14:44:41

iPhone iPhone开发 截屏

2011-08-16 15:48:37

iPhone开发抓图程序

2011-08-19 10:05:30

iPhone开发

2011-08-18 16:42:07

iPhone应用APNS推送

2011-08-11 17:32:51

iPhone视图

2011-08-10 14:40:23

iPhone动画

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2011-08-16 14:54:12

iphone开发APP

2011-08-19 14:34:03

iPhone开发

2011-07-27 11:19:33

iPhone UITableVie

2012-05-10 14:02:46

jQuery

2011-08-09 17:12:30

iPhoneCFRunLoop

2011-08-12 14:33:06

iPhone缓存文件

2011-08-22 14:12:48

iPhone开发NSTableView

2011-08-15 11:37:20

iPhone开发Mask

2011-08-02 14:23:09

iPhone UIScrollVi 图片
点赞
收藏

51CTO技术栈公众号