新闻客户端首页图片无限循环滚动展示(可点击触发不同事件)初步封装IOS

来源:互联网 发布:iphone手机数据恢复 编辑:程序博客网 时间:2024/04/28 00:41

http://blog.csdn.net/u013082522/article/details/19622937

此类继承于UIView.引入头文件,初始化对象,设置代理,只需要传入一个盛放图片的数组即可.

以下为.h文件

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @protocol ImageScrollViewDelegate <NSObject>  
  4.   
  5. - (void)tapImageAtIndex:(int)index;  
  6.   
  7. @end  
  8. @interface ImageScrollView : UIView<UIScrollViewDelegate,UIGestureRecognizerDelegate>  
  9.   
  10. @property(nonatomic,assign)id<ImageScrollViewDelegate>delegate;  
  11.   
  12. - (id)initWithFrame:(CGRect)frame imageDataArr:(NSArray *)imageDataArr;  
  13.   
  14. - (void)setPageControlColor:(UIColor *)color;//外界传颜色参数  
  15.   
  16. @end  
以下为.m文件

[objc] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #import "ImageScrollView.h"  
  2. @interface ImageScrollView ()  
  3. {  
  4.     int imageHight;  
  5.       
  6.     int imageWidth;  
  7.       
  8.     int imageCount;  
  9. }  
  10. @property(nonatomic,retain)UIScrollView *imageScrollView;  
  11.   
  12. @property(nonatomic,strong)UIPageControl *pageNumber;  
  13.   
  14. @property(nonatomic,retain)NSArray *imageArray;  
  15.   
  16. @end  
  17. @implementation ImageScrollView  
  18.   
  19. - (id)initWithFrame:(CGRect)frame imageDataArr:(NSArray *)imageDataArr  
  20. {  
  21.     self = [super initWithFrame:frame];  
  22.     if (self) {  
  23.         imageHight = self.frame.size.height-20;  
  24.         imageWidth = self.frame.size.width;  
  25.         self.imageArray = [NSArray arrayWithArray:imageDataArr];  
  26.         imageCount = [self.imageArray count];  
  27.         [self setupUI];  
  28.     }  
  29.     return self;  
  30. }  
  31.   
  32. - (void)setPageControlColor:(UIColor *)color {  
  33.     self.pageNumber.currentPageIndicatorTintColor = color;  
  34. }  
  35.   
  36. - (void)setupUI  
  37. {  
  38.     [self setupScrollView];  
  39.       
  40.     [self setupPageControl];  
  41. }  
  42. - (void)setupScrollView  
  43. {  
  44.     self.imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(00, imageWidth, imageHight)];  
  45.     _imageScrollView.bounces = NO;  
  46.     _imageScrollView.pagingEnabled = YES;  
  47.     _imageScrollView.contentOffset = CGPointMake(imageWidth, 0);  
  48.     _imageScrollView.contentSize = CGSizeMake(imageWidth*(imageCount+2),imageHight);  
  49.     _imageScrollView.showsVerticalScrollIndicator =NO;  
  50.     _imageScrollView.showsHorizontalScrollIndicator = NO;  
  51.     _imageScrollView.userInteractionEnabled = YES;  
  52.     _imageScrollView.backgroundColor = [UIColor redColor];  
  53.     _imageScrollView.delegate = self;  
  54.     [self addSubview:_imageScrollView];  
  55.     //加载图片  
  56.     for (int i = 0; i<imageCount+2; i++) {  
  57.         //加载每一张图片  
  58.         if (i == 0) {  
  59.             //实际上是最后一个位置显示的是第一个图片  
  60.             UIImageView *imageView = [[UIImageView alloc] initWithImage:(UIImage *)[_imageArray objectAtIndex:0]];  
  61.             imageView.frame = CGRectMake(imageWidth*(imageCount+1), 0, imageWidth, imageHight);  
  62.             imageView.userInteractionEnabled = YES;  
  63.             //创建手势  
  64.             UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage)];  
  65.             [imageView addGestureRecognizer:tap];  
  66.             [_imageScrollView addSubview:imageView];  
  67.         }else if (i == imageCount +1){  
  68.             //实际上是第一个位置显示的是最后一个图片  
  69.             UIImageView *imageView = [[UIImageView alloc] initWithImage:(UIImage *)[_imageArray objectAtIndex:(imageCount - 1)]];  
  70.             imageView.frame = CGRectMake(00, imageWidth, imageHight);  
  71.             imageView.userInteractionEnabled = YES;  
  72.             [_imageScrollView addSubview:imageView];  
  73.             //创建手势  
  74.             UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage)];  
  75.             [imageView addGestureRecognizer:tap];  
  76.             [_imageScrollView addSubview:imageView];  
  77.         }else if (0< i <=imageCount){  
  78.                 //正常的图片显示  
  79.             UIImageView *imageView = [[UIImageView alloc] initWithImage:(UIImage *)[_imageArray objectAtIndex:i-1]];  
  80.             imageView.frame = CGRectMake(imageWidth*i, 0, imageWidth, imageHight);  
  81.             imageView.userInteractionEnabled = YES;  
  82.             [_imageScrollView addSubview:imageView];  
  83.             //创建手势  
  84.             UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage)];  
  85.             [imageView addGestureRecognizer:tap];  
  86.             [_imageScrollView addSubview:imageView];  
  87.     }  
  88.   
  89.     }  
  90.   
  91. }  
  92. - (void)tapImage{  
  93.     if (self.delegate != nil) {  
  94.         [self.delegate tapImageAtIndex:_pageNumber.currentPage];  
  95.     }  
  96. }  
  97.   
  98. - (void)setupPageControl  
  99. {  
  100.     //翻页控件  
  101.     _pageNumber = [[UIPageControl alloc]initWithFrame:CGRectMake(imageWidth - 70, imageHight, 7020)];  
  102.     _pageNumber.numberOfPages = imageCount;  
  103.     _pageNumber.currentPage = 0;  
  104.     [_pageNumber addTarget:self action:@selector(pageAction) forControlEvents:UIControlEventTouchUpInside];  
  105.     _pageNumber.pageIndicatorTintColor = [UIColor grayColor];//选择的点的颜色  
  106.     _pageNumber.currentPageIndicatorTintColor = [UIColor blackColor];//已选择的点的颜色  
  107.     [self addSubview:_pageNumber];  
  108.   
  109. }  
  110.   
  111. -(void)pageAction  
  112. {  
  113.     int page = _pageNumber.currentPage;  
  114.     [_imageScrollView setContentOffset:CGPointMake(imageWidth * (page+1), 0)];  
  115. }  
  116. #pragma scrollView代理方法  
  117. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView  
  118. {  
  119.     int currentPage = (_imageScrollView.contentOffset.x - _imageScrollView.frame.size.width  
  120.                        / ([_imageArray count]+2)) / _imageScrollView.frame.size.width + 1;  
  121.     //NSLog(@"%d",currentPage);  
  122.     if (currentPage==0) {  
  123.         [_imageScrollView scrollRectToVisible:CGRectMake(imageWidth*imageCount, 0, imageWidth, imageHight) animated:NO];  
  124.     }  
  125.     else if (currentPage==([_imageArray count]+1)) {  
  126.         //如果是最后+1,也就是要开始循环的第一个  
  127.         [_imageScrollView scrollRectToVisible:CGRectMake(imageWidth, 0, imageWidth, imageHight) animated:NO];  
  128.     }  
  129. }  
  130. - (void)scrollViewDidScroll:(UIScrollView *)sender  
  131. {  
  132.     _pageNumber.currentPage = _imageScrollView.contentOffset.x/imageWidth-1;  
  133. }  
  134.   
  135.   
  136. @end  


http://download.csdn.net/detail/u013082522/6945667  源代码示例如下

2

0 0
原创粉丝点击