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 ...
随机推荐
- CentOS7 宝塔搭配git 实时更新项目源码
上一篇文章 介绍了如何在CentOS7上 搭建GIT环境 详见链接:https://www.cnblogs.com/mverting/p/10206532.html 本章主要介绍git如何和wdcp搭 ...
- 【Hbase三】Java,python操作Hbase
Java,python操作Hbase 操作Hbase python操作Hbase 安装Thrift之前所需准备 安装Thrift 产生针对Python的Hbase的API 启动Thrift服务 执行p ...
- Python-变量与基础数据类型
·变量(variable) 笔记: 变量本质上是一个占位符.变量可以用来存储整数.字符串.列表等.简单的可以理解为一个座位,可以坐老人也可以坐小孩,可以坐男孩,也可以坐女孩. 在python里,标识 ...
- C语言复合数据类型
C语言数据类型非常丰富,其中结构体的使用非常广泛,也有一点复杂,这一讲我们主要学习结构体的使用方法,同时也会学习到联合.枚举以及typedef的使用,因为结构体最为复杂,使用最广,所以我 ...
- 小Hi和小Ho的礼物
题目:小Hi和小Ho的礼物 注:[i.j.p.q]为下标 个人感觉这道题是有一定难度的.读者可以参考一下[四平方和]的解题思路 分析过程下次补上 代码如下: #include <iostream ...
- 使用java实现AES加密
公司最近做agent项目,需要对一些远程重要的请求参数进行加密.加密之前选型,选择了AES,而DES算法加密,容易被破解.网上有很多关于加密的算法的Demo案列,我发现这些Demo在Window平台运 ...
- SpaceVim 发布 v0.8.0
This project exists thanks to all the people who have contributed. The last release v0.7.0 is target ...
- HDU 2242 考研路茫茫——空调教室
考研路茫茫——空调教室 http://acm.hdu.edu.cn/showproblem.php?pid=2242 分析: 树形dp,删边. 代码: #include<cstdio> # ...
- SSM-Spring-23:概念《Spring中的事务是什么?》
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客会详细讲述Spring中的事务,会展开来用语言解释,用于了解概念和准备面试 事务的概念: 一个或者一组 ...
- excell 导入 导出
1.jar包 2.POIUtils工具类 package com.esstglobal.service.utils; import java.io.BufferedInputStream; impor ...