| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- //
- // GBTopLineView.m
- // 淘宝垂直跑马灯广告
- //
- // Created by 张国兵 on 15/8/28.
- // Copyright (c) 2015年 zhangguobing. All rights reserved.
- //
- #import "GBTopLineView.h"
- @interface GBTopLineView(){
-
- NSTimer *_timer; //定时器
- int count;
- int flag; //标识当前是哪个view显示(currentView/hidenView)
- NSMutableArray *_dataArr;
- }
- @property (nonatomic,strong) UIView *currentView; //当前显示的view
- @property (nonatomic,strong) UIView *hidenView; //底部藏起的view
- @property (nonatomic,strong) UILabel *currentLabel;
- @property (nonatomic,strong) UILabel *currentLabel1;
- @property (nonatomic,strong) UIButton *currentBtn;
- @property (nonatomic,strong) UIButton *currentBtn1;
- @property (nonatomic,strong) UIButton *hidenBtn;
- @property (nonatomic,strong) UIButton *hidenBtn1;
- @property (nonatomic,strong) UILabel *hidenLabel;
- @property (nonatomic,strong) UILabel *hidenLabel1;
- @end
- @implementation GBTopLineView
- - (id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- [self createUI];
- }
- return self;
- }
- - (void)createUI
- {
- count = 0;
- flag = 0;
-
- self.layer.masksToBounds = YES;
-
- //创建定时器
- [self createTimer];
- [self createCurrentView];
- [self createHidenView];
-
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dealTap:)];
- [self addGestureRecognizer:tap];
- //改进
- UILongPressGestureRecognizer*longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(dealLongPress:)];
- [self addGestureRecognizer:longPress];
-
-
- }
- - (void)setVerticalShowDataArr:(NSMutableArray *)dataArr
- {
- _dataArr = dataArr;
- NSLog(@"dataArr-->%@",dataArr);
- GBTopLineViewModel *model = _dataArr[count];
- // [self.currentBtn setTitle:model.type forState:UIControlStateNormal];
- // [self.currentBtn1 setTitle:model.type forState:UIControlStateNormal];
- self.currentLabel.text = model.titleArray[0];
- self.currentLabel1.text = model.titleArray[1];
- }
- -(void)dealLongPress:(UILongPressGestureRecognizer*)longPress{
-
- if(longPress.state==UIGestureRecognizerStateEnded){
-
- _timer.fireDate=[NSDate distantPast];
-
-
- }
- if(longPress.state==UIGestureRecognizerStateBegan){
-
- _timer.fireDate=[NSDate distantFuture];
- }
-
-
-
-
- }
- - (void)dealTap:(UITapGestureRecognizer *)tap
- {
- self.clickBlock(count);
- }
- - (void)createTimer
- {
- _timer=[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(dealTimer) userInfo:nil repeats:YES];
- }
- #pragma mark - 跑马灯操作
- -(void)dealTimer
- {
- count++;
- if (count == _dataArr.count) {
- count = 0;
- }
-
- if (flag == 1) {
- GBTopLineViewModel*currentModel = _dataArr[count];
- [self.currentBtn setTitle:currentModel.type forState:UIControlStateNormal];
- [self.currentBtn1 setTitle:currentModel.type forState:UIControlStateNormal];
- self.currentLabel.text = currentModel.titleArray[0];
- self.currentLabel1.text = currentModel.titleArray[1];
-
- }
-
- if (flag == 0) {
- GBTopLineViewModel *hienModel = _dataArr[count];
- [self.hidenBtn setTitle:hienModel.type forState:UIControlStateNormal];
- [self.hidenBtn1 setTitle:hienModel.type forState:UIControlStateNormal];
- self.hidenLabel.text = hienModel.titleArray[0];
- self.hidenLabel1.text = hienModel.titleArray[1];
- }
-
-
- if (flag == 0) {
- [UIView animateWithDuration:0.5 animations:^{
- self.currentView.frame = CGRectMake(0, -self.frame.size.height, self.frame.size.width, self.frame.size.height);
- } completion:^(BOOL finished) {
- flag = 1;
- self.currentView.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, self.frame.size.height);
- }];
- [UIView animateWithDuration:0.5 animations:^{
- self.hidenView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
- } completion:^(BOOL finished) {
-
- }];
- }else{
-
- [UIView animateWithDuration:0.5 animations:^{
- self.hidenView.frame = CGRectMake(0, -self.frame.size.height, self.frame.size.width, self.frame.size.height);
- } completion:^(BOOL finished) {
- flag = 0;
- self.hidenView.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, self.frame.size.width);
- }];
- [UIView animateWithDuration:0.5 animations:^{
- self.currentView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
- } completion:^(BOOL finished) {
-
- }];
- }
- }
- - (void)createCurrentView
- {
- GBTopLineViewModel *model = _dataArr[count];
-
- self.currentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
- [self addSubview:self.currentView];
-
- //此处是类型按钮(不需要点击)
- self.currentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- self.currentBtn.frame = CGRectMake(40, 30, 4, 4);
- // self.currentBtn.layer.masksToBounds = YES;
- // self.currentBtn.layer.cornerRadius = 5;
- // self.currentBtn.layer.borderWidth = 1;
- // self.currentBtn.layer.borderColor = [UIColor redColor].CGColor;
- // [self.currentBtn setTitle:model.type forState:UIControlStateNormal];
- // [self.currentBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
- // self.currentBtn.titleLabel.font = [UIFont systemFontOfSize:12];
- self.currentBtn.backgroundColor = [UIColor colorWithString:@"#FF8523"];
- ViewRadius(self.currentBtn, 4/2);
- [self.currentView addSubview:self.currentBtn];
-
- //此处是类型按钮(不需要点击)
- self.currentBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
- self.currentBtn1.frame = CGRectMake(40, 60, 4, 4);
- // self.currentBtn1.layer.masksToBounds = YES;
- // self.currentBtn1.layer.cornerRadius = 5;
- // self.currentBtn1.layer.borderWidth = 1;
- // self.currentBtn1.layer.borderColor = [UIColor redColor].CGColor;
- // [self.currentBtn1 setTitle:model.type forState:UIControlStateNormal];
- // [self.currentBtn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
- // self.currentBtn1.titleLabel.font = [UIFont systemFontOfSize:12];
- self.currentBtn1.backgroundColor = [UIColor colorWithString:@"#FF8523"];
- ViewRadius(self.currentBtn1, 4/2);
- [self.currentView addSubview:self.currentBtn1];
-
- //内容标题
- self.currentLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.currentBtn.frame.origin.x+self.currentBtn.frame.size.width+10, 15, self.currentView.frame.size.width-(self.currentBtn.frame.origin.x+self.currentBtn.frame.size.width+10+10), 30)];
- self.currentLabel.text = model.titleArray[0];
- self.currentLabel.textAlignment = NSTextAlignmentLeft;
- self.currentLabel.textColor = [UIColor blackColor];
- self.currentLabel.font = [UIFont systemFontOfSize:14];
- [self.currentView addSubview:self.currentLabel];
-
- self.currentLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(self.currentBtn.frame.origin.x+self.currentBtn.frame.size.width+10, 45, self.currentView.frame.size.width-(self.currentBtn.frame.origin.x+self.currentBtn.frame.size.width+10+10), 30)];
- self.currentLabel1.text = model.titleArray[1];
- self.currentLabel1.textAlignment = NSTextAlignmentLeft;
- self.currentLabel1.textColor = [UIColor blackColor];
- self.currentLabel1.font = [UIFont systemFontOfSize:14];
- [self.currentView addSubview:self.currentLabel1];
-
- }
- - (void)createHidenView
- {
- self.hidenView = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, self.frame.size.height)];
- [self addSubview:self.hidenView];
-
- //此处是类型按钮(不需要点击)
- self.hidenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- self.hidenBtn.frame = CGRectMake(40, 30, 4, 4);
- // self.hidenBtn.layer.masksToBounds = YES;
- // self.hidenBtn.layer.cornerRadius = 5;
- // self.hidenBtn.layer.borderWidth = 1;
- // self.hidenBtn.layer.borderColor = [UIColor redColor].CGColor;
- // [self.hidenBtn setTitle:@"" forState:UIControlStateNormal];
- // [self.hidenBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
- // self.hidenBtn.titleLabel.font = [UIFont systemFontOfSize:12];
- self.hidenBtn.backgroundColor = [UIColor colorWithString:@"#FF8523"];
- ViewRadius(self.hidenBtn, 4/2);
- [self.hidenView addSubview:self.hidenBtn];
-
- //此处是类型按钮(不需要点击)
- self.hidenBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
- self.hidenBtn1.frame = CGRectMake(40, 60, 4, 4);
- // self.hidenBtn1.layer.masksToBounds = YES;
- // self.hidenBtn1.layer.cornerRadius = 5;
- // self.hidenBtn1.layer.borderWidth = 1;
- // self.hidenBtn1.layer.borderColor = [UIColor redColor].CGColor;
- // [self.hidenBtn1 setTitle:@"" forState:UIControlStateNormal];
- // [self.hidenBtn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
- // self.hidenBtn1.titleLabel.font = [UIFont systemFontOfSize:12];
- self.hidenBtn1.backgroundColor = [UIColor colorWithString:@"#FF8523"];
- ViewRadius(self.hidenBtn1, 4/2);
- [self.hidenView addSubview:self.hidenBtn1];
-
- //内容标题
- self.hidenLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.hidenBtn.frame.origin.x+self.hidenBtn.frame.size.width+10, 15, self.hidenView.frame.size.width-(self.hidenBtn.frame.origin.x+self.hidenBtn.frame.size.width+10+10), 30)];
- self.hidenLabel.text = @"";
- self.hidenLabel.textAlignment = NSTextAlignmentLeft;
- self.hidenLabel.textColor = [UIColor blackColor];
- self.hidenLabel.font = [UIFont systemFontOfSize:14];
- [self.hidenView addSubview:self.hidenLabel];
-
- //内容标题
- self.hidenLabel1 = [[UILabel alloc]initWithFrame:CGRectMake(self.hidenBtn.frame.origin.x+self.hidenBtn.frame.size.width+10, 45, self.hidenView.frame.size.width-(self.hidenBtn.frame.origin.x+self.hidenBtn.frame.size.width+10+10), 30)];
- self.hidenLabel1.text = @"";
- self.hidenLabel1.textAlignment = NSTextAlignmentLeft;
- self.hidenLabel1.textColor = [UIColor blackColor];
- self.hidenLabel1.font = [UIFont systemFontOfSize:14];
- [self.hidenView addSubview:self.hidenLabel1];
- }
- #pragma mark - 停止定时器
- - (void)stopTimer
- {
- //停止定时器
- //在invalidate之前最好先用isValid先判断是否还在线程中:
- if ([_timer isValid] == YES) {
- [_timer invalidate];
- _timer = nil;
- }
- }
- @end
|