新闻客户端首页图片无限循环滚动展示(可点击触发不同事件)初步封装IOS
来源:互联网 发布:数据库规范化分析 编辑:程序博客网 时间:2024/04/28 17:56
-------------------转载请注明出处------------------------------------------
此类继承于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 源代码示例如下
2 0
- 新闻客户端首页图片无限循环滚动展示(可点击触发不同事件)初步封装IOS
- 新闻客户端首页图片无限循环滚动展示(可点击触发不同事件)初步封装IOS
- 新闻客户端首页图片无限循环滚动展示(可点击触发不同事件)初步封装IOS
- Android开发 图片轮播条Lanner(可无限滚动循环,自定义dots样式位置,延迟时间,滚动时间,点击事件)
- 图片新闻的无限滚动
- 点击可上下左右滚动的新闻图片列表
- 新闻广告图片滚动展示
- 触发点击图片事件
- Prototype插件之无限循环滚动新闻
- Android 滑动切换(首页展示,图片、新闻自动切换,循环切换,自动和手动)
- iOS-工具类封装-图片轮播无限循环
- 网易新闻首页的实现,可以添加,移除标签,自动滚动图片,无限左右切换图片
- 模拟js的首页动态推荐页面 Gallery 自动播放 无限循环 指示器显示 点击事件
- iOS 无限循环滚动视图
- iOS-利用UIScrollView实现展示图片的无限滚动及自动滚动
- iOS-利用UIScrollView实现展示图片的无限滚动及自动滚动
- iOS-利用UIScrollView实现展示图片的无限滚动及自动滚动 - 郭晓
- IOS 滚动视图加点击事件的封装
- 2010C820基于GIS的大气污染物扩散模拟分析系统
- 内存对齐(计算结构体大小)方法
- Ubuntu下root帐号使用Chrome
- "*.py"和"*.pyw"的区别
- Android开发学习笔记:浅谈WebView
- 新闻客户端首页图片无限循环滚动展示(可点击触发不同事件)初步封装IOS
- 安全厂商推出“XP用户解决方案” 更加贴合用户使用习惯
- POJ 1679 The Unique MST
- 计算机理论知识与实际应用
- redhat安装VMware tools的方法
- 强大的vim配置文件,让编程更随意
- mongodb 数据导入导出若干问题
- java synchronized同步静态方法和同步非静态方法的异同(淘宝面试问过此问题)
- Shell脚本学习之sed详解