Classification_Alertview.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // Classification_Alertview.m
  3. // iphoneLive
  4. //
  5. // Created by bugu on 2018/12/6.
  6. // Copyright © 2018年 cat. All rights reserved.
  7. //
  8. #import "UIView+Additions.h"
  9. #import "Classification_Alertview.h"
  10. #import "Classification_view.h"
  11. #define scrollview_maxH 350
  12. @implementation Classification_Alertview
  13. - (instancetype)init
  14. {
  15. self =[super init];
  16. return self;
  17. }
  18. - (void)setClassWithAry:(NSArray *)ary
  19. {
  20. [self.scrollview removeAllSubViews];
  21. CGFloat item_h =35,item_w =0,item_y =10,scroll_maxW =kScreenW*0.8,item_wJiange =10;
  22. for (int i =0; i<ary.count; i++)
  23. {
  24. UIButton *item =[[UIButton alloc]init];
  25. [self.scrollview addSubview:item];
  26. NSString *title =ary[i];
  27. UIFont *font =[UIFont systemFontOfSize:13.];
  28. CGFloat need_w =[Classification_view backWidthWithNeiRong:title andFont:font];
  29. if ((item_w +need_w +20 +item_wJiange) >scroll_maxW)
  30. {
  31. item_w =0;
  32. item_y +=(item_h +10);
  33. item_wJiange =10;
  34. }
  35. [item mas_makeConstraints:^(MASConstraintMaker *make) {
  36. make.left.equalTo(self.scrollview).offset(item_w+item_wJiange);
  37. make.height.equalTo(@(item_h));
  38. make.top.equalTo(self.scrollview).offset(46/*头部标签高度*/+item_y);
  39. make.width.equalTo(@(need_w+20));
  40. }];
  41. item.tag =1;
  42. item_w +=need_w +20;
  43. item_wJiange +=10;
  44. [item setTitle:title forState:0];
  45. item.titleLabel.font =font;
  46. item.layer.cornerRadius =item_h/2;
  47. [self drawWithBtn:item];
  48. [item addTarget:self action:@selector(itemAction:) forControlEvents:UIControlEventTouchUpInside];
  49. }
  50. }
  51. - (void)itemAction:(UIButton *)btn
  52. {
  53. for (id obj in self.scrollview.subviews)
  54. {
  55. if ([obj isKindOfClass:[UIButton class]])
  56. {
  57. UIButton *item_btn =(UIButton *)obj;
  58. if (item_btn == btn)
  59. {
  60. item_btn.tag =0;
  61. if ([_delegate respondsToSelector:@selector(didSelectWithAlert:)])
  62. {
  63. NSInteger index =[self.scrollview.subviews indexOfObject:obj];
  64. [_delegate didSelectWithAlert:@(index)];
  65. }
  66. }else
  67. {
  68. item_btn.tag =1;
  69. }
  70. [self drawWithBtn:item_btn];
  71. }
  72. }
  73. }
  74. //button样式
  75. - (void)drawWithBtn:(UIButton *)btn
  76. {
  77. if (btn.tag == 0)
  78. {
  79. [btn setTitleColor:UIColorFromRGB(0xffffff) forState:0];
  80. btn.backgroundColor =[UIColor colorWithRed:126.0/255.0 green:55.0/255.0 blue:251.0/255.0 alpha:1];
  81. }else
  82. {
  83. [btn setTitleColor:UIColorFromRGB(0x999999) forState:0];
  84. btn.backgroundColor =UIColorFromRGB(0xf5f5f5);
  85. }
  86. }
  87. -(void)selectWithIndex:(NSInteger)index
  88. {
  89. for (id obj in self.scrollview.subviews)
  90. {
  91. NSInteger _index =[self.scrollview.subviews indexOfObject:obj];
  92. if ([obj isKindOfClass:[UIButton class]])
  93. {
  94. UIButton *item =(UIButton *)obj;
  95. if (_index == index)
  96. {
  97. item.tag =0;
  98. }else
  99. {
  100. item.tag =1;
  101. }
  102. [self drawWithBtn:item];
  103. }
  104. }
  105. }
  106. - (UIScrollView *)scrollview
  107. {
  108. if (!_scrollview)
  109. {
  110. _scrollview =[[UIScrollView alloc]init];
  111. [self addSubview:_scrollview];
  112. [_scrollview mas_makeConstraints:^(MASConstraintMaker *make) {
  113. make.centerX.centerY.equalTo(self);
  114. make.width.equalTo(self).multipliedBy(0.8);
  115. make.height.equalTo(@(scrollview_maxH));
  116. }];
  117. _scrollview.backgroundColor =UIColorFromRGB(0xffffff);
  118. _scrollview.layer.cornerRadius =8.;
  119. _scrollview.layer.masksToBounds =YES;
  120. _scrollview.showsHorizontalScrollIndicator = NO;
  121. UILabel *titleLabel =[[UILabel alloc]init];
  122. [self addSubview:titleLabel];
  123. [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  124. make.centerX.top.equalTo(self.scrollview);
  125. make.width.equalTo(self.scrollview);
  126. make.height.equalTo(@45);
  127. }];
  128. titleLabel.textColor =UIColorFromRGB(0x333333);
  129. titleLabel.font =[UIFont systemFontOfSize:15.];
  130. titleLabel.textAlignment =1;
  131. titleLabel.text =ASLocalizedString(@"选择标签");
  132. UIView *fg =[[UIView alloc]init];
  133. [self addSubview:fg];
  134. [fg mas_makeConstraints:^(MASConstraintMaker *make) {
  135. make.top.equalTo(self.scrollview).offset(45);
  136. make.width.equalTo(self.scrollview).multipliedBy(0.95);
  137. make.height.equalTo(@.8);
  138. make.centerX.equalTo(_scrollview);
  139. }];
  140. fg.backgroundColor =UIColorFromRGB(0xe5e5e5);
  141. }
  142. return _scrollview;
  143. }
  144. /*
  145. // Only override drawRect: if you perform custom drawing.
  146. // An empty implementation adversely affects performance during animation.
  147. - (void)drawRect:(CGRect)rect {
  148. // Drawing code
  149. }
  150. */
  151. @end