Nenhuma Descrição

FKCouponSelectView.m 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //
  2. // FKCouponSelectView.m
  3. // FirstLink
  4. //
  5. // Created by jack on 16/8/21.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKCouponSelectView.h"
  9. #import "FKSelectCouponCell.h"
  10. #import "CashCouponItem.h"
  11. @interface FKCouponSelectView () <UITableViewDelegate, UITableViewDataSource, UIGestureRecognizerDelegate>
  12. @property (nonatomic, strong) UILabel *titleLabel;
  13. @property (nonatomic, strong) UIView *topLine;
  14. @property (nonatomic, strong) UIView *contentView;
  15. @property (nonatomic, strong) UIButton *cancelBtn;
  16. @property (nonatomic, strong) UITableView *tableView;
  17. @property (nonatomic, strong) NSArray *couponArray;
  18. @end
  19. @implementation FKCouponSelectView
  20. - (instancetype)initWithFrame:(CGRect)frame{
  21. if (self = [super initWithFrame:frame]) {
  22. [self addAllSubviews];
  23. [self addTapGesture];
  24. }
  25. return self;
  26. }
  27. - (void)addTapGesture{
  28. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureClick:)];
  29. tap.delegate = self;
  30. [self addGestureRecognizer:tap];
  31. }
  32. - (void)addAllSubviews{
  33. [self addSubview:self.contentView];
  34. [self.contentView addSubview:self.topLine];
  35. [self.contentView addSubview:self.titleLabel];
  36. [self.contentView addSubview:self.cancelBtn];
  37. [self.contentView addSubview:self.tableView];
  38. [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
  39. make.left.right.bottom.equalTo(self);
  40. make.height.mas_equalTo(285);
  41. }];
  42. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  43. make.left.equalTo(self.contentView).offset(15);
  44. make.centerY.equalTo(self.contentView.mas_top).offset(22.5);
  45. }];
  46. [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  47. make.right.top.equalTo(self.contentView);
  48. make.size.mas_equalTo(CGSizeMake(45, 45));
  49. }];
  50. [self.topLine mas_makeConstraints:^(MASConstraintMaker *make) {
  51. make.left.equalTo(self.contentView).offset(15);
  52. make.top.equalTo(self.contentView).offset(45);
  53. make.right.equalTo(self.contentView);
  54. make.height.mas_equalTo(0.5);
  55. }];
  56. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
  57. make.top.equalTo(self.topLine.mas_bottom);
  58. make.left.right.bottom.equalTo(self.contentView);
  59. }];
  60. }
  61. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  62. return self.couponArray.count;
  63. }
  64. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  65. FKSelectCouponCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FKSelectCouponCell class])];
  66. CashCouponItem *item = [self couponItemAtIndex:indexPath.row];
  67. NSArray *selectIndexs = [tableView indexPathsForSelectedRows];
  68. BOOL selected = [selectIndexs containsObject:indexPath];
  69. if (cell && item) {
  70. cell.priceLabel.text = [FLStringHelper convertFenToRMBmoneyString:item.amount];
  71. cell.typeLabel.text = item.type;
  72. cell.ruleLabel.text = item.desc;
  73. cell.timeLabel.text = item.timeDesc;
  74. cell.selectBtn.selected = selected ? YES : NO;
  75. }
  76. return cell;
  77. }
  78. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  79. return 80.0f;
  80. }
  81. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  82. NSArray *selectArray = [tableView indexPathsForSelectedRows];
  83. for (NSIndexPath *selectIndex in selectArray) {
  84. if (selectIndex != indexPath) {
  85. [tableView deselectRowAtIndexPath:selectIndex animated:NO];
  86. FKSelectCouponCell *cell = [tableView cellForRowAtIndexPath:selectIndex];
  87. cell.selectBtn.selected = NO;
  88. }
  89. }
  90. FKSelectCouponCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  91. cell.selectBtn.selected = YES;
  92. [self selectFinish];
  93. }
  94. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
  95. FKSelectCouponCell *cell = [tableView cellForRowAtIndexPath:indexPath];
  96. cell.selectBtn.selected = NO;
  97. [self selectFinish];
  98. }
  99. - (void)refreshCouponArray:(NSArray *)couponArray selectFirst:(BOOL)selectFirst{
  100. self.couponArray = couponArray;
  101. [self.tableView reloadData];
  102. if (selectFirst) {
  103. NSIndexPath *firstIndex = [NSIndexPath indexPathForRow:0 inSection:0];
  104. [self.tableView selectRowAtIndexPath:firstIndex
  105. animated:NO
  106. scrollPosition:UITableViewScrollPositionNone];
  107. FKSelectCouponCell *cell = [self.tableView cellForRowAtIndexPath:firstIndex];
  108. if (cell) cell.selectBtn.selected = YES;
  109. }
  110. [self.tableView setContentOffset:CGPointZero];
  111. }
  112. - (void)showInView:(UIView *)view animated:(BOOL)animated{
  113. if (!view) {
  114. return;
  115. }
  116. CGFloat height = view.frame.size.height;
  117. CGFloat width = view.frame.size.width;
  118. CGFloat contentH = 285; // 内容高度
  119. [view addSubview:self];
  120. [self setFrame:view.frame];
  121. [self.contentView setFrame:CGRectMake(0, height, width, contentH)];
  122. self.backgroundColor = [UIColor clearColor];
  123. CGFloat duration = animated ? 0.3f : 0;
  124. WeakSelf(weakSelf);
  125. [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  126. weakSelf.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f];
  127. [weakSelf.contentView setFrame:CGRectMake(0, height - contentH, width, contentH)];
  128. } completion:nil];
  129. }
  130. - (void)tapGestureClick:(UIGestureRecognizer *)tapGesture{
  131. CGPoint location = [tapGesture locationInView:self];
  132. BOOL isOnContent = CGRectContainsPoint(self.contentView.frame, location);
  133. if (isOnContent) {
  134. return;
  135. }else{
  136. [self clickCancelBtn];
  137. }
  138. }
  139. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
  140. CGPoint location = [touch locationInView:self];
  141. if (CGRectContainsPoint(self.contentView.frame, location)) return NO;
  142. return YES;
  143. }
  144. - (void)dismissAnimated{
  145. if (self.superview){
  146. [UIView animateWithDuration:0.3f delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
  147. self.backgroundColor = [UIColor clearColor];
  148. [self.contentView setFrame:CGRectOffset(self.contentView.frame, 0, CGRectGetHeight(self.contentView.frame))];
  149. } completion:^(BOOL finished) {
  150. [self removeFromSuperview];
  151. }];
  152. }
  153. }
  154. - (void)clickCancelBtn{
  155. [self selectFinish];
  156. }
  157. - (void)selectFinish{
  158. [self dismissAnimated];
  159. if (self.finish) {
  160. NSIndexPath *index = [self selectedIndexPath];
  161. CashCouponItem *couponItem = nil;
  162. if (index) {
  163. couponItem = [self couponItemAtIndex:index.row];
  164. }
  165. self.finish(couponItem.itemID);
  166. }
  167. }
  168. - (CashCouponItem *)couponItemAtIndex:(NSInteger)index{
  169. if (index >= 0 && index < self.couponArray.count) {
  170. return self.couponArray[index];
  171. }
  172. return nil;
  173. }
  174. - (NSIndexPath *)selectedIndexPath{
  175. NSArray *indexs = [self.tableView indexPathsForSelectedRows];
  176. if (indexs.count) {
  177. return indexs.firstObject;
  178. }
  179. return nil;
  180. }
  181. - (BOOL)isShowing{
  182. if (self.superview) return YES;
  183. return NO;
  184. }
  185. #pragma mark - property
  186. - (UILabel *)titleLabel{
  187. if (_titleLabel == nil) {
  188. _titleLabel = [[UILabel alloc]init];
  189. _titleLabel.textColor = UIColorFromRGB(0x999999);
  190. _titleLabel.font = [UIFont systemFontOfSize:14];
  191. _titleLabel.text = @"选择优惠券";
  192. }
  193. return _titleLabel;
  194. }
  195. - (UIView *)topLine{
  196. if (_topLine == nil) {
  197. _topLine = [[UIView alloc]init];
  198. _topLine.backgroundColor = UIColorFromRGB(0xe5e5e5);
  199. }
  200. return _topLine;
  201. }
  202. - (UIView *)contentView{
  203. if (_contentView == nil) {
  204. _contentView = [[UIView alloc]init];
  205. _contentView.backgroundColor = [UIColor whiteColor];
  206. }
  207. return _contentView;
  208. }
  209. - (UIButton *)cancelBtn{
  210. if (_cancelBtn == nil) {
  211. _cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  212. [_cancelBtn setImage:[UIImage imageNamed:@"selectCoupon_cancel"] forState:UIControlStateNormal];
  213. [_cancelBtn addTarget:self
  214. action:@selector(clickCancelBtn)
  215. forControlEvents:UIControlEventTouchUpInside];
  216. }
  217. return _cancelBtn;
  218. }
  219. - (UITableView *)tableView{
  220. if (_tableView == nil) {
  221. _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
  222. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  223. _tableView.dataSource = self;
  224. _tableView.delegate = self;
  225. _tableView.showsVerticalScrollIndicator = NO;
  226. _tableView.allowsMultipleSelection = YES;
  227. [_tableView registerClass:[FKSelectCouponCell class] forCellReuseIdentifier:NSStringFromClass([FKSelectCouponCell class])];
  228. }
  229. return _tableView;
  230. }
  231. @end