最近在做一個項目需要用到一個滾動評論,當然前面的項目中也有許多地方用到了這一控件,雖然網絡上關于這種控件的封裝思想大致也就那么幾種,具體哪幾種大家可以去搜索一下,但是對于這種控件的使用,每次使用總是需要重新更改里面的代碼,雖說不是一個大的控件,但是,更改起來卻也是很煩人,所以自己就依據系統的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 源碼請點擊這里