您的位置:首页 > 移动开发 > IOS开发

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

2015-01-09 13:27 531 查看
http://blog.csdn.net/u013082522/article/details/19622937

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

以下为.h文件

[objc] view
plaincopy





#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文件

[objc] view
plaincopy





#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 源代码示例如下
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐