新闻客户端首页图片无限循环滚动展示(可点击触发不同事件)初步封装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 源代码示例如下
此类继承于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 源代码示例如下
相关文章推荐
- 新闻客户端首页图片无限循环滚动展示(可点击触发不同事件)初步封装IOS
- 新闻客户端首页图片无限循环滚动展示(可点击触发不同事件)初步封装IOS
- Android开发 图片轮播条Lanner(可无限滚动循环,自定义dots样式位置,延迟时间,滚动时间,点击事件)
- iOS开发中实现新闻图片的无限循环展示的方法
- iOS-利用UIScrollView实现展示图片的无限滚动及自动滚动 - 郭晓
- 图片点击事件,ios有的页面不触发事件
- iOS-利用UIScrollView实现展示图片的无限滚动及自动滚动
- 【转】Android循环滚动广告条的完美实现,封装方便,平滑过渡,从网络加载图片,点击广告进入对应网址
- ViewPager:真正实现图片的无限轮播、自定义指示点和条目点击事件(支持长按暂停滚动)
- Android循环滚动广告条的完美实现,封装方便,平滑过渡,从网络加载图片,点击广告进入对应网址
- IOS 滚动视图加点击事件的封装
- 【iOS_GitHub】banner(广告页/循环滚动广告图片/无限循环自动滚动卡片)
- OCUI界面设计:滚动视图与分页控件结合NSTimer实现图片自动循环与无限滚动展示
- iOS-利用UIScrollView实现展示图片的无限滚动及自动滚动
- Android循环滚动广告条的完美实现,封装方便,平滑过渡,从网络加载图片,点击广告进入对应网址
- Android 滑动切换(首页展示,图片、新闻自动切换,循环切换,自动和手动)
- iOS-利用UIScrollView实现展示图片的无限滚动及自动滚动
- JS控制点击按钮图片向左或向右循环移动滚动效果
- IOS为UIImageView图片添加点击事件
- 一款点击图片进行无限循环的jquery手风琴特效