线上所有马甲包模板,与《猎豆》同UI。域名zhuadd

SelectView.m 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. #import "ComprehensiveView.h"
  16. #ifndef RGBA
  17. #define RGBA(r,g,b,a) \
  18. [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
  19. #endif
  20. // 屏幕尺寸
  21. #define KSCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
  22. #define KSCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
  23. // 主色调
  24. #define MAINCOLOR RGBA(0, 142, 236, 1)
  25. static NSString *const NY_SelectCollectionViewCellID = @"NY_SelectCollectionViewCell";
  26. static char *const btnKey = "btnKey";
  27. static CGFloat filrateHeight = 188;
  28. @interface SelectView()<SelectFiltrateViewDelegate,selectComViewDelegate>
  29. {
  30. BOOL show;
  31. BOOL comShow;
  32. }
  33. @property(nonatomic,strong)ComprehensiveView *compreView;
  34. @property(nonatomic,strong)NSArray *compreArray;
  35. @property (nonatomic, strong) SelectFiltrateView *filtrateView;
  36. @property (nonatomic, strong) NSArray *filtrateArr;
  37. @end
  38. @implementation SelectView
  39. - (instancetype)initWithFrame:(CGRect)frame withArr:(NSArray *)arr{
  40. self = [super initWithFrame:frame];
  41. if (self) {
  42. self.filtrateArr = arr;
  43. [self initUI];
  44. }
  45. return self;
  46. }
  47. - (void)initUI{
  48. // [self createCollectionView];
  49. [self creatFiltrateView];
  50. self.compreArray = @[@{@"type":@"1",@"title":@"综合排序"},@{@"type":@"6",@"title":@"优惠券面由高到低"}];
  51. self.backgroundColor = [UIColor yhGrayColor];
  52. UIView *mainView = [UIView new];
  53. mainView.backgroundColor = RGBA(255, 255, 255, 1);
  54. [self addSubview:mainView];
  55. [mainView mas_makeConstraints:^(MASConstraintMaker *make) {
  56. make.height.mas_equalTo(40);
  57. make.left.right.top.equalTo(self);
  58. }];
  59. UIView *topLine = [UIView new];
  60. topLine.backgroundColor = RGBA(222, 222, 222, 1);
  61. [self addSubview:topLine];
  62. [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
  63. make.height.mas_equalTo(0.5);
  64. make.left.right.equalTo(mainView);
  65. make.top.mas_equalTo(mainView.mas_bottom);
  66. }];
  67. NSArray *titleArr = @[@"综合",@"销量",@"价格",@"筛选"];
  68. for (int i = 0; i < 4; i++) {
  69. UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
  70. [button addTarget:self action:@selector(selectClick:) forControlEvents:UIControlEventTouchUpInside];
  71. [button setTitle:titleArr[i] forState:UIControlStateNormal ];
  72. button.titleLabel.font = [UIFont systemFontOfSize:13];
  73. [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  74. [button setTitleColor:[UIColor homeRedColor] forState:UIControlStateSelected];
  75. [mainView addSubview:button];
  76. button.tag = 100+i;
  77. [button mas_makeConstraints:^(MASConstraintMaker *make) {
  78. make.left.equalTo(mainView).offset(KSCREEN_WIDTH/4*i);
  79. make.top.bottom.equalTo(mainView);
  80. make.width.mas_equalTo(KSCREEN_WIDTH/4);
  81. }];
  82. if (i == _defaultSelectIndex) {
  83. button.selected = YES;
  84. }
  85. if (i == 0) {
  86. [button setImage:[UIImage imageNamed:@"sort_jt_sel"] forState:UIControlStateNormal];
  87. [button setButtonImageTitleStyle:ButtonImageTitleStyleRight padding:2];
  88. _sortBtn = button;
  89. }
  90. if (i == 2) {
  91. [button setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/jiagenormal"] forState:UIControlStateNormal];
  92. [button setButtonImageTitleStyle:ButtonImageTitleStyleRight padding:2];
  93. objc_setAssociatedObject(button, btnKey, @"1", OBJC_ASSOCIATION_ASSIGN);
  94. }
  95. if (i == 3) {
  96. [button setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/filtrate_n"] forState:UIControlStateNormal];
  97. [button setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/filtrate_s"] forState:UIControlStateSelected];
  98. [button setButtonImageTitleStyle:ButtonImageTitleStyleRight padding:2];
  99. }
  100. }
  101. OnlyTicketView *onlyView = [[OnlyTicketView alloc] initWithFrame:CGRectMake(0, 40, SCREEN_WIDTH, 40)];
  102. [onlyView.switchBtn addTarget:self action:@selector(onlyButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  103. [self addSubview:onlyView];
  104. [self createComView];
  105. }
  106. //只显示优惠券
  107. - (void)onlyButtonAction:(UIButton *)sender{
  108. if (self.delegate && [self.delegate respondsToSelector:@selector(selectItme:onlyTicketClick:)]) {
  109. [self.delegate selectItme:self onlyTicketClick:sender];
  110. }
  111. }
  112. -(void)setSelectItmeArr:(NSArray *)selectItmeArr{
  113. _selectItmeArr = selectItmeArr;
  114. _defaultSelectIndex = 0;
  115. UIButton *button = [self viewWithTag:102];
  116. [button setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/jiagenormal"] forState:UIControlStateNormal];
  117. objc_setAssociatedObject(button, btnKey, @"1", OBJC_ASSOCIATION_ASSIGN);
  118. button.selected = NO;
  119. for (int i = 0; i<4 ;i++) {
  120. UIButton *button = [self viewWithTag:i+100];
  121. if (i == 0) {
  122. button.selected = YES;
  123. continue;
  124. }
  125. if (i == 2) {
  126. continue;
  127. }
  128. button.selected = NO;
  129. if (i == 3) {
  130. [button setTitle:@"全部" forState:UIControlStateNormal];
  131. [button setButtonImageTitleStyle:ButtonImageTitleStyleRight padding:2];
  132. }
  133. }
  134. }
  135. /**
  136. 弹出视图
  137. */
  138. - (void)creatFiltrateView {
  139. self.filtrateView = [[SelectFiltrateView alloc] initWithFrame:CGRectMake(0, -filrateHeight, self.width, filrateHeight) filtrateArr:self.filtrateArr];
  140. self.filtrateView.delegate = self;
  141. self.filtrateView.hidden = YES;
  142. [self addSubview:self.filtrateView];
  143. }
  144. //综合视图
  145. -(void)createComView
  146. {
  147. self.compreView = [[ComprehensiveView alloc]initWithFrame:CGRectMake(0, -filrateHeight, self.width, 148) CompreensiveArray:self.compreArray];
  148. self.compreView.delegate=self;
  149. comShow =YES;
  150. self.compreView.hidden=YES;
  151. [self addSubview:self.compreView];
  152. }
  153. #pragma mark ----------- CompreHensiveViewDelegate ------
  154. -(void)selectWithBtn:(UIButton *)btn
  155. {
  156. NSInteger types =btn.tag/100;
  157. for (NSDictionary *dics in self.compreArray) {
  158. if ([dics[@"type"] integerValue] == types) {
  159. self.compreView.selectDic = dics;
  160. if ([self.delegate respondsToSelector:@selector(selectItme:sortDic:)]) {
  161. [self.delegate selectItme:self sortDic:dics];
  162. }
  163. [self compreViewsWithBtn:nil];
  164. return;
  165. }
  166. }
  167. }
  168. #pragma mark ----------- SelectFiltrateViewDelegate ------
  169. /**
  170. 筛选确定事件
  171. */
  172. - (void)makeSureActionWithFromValue:(NSString *)fromValue toValue:(NSString *)toValue {
  173. UIButton *btn = (UIButton *)[self viewWithTag:103];
  174. if ((fromValue.integerValue > 0) && (toValue.integerValue > fromValue.integerValue) &&(toValue.integerValue > 0)) {
  175. [btn setTitle:[NSString stringWithFormat:@"%@-%@",fromValue,toValue] forState:UIControlStateNormal];
  176. [btn setTitleColor:[UIColor homeRedColor] forState:UIControlStateNormal];
  177. [btn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
  178. }else if (fromValue.integerValue == 0 && toValue.integerValue > 0){
  179. [btn setTitle:[NSString stringWithFormat:@"%@以下",toValue] forState:UIControlStateNormal];
  180. [btn setTitleColor:[UIColor homeRedColor] forState:UIControlStateNormal];
  181. [btn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
  182. }else if (toValue.integerValue < 0 && fromValue.integerValue > 0){
  183. [btn setTitle:[NSString stringWithFormat:@"%@以上",fromValue] forState:UIControlStateNormal];
  184. [btn setTitleColor:[UIColor homeRedColor] forState:UIControlStateNormal];
  185. [btn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
  186. }else {
  187. [btn setTitle:@"筛选" forState:UIControlStateNormal];
  188. [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  189. [btn setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/filtrate_n"] forState:UIControlStateNormal];
  190. }
  191. if (self.delegate && [self.delegate respondsToSelector:@selector(filtrateViewMakeSureFromValue:toValue:)]) {
  192. [self.delegate filtrateViewMakeSureFromValue:fromValue toValue:toValue];
  193. }
  194. [self toggleViewWith:nil];
  195. }
  196. - (void)selectClick:(UIButton *)btn{
  197. if (btn.tag != 103) {//没点击全部分类,则让其他按钮回复默认状态
  198. for (int i = 0; i<3 ;i++) {
  199. UIButton *button = [self viewWithTag:i+100];
  200. button.selected = NO;
  201. }
  202. btn.selected = YES;
  203. [self toggleViewWith:nil];
  204. }else{//当点击全部分类按钮,则
  205. [self toggleViewWith:btn];
  206. }
  207. if (btn.tag == 100) {//综合
  208. [self compreViewsWithBtn:btn];
  209. return;
  210. }else{
  211. [_sortBtn setImage:[UIImage imageNamed:@"sort_jt"] forState:UIControlStateNormal];
  212. comShow = YES;
  213. self.compreView.hidden=comShow;
  214. self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:!comShow?0.4:0.0];
  215. }
  216. ButtonClickType type = ButtonClickTypeNormal;
  217. if (btn.tag == 102) {
  218. NSString *flag = objc_getAssociatedObject(btn, btnKey);
  219. if ([flag isEqualToString:@"1"]) {
  220. [btn setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/jiageup"] forState:UIControlStateNormal];
  221. objc_setAssociatedObject(btn, btnKey, @"2", OBJC_ASSOCIATION_ASSIGN);
  222. type = ButtonClickTypeUp;
  223. }else if ([flag isEqualToString:@"2"]){
  224. [btn setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/jiagedown"] forState:UIControlStateNormal];
  225. objc_setAssociatedObject(btn, btnKey, @"1", OBJC_ASSOCIATION_ASSIGN);
  226. type = ButtonClickTypeDown;
  227. }
  228. }else{
  229. //点击全部不复位价格
  230. if (btn.tag != 103) {
  231. UIButton *button = [self viewWithTag:102];
  232. [button setImage:[UIImage imageNamed:@"SelectViewBundle.bundle/jiagenormal"] forState:UIControlStateNormal];
  233. objc_setAssociatedObject(button, btnKey, @"1", OBJC_ASSOCIATION_ASSIGN);
  234. type = ButtonClickTypeNormal;
  235. }
  236. }
  237. if (btn.tag != 103) {
  238. if ([self.delegate respondsToSelector:@selector(selectTopButton:withIndex:withButtonType:)]) {
  239. [self.delegate selectTopButton:self withIndex:btn.tag-100 withButtonType:type];
  240. }
  241. }
  242. }
  243. -(void)compreViewsWithBtn:(UIButton *)btn
  244. {
  245. comShow = !comShow;
  246. self.compreView.hidden=comShow;
  247. if (!comShow) {
  248. self.frame = CGRectMake(0, self.frame.origin.y, KSCREEN_WIDTH, SCREEN_HEIGHT);
  249. [self.sortBtn setImage:[UIImage imageNamed:@"sort_jt_up"] forState:UIControlStateNormal];
  250. }else{
  251. self.frame = CGRectMake(0, self.frame.origin.y, KSCREEN_WIDTH, 80);
  252. [self.sortBtn setImage:[UIImage imageNamed:@"sort_jt_sel"] forState:UIControlStateNormal];
  253. }
  254. float frameY =40;
  255. [UIView animateWithDuration:0.5 animations:^{
  256. self.compreView.frame = CGRectMake(0, frameY, KSCREEN_WIDTH, 80);
  257. self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:!comShow?0.4:0.0];
  258. }];
  259. }
  260. //筛选
  261. - (void)toggleViewWith:(UIButton *)btn{
  262. if (!btn) {
  263. btn = [self viewWithTag:103];
  264. if (show) {
  265. show = NO;
  266. }else{
  267. return;
  268. }
  269. }else{
  270. show = !show;
  271. }
  272. if (_defaultSelectItmeIndex != 0) {
  273. btn.selected = YES;
  274. }else{
  275. btn.selected = NO;
  276. }
  277. if (show) {
  278. self.frame = CGRectMake(0, self.frame.origin.y, KSCREEN_WIDTH, SCREEN_HEIGHT);
  279. }else{
  280. self.frame = CGRectMake(0, self.frame.origin.y, KSCREEN_WIDTH, 80);
  281. }
  282. float frameY = show?80:-self.filtrateView.height;
  283. self.filtrateView.hidden = !show;
  284. [UIView animateWithDuration:0.5 animations:^{
  285. self.filtrateView.frame = CGRectMake(0, frameY, KSCREEN_WIDTH, filrateHeight);
  286. self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:show?0.4:0.0];
  287. }];
  288. }
  289. -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  290. [self toggleViewWith:nil];
  291. if (!comShow) {
  292. [self compreViewsWithBtn:nil];
  293. }
  294. }
  295. //综合弹出视图
  296. -(void)createComprehensiveView
  297. {
  298. }
  299. @end