直接上代码
首先是建立一个继承UIView的文件
@interface GFCarouselView : UIView
///数据源
@property (nonatomic,copy) NSArray *dataArray;
.m文件
@implementation GFCarouselView
{
CGFloat _cellHeight;
CGFloat _cellWidth;
UIView *_backViewTotal;//控制循环播放
NSTimer *_timer;//定时器
}
- (void)dealloc{
[_timer invalidate];
_timer = nil;//记得释放定时器
}
- (instancetype)initWithFrame:(CGRect)frame{
if ([super initWithFrame:frame]) {
_cellHeight = frame.size.height/4.;
_cellWidth = frame.size.width;
self.clipsToBounds = YES;
}
return self;
}
- (void)setDataArray:(NSArray *)dataArray{
_dataArray = dataArray;
//清空之前的页面
[_backViewTotal removeFromSuperview];
_backViewTotal = nil;
//创建视图
[self createView];
}
///创建视图
- (void)createView{
if (_dataArray.count==0) {
return ;
}
_backViewTotal = [[UIView alloc] init];
//创建底部视图
UIView *backView = [[UIView alloc] init];
backView.frame = CGRectMake(0, 0, _cellWidth, _cellHeight*_dataArray.count);
[_backViewTotal addSubview:backView];
for (int i=0; i<_dataArray.count; i++) {
UILabel *label = [[UILabel alloc] init];
label.textAlignment = NSTextAlignmentLeft;
label.font = [UIFont systemFontOfSize:15];
label.textColor = [UIColor blackColor];
label.text = [NSString stringWithFormat:@" %@",_dataArray[i]];
label.frame = CGRectMake(0, _cellHeight*i, _cellWidth, _cellHeight);
[backView addSubview:label];
//添加图片
UIImageView *imageView = [[UIImageView alloc] init];
imageView.backgroundColor = [UIColor redColor];
//添加图片
//imageView.image = [UIImage imageNamed:@""];
imageView.frame = CGRectMake(10, 10, _cellHeight-20, _cellHeight-20);
[label addSubview:imageView];
}
if (_dataArray.count>4) {
UIView *backViewTwo = [[UIView alloc] init];
backViewTwo.frame = CGRectMake(0, CGRectGetMaxY(backView.frame), _cellWidth, _cellHeight*_dataArray.count);
[_backViewTotal addSubview:backViewTwo];
for (int i=0; i<_dataArray.count; i++) {
UILabel *label = [[UILabel alloc] init];
label.textAlignment = NSTextAlignmentLeft;
label.font = [UIFont systemFontOfSize:15];
label.textColor = [UIColor blackColor];
label.text = [NSString stringWithFormat:@" %@",_dataArray[i]];
label.frame = CGRectMake(0, _cellHeight*i, _cellWidth, _cellHeight);
[backViewTwo addSubview:label];
//添加图片
UIImageView *imageView = [[UIImageView alloc] init];
imageView.backgroundColor = [UIColor redColor];
//添加图片
//imageView.image = [UIImage imageNamed:@""];
imageView.frame = CGRectMake(10, 10, _cellHeight-20, _cellHeight-20);
[label addSubview:imageView];
}
_backViewTotal.frame = CGRectMake(0, 0, _cellWidth, _cellHeight*_dataArray.count*2);
_timer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(startCarouse) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}else{
_backViewTotal.frame = CGRectMake(0, 0, _cellWidth, _cellHeight*_dataArray.count);
}
[self addSubview:_backViewTotal];
}
///开始轮播
- (void)startCarouse{
CGRect currentFrame = _backViewTotal.frame;
//在此做判断
if (currentFrame.origin.y <= 0 - _cellHeight*_dataArray.count) {
currentFrame.origin.y = 0;
}else{
currentFrame.origin.y -= 1;
}
_backViewTotal.frame = CGRectMake(0, currentFrame.origin.y, _cellWidth, _cellHeight*_dataArray.count*2);
}
第三步在VC文件中引用
#import "ViewController.h"
#import "GFCarouselView.h"//轮播图
@interface ViewController ()
/*注释*/
@property (nonatomic,strong)GFCarouselView *carouseView ;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.carouseView = [[GFCarouselView alloc] initWithFrame:CGRectMake(0, 100, 375, 200)];
_carouseView.dataArray = @[@"2017年1月1日 获得赠送流量10M",@"2017年2月1日 获得赠送流量9M",@"2017年3月1日 获得赠送流量8M",@"2017年4月1日 获得赠送流量7M",@"2017年5月1日 获得赠送流量6M",@"2017年6月1日 获得赠送流量5M",@"2017年7月1日 获得赠送流量4M"];
[self.view addSubview:_carouseView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
@end