口袋优选

SelectView.m 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. //
  2. // NY_SelectCView.m
  3. // 中安生态商城
  4. //
  5. // Created by LanSha on 2017/7/25.
  6. // Copyright © 2017年 王鑫年. All rights reserved.
  7. //
  8. #import "SelectView.h"
  9. #import "UIButton+ImageTitleStyle.h"
  10. #import "NY_SelectCollectionViewCell.h"
  11. #import "Masonry.h"
  12. #import "SelectFiltrateView.h"
  13. #import <objc/runtime.h>
  14. #import "OnlyTicketView.h"
  15. #ifndef RGBA
  16. #define RGBA(r,g,b,a) \
  17. [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
  18. #endif
  19. // 屏幕尺寸
  20. #define KSCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
  21. #define KSCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
  22. // 主色调
  23. #define MAINCOLOR RGBA(0, 142, 236, 1)
  24. static NSString *const NY_SelectCollectionViewCellID = @"NY_SelectCollectionViewCell";
  25. static char *const btnKey = "btnKey";
  26. static CGFloat filrateHeight = 188;
  27. @interface SelectView()<SelectFiltrateViewDelegate>
  28. {
  29. BOOL show;
  30. }
  31. @property (nonatomic, strong) SelectFiltrateView *filtrateView;
  32. @property (nonatomic, strong) NSArray *filtrateArr;
  33. @end
  34. @implementation SelectView
  35. - (instancetype)initWithFrame:(CGRect)frame withArr:(NSArray *)arr{
  36. self = [super initWithFrame:frame];
  37. if (self) {
  38. self.filtrateArr = arr;
  39. [self initUI];
  40. }
  41. return self;
  42. }
  43. - (void)initUI{
  44. // [self createCollectionView];
  45. [self creatFiltrateView];
  46. self.backgroundColor = [UIColor whiteColor];
  47. UIView *mainView = [UIView new];
  48. mainView.backgroundColor = RGBA(255, 255, 255, 1);
  49. [self addSubview:mainView];
  50. [mainView mas_makeConstraints:^(MASConstraintMaker *make) {
  51. make.height.mas_equalTo(40);
  52. make.left.right.top.equalTo(self);
  53. }];
  54. UIView *topLine = [UIView new];
  55. topLine.backgroundColor = RGBA(222, 222, 222, 1);
  56. [self addSubview:topLine];
  57. [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
  58. make.height.mas_equalTo(0.5);
  59. make.left.right.equalTo(mainView);
  60. make.top.mas_equalTo(mainView.mas_bottom);
  61. }];
  62. NSArray *titleArr = @[@"综合",@"销量",@"价格",@"筛选"];
  63. for (int i = 0; i < 4; i++) {
  64. UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
  65. [button addTarget:self action:@selector(selectClick:) forControlEvents:UIControlEventTouchUpInside];
  66. [button setTitle:titleArr[i] forState:UIControlStateNormal ];
  67. button.titleLabel.font = [UIFont systemFontOfSize:13];
  68. [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  69. [button setTitleColor:[UIColor homeRedColor] forState:UIControlStateSelected];
  70. [mainView addSubview:button];
  71. button.tag = 100+i;
  72. [button mas_makeConstraints:^(MASConstraintMaker *make) {
  73. make.left.equalTo(mainView).offset(KSCREEN_WIDTH/4*i);
  74. make.top.bottom.equalTo(mainView);
  75. make.width.mas_equalTo(KSCREEN_WIDTH/4);
  76. }];
  77. if (i == _defaultSelectIndex) {
  78. button.selected = YES;
  79. }
  80. if (i == 2) {
  81. [button setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/jiagenormal"] forState:UIControlStateNormal];
  82. [button setButtonImageTitleStyle:ButtonImageTitleStyleRight padding:2];
  83. objc_setAssociatedObject(button, btnKey, @"1", OBJC_ASSOCIATION_ASSIGN);
  84. }
  85. if (i == 3) {
  86. [button setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/filtrate_n"] forState:UIControlStateNormal];
  87. [button setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/filtrate_s"] forState:UIControlStateSelected];
  88. [button setButtonImageTitleStyle:ButtonImageTitleStyleRight padding:2];
  89. }
  90. }
  91. OnlyTicketView *onlyView = [[OnlyTicketView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, 40)];
  92. onlyView.backgroundColor = [UIColor whiteColor];
  93. [onlyView.switchBtn addTarget:self action:@selector(onlyButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  94. [self addSubview:onlyView];
  95. }
  96. //只显示优惠券
  97. - (void)onlyButtonAction:(UIButton *)sender{
  98. if (self.delegate && [self.delegate respondsToSelector:@selector(selectItme:onlyTicketClick:)]) {
  99. [self.delegate selectItme:self onlyTicketClick:sender];
  100. }
  101. }
  102. -(void)setSelectItmeArr:(NSArray *)selectItmeArr{
  103. _selectItmeArr = selectItmeArr;
  104. _defaultSelectIndex = 0;
  105. UIButton *button = [self viewWithTag:102];
  106. [button setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/jiagenormal"] forState:UIControlStateNormal];
  107. objc_setAssociatedObject(button, btnKey, @"1", OBJC_ASSOCIATION_ASSIGN);
  108. button.selected = NO;
  109. for (int i = 0; i<4 ;i++) {
  110. UIButton *button = [self viewWithTag:i+100];
  111. if (i == 0) {
  112. button.selected = YES;
  113. continue;
  114. }
  115. if (i == 2) {
  116. continue;
  117. }
  118. button.selected = NO;
  119. if (i == 3) {
  120. [button setTitle:@"全部" forState:UIControlStateNormal];
  121. [button setButtonImageTitleStyle:ButtonImageTitleStyleRight padding:2];
  122. }
  123. }
  124. }
  125. /**
  126. 弹出视图
  127. */
  128. - (void)creatFiltrateView {
  129. self.filtrateView = [[SelectFiltrateView alloc] initWithFrame:CGRectMake(0, -filrateHeight, self.width, filrateHeight) filtrateArr:self.filtrateArr];
  130. self.filtrateView.delegate = self;
  131. self.filtrateView.hidden = YES;
  132. [self addSubview:self.filtrateView];
  133. }
  134. #pragma mark ----------- SelectFiltrateViewDelegate ------
  135. /**
  136. 筛选确定事件
  137. */
  138. - (void)makeSureActionWithFromValue:(NSString *)fromValue toValue:(NSString *)toValue {
  139. UIButton *btn = (UIButton *)[self viewWithTag:103];
  140. if ((fromValue.integerValue > 0) && (toValue.integerValue > fromValue.integerValue) &&(toValue.integerValue > 0)) {
  141. [btn setTitle:[NSString stringWithFormat:@"%@-%@",fromValue,toValue] forState:UIControlStateNormal];
  142. [btn setTitleColor:[UIColor homeRedColor] forState:UIControlStateNormal];
  143. [btn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
  144. }else if (fromValue.integerValue == 0 && toValue.integerValue > 0){
  145. [btn setTitle:[NSString stringWithFormat:@"%@以下",toValue] forState:UIControlStateNormal];
  146. [btn setTitleColor:[UIColor homeRedColor] forState:UIControlStateNormal];
  147. [btn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
  148. }else if (toValue.integerValue < 0 && fromValue.integerValue > 0){
  149. [btn setTitle:[NSString stringWithFormat:@"%@以上",fromValue] forState:UIControlStateNormal];
  150. [btn setTitleColor:[UIColor homeRedColor] forState:UIControlStateNormal];
  151. [btn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
  152. }else {
  153. [btn setTitle:@"筛选" forState:UIControlStateNormal];
  154. [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  155. [btn setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/filtrate_n"] forState:UIControlStateNormal];
  156. }
  157. if (self.delegate && [self.delegate respondsToSelector:@selector(filtrateViewMakeSureFromValue:toValue:)]) {
  158. [self.delegate filtrateViewMakeSureFromValue:fromValue toValue:toValue];
  159. }
  160. [self toggleViewWith:nil];
  161. }
  162. - (void)selectClick:(UIButton *)btn{
  163. if (btn.tag != 103) {//没点击全部分类,则让其他按钮回复默认状态
  164. for (int i = 0; i<3 ;i++) {
  165. UIButton *button = [self viewWithTag:i+100];
  166. button.selected = NO;
  167. }
  168. btn.selected = YES;
  169. [self toggleViewWith:nil];
  170. }else{//当点击全部分类按钮,则
  171. [self toggleViewWith:btn];
  172. }
  173. ButtonClickType type = ButtonClickTypeNormal;
  174. if (btn.tag == 102) {
  175. NSString *flag = objc_getAssociatedObject(btn, btnKey);
  176. if ([flag isEqualToString:@"1"]) {
  177. [btn setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/jiageup"] forState:UIControlStateNormal];
  178. objc_setAssociatedObject(btn, btnKey, @"2", OBJC_ASSOCIATION_ASSIGN);
  179. type = ButtonClickTypeUp;
  180. }else if ([flag isEqualToString:@"2"]){
  181. [btn setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/jiagedown"] forState:UIControlStateNormal];
  182. objc_setAssociatedObject(btn, btnKey, @"1", OBJC_ASSOCIATION_ASSIGN);
  183. type = ButtonClickTypeDown;
  184. }
  185. }else{
  186. //点击全部不复位价格
  187. if (btn.tag != 103) {
  188. UIButton *button = [self viewWithTag:102];
  189. [button setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/jiagenormal"] forState:UIControlStateNormal];
  190. objc_setAssociatedObject(button, btnKey, @"1", OBJC_ASSOCIATION_ASSIGN);
  191. type = ButtonClickTypeNormal;
  192. }
  193. }
  194. if (btn.tag != 103) {
  195. if ([self.delegate respondsToSelector:@selector(selectTopButton:withIndex:withButtonType:)]) {
  196. [self.delegate selectTopButton:self withIndex:btn.tag-100 withButtonType:type];
  197. }
  198. }
  199. }
  200. - (void)toggleViewWith:(UIButton *)btn{
  201. if (!btn) {
  202. btn = [self viewWithTag:103];
  203. if (show) {
  204. show = NO;
  205. }else{
  206. return;
  207. }
  208. }else{
  209. show = !show;
  210. }
  211. if (_defaultSelectItmeIndex != 0) {
  212. btn.selected = YES;
  213. }else{
  214. btn.selected = NO;
  215. }
  216. if (show) {
  217. self.frame = CGRectMake(0, self.frame.origin.y, KSCREEN_WIDTH, SCREEN_HEIGHT);
  218. }else{
  219. self.frame = CGRectMake(0, self.frame.origin.y, KSCREEN_WIDTH, 80);
  220. }
  221. float frameY = show?80:-self.filtrateView.height;
  222. self.filtrateView.hidden = !show;
  223. [UIView animateWithDuration:0.5 animations:^{
  224. self.filtrateView.frame = CGRectMake(0, frameY, KSCREEN_WIDTH, filrateHeight);
  225. self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:show?0.4:0.0];
  226. }];
  227. }
  228. -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  229. [self toggleViewWith:nil];
  230. }
  231. @end