动画设计学习ios精彩好文

仿UITableView制作广告轮播器

2016-08-14  本文已影响284人  大么大二郎

最近在做一个项目需要用到一个滚动评论,当然前面的项目中也有许多地方用到了这一控件,虽然网络上关于这种控件的封装思想大致也就那么几种,具体哪几种大家可以去搜索一下,但是对于这种控件的使用,每次使用总是需要重新更改里面的代码,虽说不是一个大的控件,但是,更改起来却也是很烦人,所以自己就依据系统的UITableView的使用方法封装了该控件.废话不多说,程序员看的是实现效果以及代码思想,希望能帮助到需要的朋友.

效果图如下###

2.gif

使用方法如下###

具体代码如下:

@protocol cmtViewDataSource <NSObject>
@required
//返回有多少条
- (NSInteger)numberOfPageInCmtView:(GGCMTView *)cmtView;
//返回每一个滚动的自定义视图,直接返回自定义的UITableViewCell
- (__kindof UITableViewCell *)cmtView:(GGCMTView *)cmtView cellForIndex:(NSInteger)index;
@end

具体代码如下:

@protocol cmtViewDelegate <NSObject>
@optional
//用来告诉点击了某个index的cell
- (void)cmtView:(GGCMTView *)cmtView didSelectIndex:(NSInteger)index;
@end

用来创建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 源码请点击这里

上一篇下一篇

猜你喜欢

热点阅读