最近在做一個項(xiàng)目需要用到一個滾動評論,當(dāng)然前面的項(xiàng)目中也有許多地方用到了這一控件,雖然網(wǎng)絡(luò)上關(guān)于這種控件的封裝思想大致也就那么幾種,具體哪幾種大家可以去搜索一下,但是對于這種控件的使用,每次使用總是需要重新更改里面的代碼,雖說不是一個大的控件,但是,更改起來卻也是很煩人,所以自己就依據(jù)系統(tǒng)的UITableView的使用方法封裝了該控件.廢話不多說,程序員看的是實(shí)現(xiàn)效果以及代碼思想,希望能幫助到需要的朋友.
效果圖如下###
使用方法如下###
首先引入頭文件
<pre><code>#import "GGCMTView.h"</code></pre>創(chuàng)建對象
1.創(chuàng)建對象的時候類型有兩種一種是:CMTViewHorizontalStyle
另一種是:CMTViewVerticalStyle
其中CMTViewHorizontalStyle創(chuàng)建的是水平滾動類型的,就是平痴觯縮減的廣告輪播圖
而CMTViewVerticalStyle 創(chuàng)建的則是垂直滾動類型,常見的滾動評論
//創(chuàng)建方法如下
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];設(shè)置對象的屬性,數(shù)據(jù)源,代理,其中屬性包括設(shè)置滾動間隔,是否允許用戶拖動
//用來設(shè)置每一次滾動的間隔時間,默認(rèn)為0.5秒
cmtView.timeInterval = 4.0f;
//用來設(shè)置是否允許用戶拖動,默認(rèn)為NO
cmtView.enableUserScroll = YES;
//將該view加到控制器中
[self.view addSubview:cmtView];
//為cmtView設(shè)置數(shù)據(jù)源
cmtView.dataSource = self;
//為cmtView設(shè)置代理
cmtView.delegate = self;GGCMTView的數(shù)據(jù)源cmtViewDataSource是一個協(xié)議包含兩個接口方法,都是必須實(shí)現(xiàn)的,一個是用來返回滾動的內(nèi)容總共有多少條,另一個是用來返回滾動的自定義視圖,利用系統(tǒng)的UITableViewCell來接收,該創(chuàng)建方法與系統(tǒng)UITableViewCell的創(chuàng)建方法保持一致.
具體代碼如下:
@protocol cmtViewDataSource <NSObject>
@required
//返回有多少條
- (NSInteger)numberOfPageInCmtView:(GGCMTView *)cmtView;
//返回每一個滾動的自定義視圖,直接返回自定義的UITableViewCell
- (__kindof UITableViewCell *)cmtView:(GGCMTView *)cmtView cellForIndex:(NSInteger)index;
@end
- GGCMTView的代理方法cmtViewDelegate只定義了一個可選實(shí)現(xiàn)的接口,該接口用來告訴代理點(diǎn)擊了某一個Cell,返回點(diǎn)擊cell的index
具體代碼如下:
@protocol cmtViewDelegate <NSObject>
@optional
//用來告訴點(diǎn)擊了某個index的cell
- (void)cmtView:(GGCMTView *)cmtView didSelectIndex:(NSInteger)index;
@end
- GGCMTView暴露出來的對象方法有八個,有五個是關(guān)于生命周期的方法,具體每個方法的作用見下面的代碼注釋
用來創(chuàng)建cmtView CMTViewStyle在創(chuàng)建的時候給定,在此類的屬性中包含字段cmtViewStyle 但其僅是可讀的,僅用來讀取其類型
- (instancetype)initWithFrame:(CGRect)frame andCMTViewStyle:(CMTViewStyle)cmtViewStyle;
此方法用來從緩沖池中取得cell,如果沒有,則需要手動創(chuàng)建cell,用法與系統(tǒng)的UITableView保持一致
- (__kindof UITableViewCell *)dequeueReusableCMTViewCellWithIdentifier:(NSString *)identifier;
如果UITableViewCell 有使用xib,則可以使用此方法來進(jìn)行注冊nib,用法與UITableView 保持一致
- (void)registerNib:(UINib *)nib forCMTViewCellReuseIdentifier:(NSString *)identifier;
此方法用來準(zhǔn)備開始動畫
- (void)prepareScroll;
此方法用來開始動畫
- (void)startScroll;
此方法用來停止動畫
- (void)stopScroll;
此方法用來暫停動畫
- (void)pauseScroll;
此方法用來繼續(xù)動畫
- (void)continueScroll;
由于該類使用的定時器是NSTimer ,所以要求使用者,在controller中視圖將要消失的時候調(diào)用暫颓担或者停止方法,而在視圖將要出現(xiàn)的時候根據(jù)需要,用來開始,或者繼續(xù)動畫,首次開始滾動之前需要調(diào)用prepareScroll方法,初始化一下數(shù)據(jù),若是想使數(shù)據(jù)重新從第0頁開始,也可以手動調(diào)用此方法
具體的使用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
總結(jié):作為一個程序員,如果想提升自己,我個人覺得思想最為重要,一個好的解決方案,快速的建模反應(yīng),快速的接收速度以及學(xué)習(xí)能力,是一個好的程序員的必備素質(zhì),在我們自己嘗試著學(xué)習(xí)思想之前,那就先模仿系統(tǒng)的吧,學(xué)習(xí)成本低而且思想也比較成熟了,對吧,希望大家一起努力,另外在源碼中,對tableViewCell的復(fù)用我這兒處理的不是很好,希望有高人可以指點(diǎn)一二,大家共同進(jìn)步,謝謝!
Github 源碼請點(diǎn)擊這里