-------------------转载请注明出处------------------------------------------
此类继承于UIView.引入头文件,初始化对象,设置代理,只需要传入一个盛放图片的数组即可.
-------------------转载请注明出处------------------------------------------
以下为.h文件
#import <UIKit/UIKit.h> @protocol ImageScrollViewDelegate <NSObject> - (void)tapImageAtIndex:(int)index; @end @interface ImageScrollView : UIView<UIScrollViewDelegate,UIGestureRecognizerDelegate> @property(nonatomic,assign)id<ImageScrollViewDelegate>delegate; - (id)initWithFrame:(CGRect)frame imageDataArr:(NSArray *)imageDataArr; - (void)setPageControlColor:(UIColor *)color;//外界传颜色参数 @end以下为.m文件
#import "ImageScrollView.h" @interface ImageScrollView () { int imageHight; int imageWidth; int imageCount; } @property(nonatomic,retain)UIScrollView *imageScrollView; @property(nonatomic,strong)UIPageControl *pageNumber; @property(nonatomic,retain)NSArray *imageArray; @end @implementation ImageScrollView - (id)initWithFrame:(CGRect)frame imageDataArr:(NSArray *)imageDataArr { self = [super initWithFrame:frame]; if (self) { imageHight = self.frame.size.height-20; imageWidth = self.frame.size.width; self.imageArray = [NSArray arrayWithArray:imageDataArr]; imageCount = [self.imageArray count]; [self setupUI]; } return self; } - (void)setPageControlColor:(UIColor *)color { self.pageNumber.currentPageIndicatorTintColor = color; } - (void)setupUI { [self setupScrollView]; [self setupPageControl]; } - (void)setupScrollView { self.imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, imageWidth, imageHight)]; _imageScrollView.bounces = NO; _imageScrollView.pagingEnabled = YES; _imageScrollView.contentOffset = CGPointMake(imageWidth, 0); _imageScrollView.contentSize = CGSizeMake(imageWidth*(imageCount+2),imageHight); _imageScrollView.showsVerticalScrollIndicator =NO; _imageScrollView.showsHorizontalScrollIndicator = NO; _imageScrollView.userInteractionEnabled = YES; _imageScrollView.backgroundColor = [UIColor redColor]; _imageScrollView.delegate = self; [self addSubview:_imageScrollView]; //加载图片 for (int i = 0; i<imageCount+2; i++) { //加载每一张图片 if (i == 0) { //实际上是最后一个位置显示的是第一个图片 UIImageView *imageView = [[UIImageView alloc] initWithImage:(UIImage *)[_imageArray objectAtIndex:0]]; imageView.frame = CGRectMake(imageWidth*(imageCount+1), 0, imageWidth, imageHight); imageView.userInteractionEnabled = YES; //创建手势 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage)]; [imageView addGestureRecognizer:tap]; [_imageScrollView addSubview:imageView]; }else if (i == imageCount +1){ //实际上是第一个位置显示的是最后一个图片 UIImageView *imageView = [[UIImageView alloc] initWithImage:(UIImage *)[_imageArray objectAtIndex:(imageCount - 1)]]; imageView.frame = CGRectMake(0, 0, imageWidth, imageHight); imageView.userInteractionEnabled = YES; [_imageScrollView addSubview:imageView]; //创建手势 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage)]; [imageView addGestureRecognizer:tap]; [_imageScrollView addSubview:imageView]; }else if (0< i <=imageCount){ //正常的图片显示 UIImageView *imageView = [[UIImageView alloc] initWithImage:(UIImage *)[_imageArray objectAtIndex:i-1]]; imageView.frame = CGRectMake(imageWidth*i, 0, imageWidth, imageHight); imageView.userInteractionEnabled = YES; [_imageScrollView addSubview:imageView]; //创建手势 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage)]; [imageView addGestureRecognizer:tap]; [_imageScrollView addSubview:imageView]; } } } - (void)tapImage{ if (self.delegate != nil) { [self.delegate tapImageAtIndex:_pageNumber.currentPage]; } } - (void)setupPageControl { //翻页控件 _pageNumber = [[UIPageControl alloc]initWithFrame:CGRectMake(imageWidth - 70, imageHight, 70, 20)]; _pageNumber.numberOfPages = imageCount; _pageNumber.currentPage = 0; [_pageNumber addTarget:self action:@selector(pageAction) forControlEvents:UIControlEventTouchUpInside]; _pageNumber.pageIndicatorTintColor = [UIColor grayColor];//选择的点的颜色 _pageNumber.currentPageIndicatorTintColor = [UIColor blackColor];//已选择的点的颜色 [self addSubview:_pageNumber]; } -(void)pageAction { int page = _pageNumber.currentPage; [_imageScrollView setContentOffset:CGPointMake(imageWidth * (page+1), 0)]; } #pragma scrollView代理方法 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { int currentPage = (_imageScrollView.contentOffset.x - _imageScrollView.frame.size.width / ([_imageArray count]+2)) / _imageScrollView.frame.size.width + 1; //NSLog(@"%d",currentPage); if (currentPage==0) { [_imageScrollView scrollRectToVisible:CGRectMake(imageWidth*imageCount, 0, imageWidth, imageHight) animated:NO]; } else if (currentPage==([_imageArray count]+1)) { //如果是最后+1,也就是要开始循环的第一个 [_imageScrollView scrollRectToVisible:CGRectMake(imageWidth, 0, imageWidth, imageHight) animated:NO]; } } - (void)scrollViewDidScroll:(UIScrollView *)sender { _pageNumber.currentPage = _imageScrollView.contentOffset.x/imageWidth-1; } @end
http://download.csdn.net/detail/u013082522/6945667 源代码示例如下