iOS-创建UIScrollerView(封装UIScrollerView)
创建继承于UIView的类WJImageScrollView,代码实现如下:
WJImageScrollView.h
#import <UIKit/UIKit.h> /**点击图片block,参数当前图片索引*/
typedef void(^TapImageViewButtonBlock) (NSInteger imageIndex); @interface WJImageScrollView : UIView
/**切换图片的时间间隔,可选,默认3s*/
@property(nonatomic,assign)CGFloat scrollInterval;
/**页面销毁时应该停止定时器,不然无法释放view 类dealloc时调用
[timer invalidate];
timer = nil;
这个方法能解决问题但不明智,有违封装思想,实际使用中优化 */
@property(nonatomic,strong)NSTimer * timer; /**
* 创建轮播器:创建时调用这个方法
*
* @param frame 滚动视图的frame
* @param images 要显示的图片数组
*
* @return 类的对象
*/
+(instancetype)wjImageScrollViewWithFrame:(CGRect)frame
WithImages:(NSArray *)images; /**
*
*
* @param frame 滚动视图的frame
* @param images 要显示的图片数组
*
* @return 类的对象
*/
-(instancetype)initWithFrame:(CGRect)frame
WithIamges:(NSArray *)images; -(void)addTapEventForImageWithBlock:(TapImageViewButtonBlock)block;
@end
WJImageScrollView.m
#import "WJImageScrollView.h" @interface WJImageScrollView ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView * mainScrollView; @property(nonatomic,assign)CGFloat widthView; @property(nonatomic,assign)CGFloat hightView; @property(nonatomic,strong)NSArray * imagesNameArray; @property(nonatomic,assign)NSInteger currentPage; @property(nonatomic,assign)UIViewContentMode imageViewcontentModel; @property(nonatomic,strong)UIPageControl * imageViewPageControl; @property(nonatomic,strong)TapImageViewButtonBlock block; @end
@implementation WJImageScrollView +(instancetype)wjImageScrollViewWithFrame:(CGRect)frame
WithImages:(NSArray *)images{ WJImageScrollView * instance = [[WJImageScrollView alloc]
initWithFrame:frame
WithIamges:images];
return instance; } -(instancetype)initWithFrame:(CGRect)frame
WithIamges:(NSArray *)images{ self = [super initWithFrame:frame];
self.translatesAutoresizingMaskIntoConstraints = NO;
if (self) { /**获取滚动视图的宽度*/
_widthView = frame.size.width; /**获取滚动视图的高度*/
_hightView = frame.size.height; _scrollInterval = ; /**当前显示页面*/
_currentPage = ; _imageViewPageControl = UIViewContentModeScaleToFill; self.clipsToBounds = YES; _imagesNameArray = images; /**初始化滚动视图*/
[self addSubview:self.mainScrollView]; /**创建ImageView*/
[self createImageView]; /**添加imageViewPageControl*/
[self addSubview:self.imageViewPageControl]; /**添加timer*/
[self startTimer];
}
return self;
} -(void)addTapEventForImageWithBlock:(TapImageViewButtonBlock)block{
if (self.block == nil) {
if (block != nil) {
self.block = block;
[self initImageViewButton]; }
}
} -(void)initImageViewButton{
for (int i = ; i <= _imagesNameArray.count; i++) { UIButton * imageBtn = [[UIButton alloc] initWithFrame:CGRectMake(_widthView * i, , _widthView, _hightView)];
imageBtn.tag = + i;
[imageBtn addTarget:self action:@selector(imageBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.mainScrollView addSubview:imageBtn];
}
} #pragma mark - 初始化滚动视图 -(UIScrollView *)mainScrollView{
if (!_mainScrollView) {
_mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(, , _widthView, _hightView)];
_mainScrollView.contentSize = CGSizeMake(_widthView * _imagesNameArray.count, _hightView);
_mainScrollView.pagingEnabled = YES;
_mainScrollView.showsHorizontalScrollIndicator = NO;
_mainScrollView.showsVerticalScrollIndicator = NO;
_mainScrollView.delegate = self;
_mainScrollView.bounces = NO;
}
return _mainScrollView;
} #pragma mark - 创建UIImageView
-(void)createImageView{ for (int i = ; i <= _imagesNameArray.count; i++) { UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(_widthView * i, , _widthView, _hightView)]; if (i == _imagesNameArray.count) { [self addImageToImageViewWithImageView:imageView WithIndex:]; }else{ [self addImageToImageViewWithImageView:imageView WithIndex:i];
} [self.mainScrollView addSubview:imageView]; }
} #pragma mark - 加载网络图片或者本地图片
-(void)addImageToImageViewWithImageView:(UIImageView *)imageView WithIndex:(NSInteger)index{
[imageView setImage:[UIImage imageNamed:_imagesNameArray[index]]];
} -(void)imageBtnClick:(UIButton *)btn{
if (self.block) {
self.block((btn.tag - ) == self.imagesNameArray.count? :(btn.tag - ));
}
} #pragma mark - 添加PageControl
-(UIPageControl *)imageViewPageControl{
if (!_imageViewPageControl) {
_imageViewPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(,_hightView - , _widthView, )];
_imageViewPageControl.numberOfPages = _imagesNameArray.count;
_imageViewPageControl.pageIndicatorTintColor = [UIColor whiteColor];
_imageViewPageControl.currentPageIndicatorTintColor = [UIColor redColor];
_imageViewPageControl.currentPage = _currentPage;
}
return _imageViewPageControl;
} -(void)changeOffset{ if (self.mainScrollView.contentOffset.x / _widthView == _imagesNameArray.count ) { self.imageViewPageControl.currentPage = ;
[self.mainScrollView setContentOffset:CGPointMake(, ) animated:NO]; }else{ [self.mainScrollView setContentOffset:CGPointMake(self.mainScrollView.contentOffset.x + _widthView, ) animated:YES]; }
}
#pragma mark - UIScollerView 代理 -(void)scrollViewDidScroll:(UIScrollView *)scrollView{ NSInteger currentPageNum = (self.mainScrollView.contentOffset.x / _widthView) ;
if (self.mainScrollView.contentOffset.x / _widthView == _imagesNameArray.count ) { self.imageViewPageControl.currentPage = ;
[self.mainScrollView setContentOffset:CGPointMake(, ) animated:NO]; }else{ self.imageViewPageControl.currentPage = currentPageNum; } }
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{ [self colseTimer];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView{ [self startTimer];
}
#pragma mark --- 开始计时
-(void)startTimer{ if (_timer == nil) {
_timer = [NSTimer scheduledTimerWithTimeInterval:_scrollInterval target:self selector:@selector(changeOffset) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop]addTimer:_timer forMode:NSRunLoopCommonModes];
}
} #pragma mark --- 停止计时
-(void)colseTimer
{
if (self.timer) {
[self.timer invalidate];
self.timer = nil;
}
} -(void)dealloc{
NSLog(@"WJImageScrollView释放");
}
@end
使用方法
-(void) addWJImageScrollView{ //获取要显示的位置
CGRect screenFrame = [[UIScreen mainScreen] bounds]; CGRect frame = CGRectMake(, , screenFrame.size.width - , ); NSArray *imageArray = @[@"001.jpg", @"002.jpg", @"003.jpg", @"004.jpg", @"005.jpg"]; //初始化控件
self.imageViewDisplay = [WJImageScrollView wjImageScrollViewWithFrame:frame WithImages:imageArray]; //设定轮播时间
self.imageViewDisplay.scrollInterval = ; //把该视图添加到相应的父视图上
[self.view addSubview:self.imageViewDisplay]; [self.imageViewDisplay addTapEventForImageWithBlock:^(NSInteger imageIndex) {
NSLog(@"点击了------%zd",imageIndex);
}]; }
能解决问题的不好的方法释放view
-(void)dealloc{
NSLog(@"释放");
[self.imageViewDisplay.timer invalidate];
self.imageViewDisplay.timer = nil;
}
iOS-创建UIScrollerView(封装UIScrollerView)的更多相关文章
- [官方教程] [ES4封装教程]1.使用 VMware Player 创建适合封装的虚拟机
[转载处,http://bbs.itiankong.com/] 前言: 首先要明确的一点,系统封装操作的源计算机一般为虚拟计算机(简称虚拟机.VM等),这也是为什么我们要在封装教程的第一章就专门学习虚 ...
- iOS蓝牙原生封装,助力智能硬件开发
代码地址如下:http://www.demodashi.com/demo/12010.html 人工智能自1956年提出以来,一直默默无闻,近年来人工智能的发展得到重视逐渐发展起步,智能硬件.智能手环 ...
- 第九节: 利用RemoteScheduler实现Sheduler的远程控制 第八节: Quartz.Net五大构件之SimpleThreadPool及其四种配置方案 第六节: 六类Calander处理六种不同的时间场景 第五节: Quartz.Net五大构件之Trigger的四大触发类 第三节: Quartz.Net五大构件之Scheduler(创建、封装、基本方法等)和Job(创建、关联
第九节: 利用RemoteScheduler实现Sheduler的远程控制 一. RemoteScheduler远程控制 1. 背景: 在A服务器上部署了一个Scheduler,我们想在B服务器上 ...
- iOS 瀑布流封装
代码地址如下:http://www.demodashi.com/demo/12284.html 一.效果预览 功能描述:WSLWaterFlowLayout 是在继承于UICollectionView ...
- android 仿ios 对话框已封装成工具类
对话框 在android中是一种非经常见的交互提示用户的方式,可是非常多产品狗都叫我们这些做android的仿ios,搞的我们android程序猿非常苦逼,凭什么效果老是仿ios,有没有一点情怀,只是 ...
- JS 函数创建、封装、调用
一.简单函数创建.封装 第三种就是构造函数 function fun(a,b){ this.firstName=a this.lastName=b } var x=new myFun(Jhon,Dav ...
- iOS 下如果存在UIScrollerView 使用UIScreenEdgePanGestureRecognizer实现侧滑效果失效的问题
当你在使用UIScreenEdgePanGestureRecognizer手势实现侧滑的时候,如果后期你导航控制器push出的界面中包含UIScrollerView,这个时候你会发现,侧滑效果无法实现 ...
- iOS创建子工程
实际开发中,我们可能会同时开发好几个端,比如楼主目前开发的家教平台,需要老师端,家长端,助教端三个端.有很多工具方法,或者封装的自定义控件都是可以复用的.我们就可以把公用的代码抽取出去,新建一个工程, ...
- iOS创建界面方法的讨论
以前在入门的时候,找的入门书籍上编写的 demo 都是基于 Storyboards 拖界面的.后来接触公司项目,发现界面都是用纯代码去写复杂的 autoLayout 的.再然后,领导给我发了个 Mas ...
随机推荐
- Lavavel5.5源代码 - RedisQueue是怎么实现
队列的基本功能: 1.立即执行:yes 2.延迟执行:yes 3.保证至少执行一次:yes 4.必须执行且最多执行一次:no 用到的数据结构: list.Sorted sets 延迟执行的机制: 1. ...
- Debian中CodeIgniter+nginx+MariaDB+phpMyAdmin配置
本文不讲述软件安装过程,记述本人在Debia中配置CodeIgniter时遇到的问题及解决方法,希望能够为有需要的人提供帮助. 一.Debian版本及所需的软件 Debian 9.8 stretch ...
- Anaconda下的python如何写入环境变量中
Anaconda是一个非常好的python管理软件,实际使用起来要比直接用python自带的管理工具更好. 若需要将Anaconda下的python.exe添入环境变量中,需要如下设置 如上图所示,需 ...
- 主存和cache的地址映射
cache是一种高速缓冲寄存器,是为解决CPU和主存之间速度不匹配而采用的一项重要技术. 主存与cache的地址映射方式有全相联方式.直接方式和组相联方式三种. 直接映射(directmapping) ...
- C语言变量的初始化
关于C语言变量是否需要初始化的问题.以前西北工业大学的C语言老师说的是,需要初始化,如果不初始化就使用的话,变量的值是以前遗留在内存中的,是不确定的(这只是针对局部变量的).C语言全局变量如果没有初始 ...
- I2C软件模拟协议与电容触摸控制
I2C 与 Touch slide 最近做了一个与触摸滑条相关的测试,利用I2C通讯协议来配置触摸控制芯片的相关寄存器,读取触摸读数,并通过STM Studio动态显示触摸读数的变化过程.这个测试相对 ...
- 解决protobuf import路径的问题
网上关于protobuf import的文章不太详细,有些问题说的不全,比如import时的路径是在哪个目录中搜索的,比如: 我有一个这样的目录结构,我怎么在demo2/protoDemo2.prot ...
- 洛谷P4136 谁能赢呢?
题目描述 小明和小红经常玩一个博弈游戏.给定一个n×n的棋盘,一个石头被放在棋盘的左上角.他们轮流移动石头.每一回合,选手只能把石头向上,下,左,右四个方向移动一格,并且要求移动到的格子之前不能被访问 ...
- P1803 凌乱的yyy
P1803 凌乱的yyy 题目背景 快noip了,yyy很紧张! 题目描述 现在各大oj上有n个比赛,每个比赛的开始.结束的时间点是知道的. yyy认为,参加越多的比赛,noip就能考的越好(假的) ...
- Ruby on Rails Tutorial 第2版 学习笔记
Ruby on Rails Tutorial 第2版 在线阅读:http://railstutorial-china.org/ 英文版:http://ruby.railstutorial.org/ru ...