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 ...
随机推荐
- YII2.0 后台手动添加用户功能
后台添加管理员用户使用SignupForm类实现 步骤一.复制一份前台frontend/models/SignupForm.php 到后台模型文件夹中 backend/models/SignupFor ...
- 基于Python的飞机大战游戏
前几天决定学Python,上网找了教程看了两天,和C比起来面向对象的特性真的都很便捷,有了类开发各种敌机,子弹什么的都很方便. 在此要感谢开发pygame模块的开发人员,真的很好用(逃 效果图↓ 主函 ...
- 【数据结构】线性表&&顺序表详解和代码实例
喜欢的话可以扫码关注我们的公众号哦,更多精彩尽在微信公众号[程序猿声] 01 预备知识 1.0 什么是线性表? 线性表(List)是零个或者多个数据元素的有限序列. 首先它是一个序列.里面的元素是有顺 ...
- 变分自编码器(Variational auto-encoder,VAE)
参考: https://www.cnblogs.com/huangshiyu13/p/6209016.html https://zhuanlan.zhihu.com/p/25401928 https: ...
- (数据科学学习手札46)Scala中的面向对象
一.简介 在Scala看来,一切皆是对象,对象是Scala的核心,Scala面向对象涉及到class.object.构造器等,本文就将对class中的重点内容进行介绍: 二.Scala中的类 2.1 ...
- 20145234黄斐《信息安全系统设计基础》第七周(Linux命令复习)
已经到了11月,学期过半,而<信息安全系统设计基础>这门课也要到了期中考试了.所以,我在这里,对前半个学期的最基础的知识,做一个复习 复习计划分为两步,本次为Linux命令,下次计划复习g ...
- 成都Uber优步司机奖励政策(3月29日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- libevent学习八(evbuffer)
1.evbuffer以队列的形式管理字节,从尾部添加,从头部取出(FIFO) 2.evbuffer内部存储形式是多个独立的连续内存 接口 //创建和删除 struct evbuffer * ...
- 60帧的丝般顺畅 - QQ飞车手游优化点滴
WeTest 导读 加入项目组的这段时间主要是承担性能优化这块的工作,同时也会去实现一些场景材质.特效材质以及工具.今天就性能优化这块分享一下个人的经验. 设备等级划分 设备等级划分是一切优化,LOD ...
- Qt 解析EXcel文件
写代码需要将excel中的文件导入到数据库中 网上找到以为大神写的,但是当初没有保存,也没有找到 我几乎是原分不动拔下来的,希望大神莫怪 void AddDialog::readExcel(QStri ...