用一個大的UIScrollView裡放一些小的UIScrollView,小的UIScrollView裡面放UIImageView來顯示圖檔,切換圖檔的時候通過UIPageControl來顯示的圖檔的位置,通過切換頁也可以切換圖檔
1.UIScrollView
用到的屬性
@property(nonatomic) CGPoint contentOffset; // default CGPointZero 偏移@property(nonatomic) CGSize contentSize; // default CGSizeZero 可以滾動的區域@property(nonatomic) BOOL bounces; // default YES. if YES, bounces past edge of content and back again 到邊緣之後是否可以拖動
@property(nonatomic,assign)id<UIScrollViewDelegate> delegate; // default nil. weak reference 協定代理
用到的代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView; // any offset changes 滾動發生- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView; // return a view that will be scaled. if delegate returns nil, nothing happens 得到正在放大的視圖
2,UIPageControl
用到的屬性
@property(nonatomic)NSInteger numberOfPages; // default is 0 頁數@property(nonatomic)NSInteger currentPage; // default is 0. value pinned to 0..numberOfPages-1 目前頁
3.簡單實作
.h中添加兩個屬性或成員
{UIScrollView *_scrollView;UIPageControl *_pageControl;}
.m中
- (void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view._scrollView=[[UIScrollViewalloc]initWithFrame:CGRectMake(0,20,320,180)];[_scrollViewsetBackgroundColor:[UIColorgrayColor]];[_scrollViewsetMaximumZoomScale:4.0f];//放大比例[_scrollViewsetMinimumZoomScale:0.1f];//縮小比例[_scrollViewsetShowsVerticalScrollIndicator:false];//隐藏滑動訓示器[_scrollViewsetBounces:NO];//到邊緣之後是否可以持續拉動的效果[_scrollViewsetContentSize:CGSizeMake(2880 ,0)];//大小_scrollView.delegate=self;//設定代理人[_scrollViewsetPagingEnabled:YES];//每次拖動,總是拖到新一頁[_scrollViewsetContentOffset:CGPointMake(640,0)];//添加圖檔for (int i=1; i<10; i++) {UIScrollView *scrollView=[[UIScrollViewalloc]initWithFrame:CGRectMake(i*320-320,0,320, 180)];//建立子滾動視圖NSString *str=[NSStringstringWithFormat:@"%d.jpg",i];UIImageView *imageView=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:str]];//建立imageView添加到子滾動視圖[imageView setFrame:CGRectMake(0,0,320, 180)];[scrollView addSubview:imageView];[imageView release];scrollView.delegate=self;//給子滾動視圖添加代理[scrollView setMaximumZoomScale:1.5f];[scrollView setMinimumZoomScale:0.1f];[_scrollView addSubview:scrollView];//子滾動視圖添加到大的滾動視圖上面[scrollView release];}//添加頁控制_pageControl=[[UIPageControlalloc]initWithFrame:CGRectMake(0,240,320,40 )];[_pageControlsetBackgroundColor:[UIColorgrayColor]];[_pageControladdTarget:selfaction:@selector(pageControlAction:)forControlEvents:UIControlEventValueChanged];[_pageControlsetNumberOfPages:9];[_pageControlsetPageIndicatorTintColor:[UIColorredColor]];[self.viewaddSubview:_pageControl];[_pageControl release];[self.viewaddSubview:_scrollView];[_scrollView release];}//得到目前要放大的視圖-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{NSArray *viewArr=[scrollView subviews];//得到子視圖return [viewArr objectAtIndex:0];}//頁切換動作,換到下一張圖檔-(void)pageControlAction:(id)sender{if ([sender isKindOfClass:[UIPageControlclass]]) {UIPageControl *pagecontrol=(UIPageControl *)sender;NSLog(@"%d",pagecontrol.currentPage);//每次切換到下一張的時候,讓它顯示原來的比例UIScrollView *scrollView=[[_scrollViewsubviews]objectAtIndex:pagecontrol.currentPage];[scrollView setZoomScale:1.0];[_scrollView setContentOffset:CGPointMake(320*pagecontrol.currentPage,0 )];}}//滾動動作發生-(void)scrollViewDidScroll:(UIScrollView *)scrollView{NSLog(@"滾動發生");NSLog(@"content offset=%@",NSStringFromCGPoint(scrollView.contentOffset));int page=scrollView.contentOffset.x/320;[_pageControlsetCurrentPage:page];NSLog(@"page=%d",page);}效果圖