Classification_view.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // Classification_view.m
  3. // iphoneLive
  4. //
  5. // Created by bugu on 2018/12/6.
  6. // Copyright © 2018年 cat. All rights reserved.
  7. //
  8. #import "Classification_view.h"
  9. #import "UIView+Additions.h"
  10. @implementation Classification_view
  11. - (instancetype)init
  12. {
  13. self =[super init];
  14. self.backgroundColor =UIColorFromRGB(0xffffff);
  15. return self;
  16. }
  17. - (UIScrollView *)scrollview
  18. {
  19. if (!_scrollview)
  20. {
  21. _scrollview =[[UIScrollView alloc]init];
  22. [self addSubview:_scrollview];
  23. [_scrollview mas_makeConstraints:^(MASConstraintMaker *make) {
  24. make.left.top.height.right.equalTo(self);
  25. // make.width.equalTo(self).offset(-44);
  26. }];
  27. _scrollview.showsHorizontalScrollIndicator = NO;
  28. // UIButton *allBtn =[[UIButton alloc]init];
  29. // [self addSubview:allBtn];
  30. // [allBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  31. // make.right.top.height.equalTo(self);
  32. // make.width.equalTo(@44);
  33. // }];
  34. // [allBtn setImage:[UIImage imageNamed:@"hm_img_hot_class"] forState:0];
  35. // [allBtn addTarget:self action:@selector(allAction) forControlEvents:UIControlEventTouchUpInside];
  36. // allBtn.hidden = YES;
  37. // UIView *fg =[[UIView alloc]init];
  38. // fg.backgroundColor =UIColorFromRGB(0xf5f5f5);
  39. // [self addSubview:fg];
  40. // [fg mas_makeConstraints:^(MASConstraintMaker *make) {
  41. // make.right.equalTo(allBtn.mas_left);
  42. // make.width.equalTo(@1);
  43. // make.height.equalTo(@25);
  44. // make.centerY.equalTo(self);
  45. // }];
  46. }
  47. return _scrollview;
  48. }
  49. - (void)allAction
  50. {
  51. if ([_delegate respondsToSelector:@selector(showAllClass)])
  52. {
  53. [_delegate showAllClass];
  54. }
  55. }
  56. - (void)setClassWithAry:(NSArray *)ary
  57. {
  58. [self.scrollview removeAllSubViews];
  59. _data_ary =[ary copy];
  60. CGFloat item_w =10,item_h =25;
  61. for (int i=0; i<ary.count; i++)
  62. {
  63. UIButton *item =[[UIButton alloc]init];
  64. [self.scrollview addSubview:item];
  65. NSString *title =ary[i];
  66. UIFont *font =[UIFont systemFontOfSize:13.];
  67. CGFloat need_w =[Classification_view backWidthWithNeiRong:title andFont:font];
  68. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  69. make.left.equalTo(self.scrollview).offset(item_w);
  70. make.height.equalTo(@(item_h));
  71. make.centerY.equalTo(self.scrollview);
  72. make.width.equalTo(@(need_w+20));
  73. }];
  74. item_w +=need_w +20;
  75. [item setTitle:title forState:0];
  76. item.titleLabel.font =font;
  77. if (i == 0)
  78. {
  79. item.tag =i;
  80. }else
  81. {
  82. item.tag =1;
  83. }
  84. item.layer.cornerRadius =item_h/2;
  85. [self drawWithBtn:item];
  86. [item addTarget:self action:@selector(itemAction:) forControlEvents:UIControlEventTouchUpInside];
  87. }
  88. [self layoutIfNeeded];
  89. self.scrollview.contentSize =CGSizeMake(item_w,self.scrollview.bounds.size.height);
  90. }
  91. - (void)selectWithIndex:(NSInteger)index
  92. {
  93. for (id obj in self.scrollview.subviews)
  94. {
  95. NSInteger _index =[self.scrollview.subviews indexOfObject:obj];
  96. if ([obj isKindOfClass:[UIButton class]]&&index == _index)
  97. {
  98. UIButton *item_btn =(UIButton *)obj;
  99. [self itemAction:item_btn];
  100. [self.scrollview scrollRectToVisible:item_btn.frame animated:YES];
  101. }
  102. }
  103. }
  104. - (void)itemAction:(UIButton *)btn
  105. {
  106. for (id obj in self.scrollview.subviews)
  107. {
  108. if ([obj isKindOfClass:[UIButton class]])
  109. {
  110. UIButton *item_btn =(UIButton *)obj;
  111. if (item_btn == btn)
  112. {
  113. item_btn.tag =0;
  114. if ([_delegate respondsToSelector:@selector(didSelectWith:andxiabiao:)])
  115. {
  116. NSInteger index =[self.scrollview.subviews indexOfObject:obj];
  117. [_delegate didSelectWith:_data_ary[index] andxiabiao:index];
  118. }
  119. }else
  120. {
  121. item_btn.tag =1;
  122. }
  123. [self drawWithBtn:item_btn];
  124. }
  125. }
  126. }
  127. //button样式
  128. - (void)drawWithBtn:(UIButton *)btn
  129. {
  130. if (btn.tag == 0)
  131. {
  132. [btn setTitleColor:Main_textColor forState:0];
  133. // btn.backgroundColor =[UIColor colorWithRed:126.0/255.0 green:55.0/255.0 blue:251.0/255.0 alpha:.1];
  134. // btn.backgroundColor =UIColorFromRGB(0xffffff);
  135. }else
  136. {
  137. [btn setTitleColor:UIColorFromRGB(0x999999) forState:0];
  138. // btn.backgroundColor =UIColorFromRGB(0xffffff);
  139. }
  140. }
  141. + (CGFloat)backWidthWithNeiRong:(NSString *)msg andFont:(UIFont *)font
  142. {
  143. NSDictionary *dic = @{NSFontAttributeName:font};//指定字号
  144. CGRect rect = [msg boundingRectWithSize:CGSizeMake(MAXFLOAT, 0)/*计算高度要先指定宽度*/ options:NSStringDrawingUsesLineFragmentOrigin |
  145. NSStringDrawingUsesFontLeading attributes:dic context:nil];
  146. return rect.size.width;
  147. }
  148. /*
  149. // Only override drawRect: if you perform custom drawing.
  150. // An empty implementation adversely affects performance during animation.
  151. - (void)drawRect:(CGRect)rect {
  152. // Drawing code
  153. }
  154. */
  155. @end