GBTopLineView.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. //
  2. // GBTopLineView.m
  3. // 淘宝垂直跑马灯广告
  4. //
  5. // Created by 张国兵 on 15/8/28.
  6. // Copyright (c) 2015年 zhangguobing. All rights reserved.
  7. //
  8. #import "GBTopLineView.h"
  9. @interface GBTopLineView(){
  10. NSTimer *_timer; //定时器
  11. int count;
  12. int flag; //标识当前是哪个view显示(currentView/hidenView)
  13. NSMutableArray *_dataArr;
  14. }
  15. @property (nonatomic,strong) UIView *currentView; //当前显示的view
  16. @property (nonatomic,strong) UIView *hidenView; //底部藏起的view
  17. @property (nonatomic,strong) UILabel *currentLabel;
  18. @property (nonatomic,strong) UILabel *currentLabel1;
  19. @property (nonatomic,strong) UIButton *currentBtn;
  20. @property (nonatomic,strong) UIButton *currentBtn1;
  21. @property (nonatomic,strong) UIButton *hidenBtn;
  22. @property (nonatomic,strong) UIButton *hidenBtn1;
  23. @property (nonatomic,strong) UILabel *hidenLabel;
  24. @property (nonatomic,strong) UILabel *hidenLabel1;
  25. @end
  26. @implementation GBTopLineView
  27. - (id)initWithFrame:(CGRect)frame
  28. {
  29. self = [super initWithFrame:frame];
  30. if (self) {
  31. [self createUI];
  32. }
  33. return self;
  34. }
  35. - (void)createUI
  36. {
  37. count = 0;
  38. flag = 0;
  39. self.layer.masksToBounds = YES;
  40. //创建定时器
  41. [self createTimer];
  42. [self createCurrentView];
  43. [self createHidenView];
  44. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dealTap:)];
  45. [self addGestureRecognizer:tap];
  46. //改进
  47. UILongPressGestureRecognizer*longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(dealLongPress:)];
  48. [self addGestureRecognizer:longPress];
  49. }
  50. - (void)setVerticalShowDataArr:(NSMutableArray *)dataArr
  51. {
  52. _dataArr = dataArr;
  53. NSLog(@"dataArr-->%@",dataArr);
  54. GBTopLineViewModel *model = _dataArr[count];
  55. // [self.currentBtn setTitle:model.type forState:UIControlStateNormal];
  56. // [self.currentBtn1 setTitle:model.type forState:UIControlStateNormal];
  57. self.currentLabel.text = model.titleArray[0];
  58. self.currentLabel1.text = model.titleArray[1];
  59. }
  60. -(void)dealLongPress:(UILongPressGestureRecognizer*)longPress{
  61. if(longPress.state==UIGestureRecognizerStateEnded){
  62. _timer.fireDate=[NSDate distantPast];
  63. }
  64. if(longPress.state==UIGestureRecognizerStateBegan){
  65. _timer.fireDate=[NSDate distantFuture];
  66. }
  67. }
  68. - (void)dealTap:(UITapGestureRecognizer *)tap
  69. {
  70. self.clickBlock(count);
  71. }
  72. - (void)createTimer
  73. {
  74. _timer=[NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(dealTimer) userInfo:nil repeats:YES];
  75. }
  76. #pragma mark - 跑马灯操作
  77. -(void)dealTimer
  78. {
  79. count++;
  80. if (count == _dataArr.count) {
  81. count = 0;
  82. }
  83. if (flag == 1) {
  84. GBTopLineViewModel*currentModel = _dataArr[count];
  85. [self.currentBtn setTitle:currentModel.type forState:UIControlStateNormal];
  86. [self.currentBtn1 setTitle:currentModel.type forState:UIControlStateNormal];
  87. self.currentLabel.text = currentModel.titleArray[0];
  88. self.currentLabel1.text = currentModel.titleArray[1];
  89. }
  90. if (flag == 0) {
  91. GBTopLineViewModel *hienModel = _dataArr[count];
  92. [self.hidenBtn setTitle:hienModel.type forState:UIControlStateNormal];
  93. [self.hidenBtn1 setTitle:hienModel.type forState:UIControlStateNormal];
  94. self.hidenLabel.text = hienModel.titleArray[0];
  95. self.hidenLabel1.text = hienModel.titleArray[1];
  96. }
  97. if (flag == 0) {
  98. [UIView animateWithDuration:0.5 animations:^{
  99. self.currentView.frame = CGRectMake(0, -self.frame.size.height, self.frame.size.width, self.frame.size.height);
  100. } completion:^(BOOL finished) {
  101. flag = 1;
  102. self.currentView.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, self.frame.size.height);
  103. }];
  104. [UIView animateWithDuration:0.5 animations:^{
  105. self.hidenView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
  106. } completion:^(BOOL finished) {
  107. }];
  108. }else{
  109. [UIView animateWithDuration:0.5 animations:^{
  110. self.hidenView.frame = CGRectMake(0, -self.frame.size.height, self.frame.size.width, self.frame.size.height);
  111. } completion:^(BOOL finished) {
  112. flag = 0;
  113. self.hidenView.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, self.frame.size.width);
  114. }];
  115. [UIView animateWithDuration:0.5 animations:^{
  116. self.currentView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
  117. } completion:^(BOOL finished) {
  118. }];
  119. }
  120. }
  121. - (void)createCurrentView
  122. {
  123. GBTopLineViewModel *model = _dataArr[count];
  124. self.currentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
  125. [self addSubview:self.currentView];
  126. //此处是类型按钮(不需要点击)
  127. self.currentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  128. self.currentBtn.frame = CGRectMake(40, 30, 4, 4);
  129. // self.currentBtn.layer.masksToBounds = YES;
  130. // self.currentBtn.layer.cornerRadius = 5;
  131. // self.currentBtn.layer.borderWidth = 1;
  132. // self.currentBtn.layer.borderColor = [UIColor redColor].CGColor;
  133. // [self.currentBtn setTitle:model.type forState:UIControlStateNormal];
  134. // [self.currentBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  135. // self.currentBtn.titleLabel.font = [UIFont systemFontOfSize:12];
  136. self.currentBtn.backgroundColor = [UIColor colorWithString:@"#FF8523"];
  137. ViewRadius(self.currentBtn, 4/2);
  138. [self.currentView addSubview:self.currentBtn];
  139. //此处是类型按钮(不需要点击)
  140. self.currentBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
  141. self.currentBtn1.frame = CGRectMake(40, 60, 4, 4);
  142. // self.currentBtn1.layer.masksToBounds = YES;
  143. // self.currentBtn1.layer.cornerRadius = 5;
  144. // self.currentBtn1.layer.borderWidth = 1;
  145. // self.currentBtn1.layer.borderColor = [UIColor redColor].CGColor;
  146. // [self.currentBtn1 setTitle:model.type forState:UIControlStateNormal];
  147. // [self.currentBtn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  148. // self.currentBtn1.titleLabel.font = [UIFont systemFontOfSize:12];
  149. self.currentBtn1.backgroundColor = [UIColor colorWithString:@"#FF8523"];
  150. ViewRadius(self.currentBtn1, 4/2);
  151. [self.currentView addSubview:self.currentBtn1];
  152. //内容标题
  153. 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)];
  154. self.currentLabel.text = model.titleArray[0];
  155. self.currentLabel.textAlignment = NSTextAlignmentLeft;
  156. self.currentLabel.textColor = [UIColor blackColor];
  157. self.currentLabel.font = [UIFont systemFontOfSize:14];
  158. [self.currentView addSubview:self.currentLabel];
  159. 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)];
  160. self.currentLabel1.text = model.titleArray[1];
  161. self.currentLabel1.textAlignment = NSTextAlignmentLeft;
  162. self.currentLabel1.textColor = [UIColor blackColor];
  163. self.currentLabel1.font = [UIFont systemFontOfSize:14];
  164. [self.currentView addSubview:self.currentLabel1];
  165. }
  166. - (void)createHidenView
  167. {
  168. self.hidenView = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, self.frame.size.height)];
  169. [self addSubview:self.hidenView];
  170. //此处是类型按钮(不需要点击)
  171. self.hidenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  172. self.hidenBtn.frame = CGRectMake(40, 30, 4, 4);
  173. // self.hidenBtn.layer.masksToBounds = YES;
  174. // self.hidenBtn.layer.cornerRadius = 5;
  175. // self.hidenBtn.layer.borderWidth = 1;
  176. // self.hidenBtn.layer.borderColor = [UIColor redColor].CGColor;
  177. // [self.hidenBtn setTitle:@"" forState:UIControlStateNormal];
  178. // [self.hidenBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  179. // self.hidenBtn.titleLabel.font = [UIFont systemFontOfSize:12];
  180. self.hidenBtn.backgroundColor = [UIColor colorWithString:@"#FF8523"];
  181. ViewRadius(self.hidenBtn, 4/2);
  182. [self.hidenView addSubview:self.hidenBtn];
  183. //此处是类型按钮(不需要点击)
  184. self.hidenBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
  185. self.hidenBtn1.frame = CGRectMake(40, 60, 4, 4);
  186. // self.hidenBtn1.layer.masksToBounds = YES;
  187. // self.hidenBtn1.layer.cornerRadius = 5;
  188. // self.hidenBtn1.layer.borderWidth = 1;
  189. // self.hidenBtn1.layer.borderColor = [UIColor redColor].CGColor;
  190. // [self.hidenBtn1 setTitle:@"" forState:UIControlStateNormal];
  191. // [self.hidenBtn1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  192. // self.hidenBtn1.titleLabel.font = [UIFont systemFontOfSize:12];
  193. self.hidenBtn1.backgroundColor = [UIColor colorWithString:@"#FF8523"];
  194. ViewRadius(self.hidenBtn1, 4/2);
  195. [self.hidenView addSubview:self.hidenBtn1];
  196. //内容标题
  197. 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)];
  198. self.hidenLabel.text = @"";
  199. self.hidenLabel.textAlignment = NSTextAlignmentLeft;
  200. self.hidenLabel.textColor = [UIColor blackColor];
  201. self.hidenLabel.font = [UIFont systemFontOfSize:14];
  202. [self.hidenView addSubview:self.hidenLabel];
  203. //内容标题
  204. 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)];
  205. self.hidenLabel1.text = @"";
  206. self.hidenLabel1.textAlignment = NSTextAlignmentLeft;
  207. self.hidenLabel1.textColor = [UIColor blackColor];
  208. self.hidenLabel1.font = [UIFont systemFontOfSize:14];
  209. [self.hidenView addSubview:self.hidenLabel1];
  210. }
  211. #pragma mark - 停止定时器
  212. - (void)stopTimer
  213. {
  214. //停止定时器
  215. //在invalidate之前最好先用isValid先判断是否还在线程中:
  216. if ([_timer isValid] == YES) {
  217. [_timer invalidate];
  218. _timer = nil;
  219. }
  220. }
  221. @end