最近在做一个项目需要用到一个滚动评论,当然前面的项目中也有许多地方用到了这一控件,虽然网络上关于这种控件的封装思想大致也就那么几种,具体哪几种大家可以去搜索一下,但是对于这种控件的使用,每次使用总是需要重新更改里面的代码,虽说不是一个大的控件,但是,更改起来却也是很烦人,所以自己就依据系统的UITableView的使用方法封装了该控件.废话不多说,程序员看的是实现效果以及代码思想,希望能帮助到需要的朋友.
效果图如下###
使用方法如下###
首先引入头文件
<pre><code>#import "GGCMTView.h"</code></pre>创建对象
1.创建对象的时候类型有两种一种是:CMTViewHorizontalStyle
另一种是:CMTViewVerticalStyle
其中CMTViewHorizontalStyle创建的是水平滚动类型的,就是平常缩减的广告轮播图
而CMTViewVerticalStyle 创建的则是垂直滚动类型,常见的滚动评论
//创建方法如下
GGCMTView * cmtView = [[GGCMTView alloc]initWithFrame:CGRectMake(0, 100, self.view.width, 200) andCMTViewStyle:CMTViewHorizontalStyle];
//GGCMTView * cmtView = [[GGCMTView alloc]initWithFrame:CGRectMake(0, 100, self.view.width, 200) andCMTViewStyle: CMTViewVerticalStyle];设置对象的属性,数据源,代理,其中属性包括设置滚动间隔,是否允许用户拖动
//用来设置每一次滚动的间隔时间,默认为0.5秒
cmtView.timeInterval = 4.0f;
//用来设置是否允许用户拖动,默认为NO
cmtView.enableUserScroll = YES;
//将该view加到控制器中
[self.view addSubview:cmtView];
//为cmtView设置数据源
cmtView.dataSource = self;
//为cmtView设置代理
cmtView.delegate = self;GGCMTView的数据源cmtViewDataSource是一个协议包含两个接口方法,都是必须实现的,一个是用来返回滚动的内容总共有多少条,另一个是用来返回滚动的自定义视图,利用系统的UITableViewCell来接收,该创建方法与系统UITableViewCell的创建方法保持一致.
具体代码如下:
@protocol cmtViewDataSource <NSObject>
@required
//返回有多少条
- (NSInteger)numberOfPageInCmtView:(GGCMTView *)cmtView;
//返回每一个滚动的自定义视图,直接返回自定义的UITableViewCell
- (__kindof UITableViewCell *)cmtView:(GGCMTView *)cmtView cellForIndex:(NSInteger)index;
@end
- GGCMTView的代理方法cmtViewDelegate只定义了一个可选实现的接口,该接口用来告诉代理点击了某一个Cell,返回点击cell的index
具体代码如下:
@protocol cmtViewDelegate <NSObject>
@optional
//用来告诉点击了某个index的cell
- (void)cmtView:(GGCMTView *)cmtView didSelectIndex:(NSInteger)index;
@end
- GGCMTView暴露出来的对象方法有八个,有五个是关于生命周期的方法,具体每个方法的作用见下面的代码注释
用来创建cmtView CMTViewStyle在创建的时候给定,在此类的属性中包含字段cmtViewStyle 但其仅是可读的,仅用来读取其类型
- (instancetype)initWithFrame:(CGRect)frame andCMTViewStyle:(CMTViewStyle)cmtViewStyle;
此方法用来从缓冲池中取得cell,如果没有,则需要手动创建cell,用法与系统的UITableView保持一致
- (__kindof UITableViewCell *)dequeueReusableCMTViewCellWithIdentifier:(NSString *)identifier;
如果UITableViewCell 有使用xib,则可以使用此方法来进行注册nib,用法与UITableView 保持一致
- (void)registerNib:(UINib *)nib forCMTViewCellReuseIdentifier:(NSString *)identifier;
此方法用来准备开始动画
- (void)prepareScroll;
此方法用来开始动画
- (void)startScroll;
此方法用来停止动画
- (void)stopScroll;
此方法用来暂停动画
- (void)pauseScroll;
此方法用来继续动画
- (void)continueScroll;
由于该类使用的定时器是NSTimer ,所以要求使用者,在controller中视图将要消失的时候调用暂停或者停止方法,而在视图将要出现的时候根据需要,用来开始,或者继续动画,首次开始滚动之前需要调用prepareScroll方法,初始化一下数据,若是想使数据重新从第0页开始,也可以手动调用此方法
具体的使用Demo见下方源码
#import "ViewController.h"
#import "GGCMTView.h"
#import "CustomTableViewCell.h"
#import "UIView+Frame.h"
static NSString * reuseIdentifier = @"cell";
@interface ViewController ()<cmtViewDelegate,cmtViewDataSource>
@property(nonatomic,strong)NSArray * dataArr1;
@property(nonatomic,strong)NSArray * dataArr2;
@property(nonatomic,strong)GGCMTView * cmtView1;
@property(nonatomic,strong)GGCMTView * cmtView2;
@end
@implementation ViewController
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.cmtView1 pauseScroll];
[self.cmtView2 pauseScroll];
}
- (NSArray *)dataArr1{
if (!_dataArr1) {
_dataArr1 = @[@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg",@"6.jpg",@"7.jpg"];
}
return _dataArr1;
}
- (NSArray *)dataArr2{
if (!_dataArr2) {
_dataArr2 = @[@"7.jpg",@"6.jpg",@"5.jpg",@"4.jpg",@"3.jpg",@"2.jpg",@"1.jpg"];
}
return _dataArr2;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
[self testCMTView];
[self testCMTView2];
}
- (void)testCMTView{
GGCMTView * cmtView = [[GGCMTView alloc]initWithFrame:CGRectMake(0, 100, self.view.width, 200) andCMTViewStyle:CMTViewHorizontalStyle];
cmtView.timeInterval = 4.0f;
cmtView.enableUserScroll = YES;
[self.view addSubview:cmtView];
cmtView.dataSource = self;
cmtView.delegate = self;
self.cmtView1 = cmtView;
[cmtView prepareScroll];
[cmtView startScroll];
}
- (void)testCMTView2{
GGCMTView * cmtView = [[GGCMTView alloc]initWithFrame:CGRectMake(0, 330, self.view.width, 200) andCMTViewStyle:CMTViewVerticalStyle];
cmtView.timeInterval = 4.0f;
cmtView.enableUserScroll = YES;
[self.view addSubview:cmtView];
cmtView.dataSource = self;
cmtView.delegate = self;
self.cmtView2 = cmtView;
[cmtView prepareScroll];
[cmtView startScroll];
}
- (UITableViewCell *)cmtView:(GGCMTView *)cmtView cellForIndex:(NSInteger)index{
CustomTableViewCell * cell = [CustomTableViewCell customTableViewCellWithTableView:cmtView];
if (cmtView == self.cmtView1) {
cell.image.image = [UIImage imageNamed:self.dataArr1[index]];
cell.cust.text = self.dataArr1[index];
}else{
cell.image.image = [UIImage imageNamed:self.dataArr2[index]];
cell.cust.text = self.dataArr2[index];
}
return cell;
}
- (void)cmtView:(GGCMTView *)cmtView didSelectIndex:(NSInteger)index{
if (cmtView == self.cmtView1) {
NSLog(@"第%ld张 ===== 名字是 %@",(long)index,self.dataArr1[index]);
}else{
NSLog(@"第%ld张 ===== 名字是 %@",(long)index,self.dataArr2[index]);
}
}
- (NSInteger)numberOfPageInCmtView:(GGCMTView *)cmtView{
if (cmtView == self.cmtView1) {
return self.dataArr1.count;
}
return self.dataArr2.count;
}
@end
总结:作为一个程序员,如果想提升自己,我个人觉得思想最为重要,一个好的解决方案,快速的建模反应,快速的接收速度以及学习能力,是一个好的程序员的必备素质,在我们自己尝试着学习思想之前,那就先模仿系统的吧,学习成本低而且思想也比较成熟了,对吧,希望大家一起努力,另外在源码中,对tableViewCell的复用我这儿处理的不是很好,希望有高人可以指点一二,大家共同进步,谢谢!
Github 源码请点击这里