用一个大的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);}效果图