新闻客户端首页图片无限循环滚动展示(可点击触发不同事件)初步封装IOS
来源:互联网 发布:iphone手机数据恢复 编辑:程序博客网 时间:2024/04/28 00:41
http://blog.csdn.net/u013082522/article/details/19622937
此类继承于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
- #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 源代码示例如下
- 顶
- 2
0 0
- 新闻客户端首页图片无限循环滚动展示(可点击触发不同事件)初步封装IOS
- 新闻客户端首页图片无限循环滚动展示(可点击触发不同事件)初步封装IOS
- 新闻客户端首页图片无限循环滚动展示(可点击触发不同事件)初步封装IOS
- Android开发 图片轮播条Lanner(可无限滚动循环,自定义dots样式位置,延迟时间,滚动时间,点击事件)
- 图片新闻的无限滚动
- 点击可上下左右滚动的新闻图片列表
- 新闻广告图片滚动展示
- 触发点击图片事件
- Prototype插件之无限循环滚动新闻
- Android 滑动切换(首页展示,图片、新闻自动切换,循环切换,自动和手动)
- iOS-工具类封装-图片轮播无限循环
- 网易新闻首页的实现,可以添加,移除标签,自动滚动图片,无限左右切换图片
- 模拟js的首页动态推荐页面 Gallery 自动播放 无限循环 指示器显示 点击事件
- iOS 无限循环滚动视图
- iOS-利用UIScrollView实现展示图片的无限滚动及自动滚动
- iOS-利用UIScrollView实现展示图片的无限滚动及自动滚动
- iOS-利用UIScrollView实现展示图片的无限滚动及自动滚动 - 郭晓
- IOS 滚动视图加点击事件的封装
- 黑马程序员——java基础——文件File
- 捷易通话费充值招代理加QQ398677951
- postgresql 备忘
- 刘奇-豌豆荚分布式redis的设计与实现
- 口吐白沫是癫痫吗
- 新闻客户端首页图片无限循环滚动展示(可点击触发不同事件)初步封装IOS
- 音频解码器madplay移植
- PHP学习问题
- 通过common-emai类库发送普通邮件和带有附件的邮件的完整示例
- 2015年薪酬大涨的15个IT岗位
- OSGi 框架嵌入 Http 服务器的运行环境配置
- android之layout_toLeftOf和layout_toRightOf出现的错误
- python 安装jpeg支持
- UVA 902 Password Search (字符串)