RechargeWayView.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // RechargeWayView.m
  3. // BuguLive
  4. //
  5. // Created by 王珂 on 17/3/13.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. //
  8. #import "RechargeWayView.h"
  9. @implementation RechargeWayView
  10. - (instancetype)initWithFrame:(CGRect)frame
  11. {
  12. if (self = [super initWithFrame:frame]) {
  13. UICollectionViewFlowLayout * flow = [[UICollectionViewFlowLayout alloc] init] ;
  14. CGFloat itemW = (self.width-40)/3.0;
  15. CGFloat itemH = 45;
  16. //设置cell的大小
  17. flow.itemSize = CGSizeMake(itemW, itemH) ;
  18. flow.minimumLineSpacing = 10;
  19. flow.minimumInteritemSpacing = 10;
  20. flow.sectionInset = UIEdgeInsetsMake(15, 10, 15, 10) ;
  21. flow.scrollDirection = 1;
  22. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.width, 75) collectionViewLayout:flow];
  23. _collectionView.delegate = self;
  24. _collectionView.dataSource = self;
  25. _collectionView.backgroundColor = [UIColor clearColor];
  26. // _collectionView.pagingEnabled = YES;
  27. _collectionView.showsHorizontalScrollIndicator = NO;
  28. [_collectionView registerNib:[UINib nibWithNibName:@"RechargeWayCell" bundle:nil] forCellWithReuseIdentifier:@"RechargeWayCell"];
  29. [self addSubview:_collectionView];
  30. }
  31. return self;
  32. }
  33. - (void)setRechargeWayArr:(NSArray *)rechargeWayArr
  34. {
  35. _rechargeWayArr = rechargeWayArr;
  36. for (PayTypeModel * model in _rechargeWayArr) {
  37. NSString * string;
  38. if (model.name.length>0) {
  39. string = model.name;
  40. }
  41. else
  42. {
  43. if ([model.class_name isEqual:@"Aliapp"]) {
  44. string = ASLocalizedString(@"支付宝");
  45. }
  46. else if ([model.class_name isEqual:@"Iappay"])
  47. {
  48. string = ASLocalizedString(@"苹果内购");
  49. }
  50. else if ([model.class_name isEqual:@"WxApp"])
  51. {
  52. string = ASLocalizedString(@"微信支付");
  53. }
  54. }
  55. model.name = string;
  56. }
  57. NSInteger count = rechargeWayArr.count;
  58. if (count>3) {
  59. self.selectIndex = 0;
  60. _collectionView.hidden = NO;
  61. //[self addSubview:_collectionView];
  62. }
  63. else
  64. {
  65. _collectionView.hidden = YES;
  66. [_collectionView removeFromSuperview];
  67. while (self.subviews.count<count) {
  68. UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
  69. button.backgroundColor = kAppRechargeBtnColor;
  70. button.layer.cornerRadius = 12.5;
  71. button.layer.masksToBounds = YES;
  72. button.titleLabel.font = kAppSmallTextFont;
  73. button.layer.borderWidth = 1.0;
  74. button.layer.borderColor = kAppGrayColor1.CGColor;
  75. [self addSubview:button];
  76. }
  77. CGFloat width = count >3 ? (self.width-10*(count+1))/count:(self.width-40)/3;
  78. CGFloat height = 25;
  79. for (NSInteger i=0; i<self.subviews.count; ++i) {
  80. UIButton * button = self.subviews[i];
  81. PayTypeModel *model = rechargeWayArr[i];
  82. NSString * string;
  83. if (model.name.length>0) {
  84. string = model.name;
  85. }
  86. else
  87. {
  88. if ([model.class_name isEqual:@"Aliapp"]) {
  89. string = ASLocalizedString(@"支付宝");
  90. }
  91. else if ([model.class_name isEqual:@"Iappay"])
  92. {
  93. string = ASLocalizedString(@"苹果内购");
  94. }
  95. else if ([model.class_name isEqual:@"WxApp"])
  96. {
  97. string = ASLocalizedString(@"微信支付");
  98. }
  99. }
  100. if (i<count) {
  101. button.frame = CGRectMake((i+1)*10+i*width, 25, width, height);
  102. [button setTitle:string forState:UIControlStateNormal];
  103. [button setTitleColor:kAppRechargeSelectColor forState:UIControlStateNormal];
  104. [button addTarget:self action:@selector(choseWayBtn:) forControlEvents:UIControlEventTouchUpInside];
  105. if (i==_selectIndex) {
  106. button.backgroundColor = kAppGrayColor1;
  107. [button setTitleColor:kWhiteColor forState:UIControlStateNormal];
  108. }
  109. else
  110. {
  111. button.backgroundColor = kWhiteColor;
  112. [button setTitleColor:kAppGrayColor1 forState:UIControlStateNormal];
  113. }
  114. }
  115. else
  116. {
  117. button.hidden = YES;
  118. }
  119. }
  120. if (count<3) {
  121. if (count == 1) {
  122. UIButton * button = self.subviews[0];
  123. button.frame = CGRectMake((self.width-width)/2, 25, width, height);
  124. }
  125. else if (count == 2)
  126. {
  127. UIButton * oneButton = self.subviews[0];
  128. oneButton.frame = CGRectMake((self.width-width*2)/3, 25, width, height);
  129. UIButton * twoButton = self.subviews[1];
  130. twoButton.frame = CGRectMake(width+(self.width-width*2)/3*2, 25, width, height);
  131. }
  132. }
  133. }
  134. }
  135. - (void)setSelectIndex:(NSInteger)selectIndex
  136. {
  137. _selectIndex = selectIndex;
  138. for (int i=0; i<self.rechargeWayArr.count; ++i) {
  139. PayTypeModel *model = _rechargeWayArr[i];
  140. if (i != selectIndex) {
  141. model.isSelect = NO;
  142. }
  143. else
  144. {
  145. model.isSelect = YES;
  146. }
  147. }
  148. if (self.rechargeWayArr.count>3) {
  149. //self.collectionView.contentOffset = CGPointMake(self.width*(selectIndex/3), 0);
  150. [self.collectionView reloadData];
  151. }
  152. else
  153. {
  154. if (self.subviews.count>_selectIndex) {
  155. for (UIButton * button in self.subviews) {
  156. button.backgroundColor = kWhiteColor;
  157. [button setTitleColor:kAppGrayColor1 forState:UIControlStateNormal];
  158. }
  159. _selectIndex = selectIndex;
  160. UIButton * selectButton = self.subviews[_selectIndex];
  161. selectButton.backgroundColor = kAppGrayColor1;
  162. [selectButton setTitleColor:kWhiteColor forState:UIControlStateNormal];
  163. }
  164. }
  165. }
  166. - (void)choseWayBtn:(UIButton *) button
  167. {
  168. for (int i=0; i<self.subviews.count; ++i) {
  169. if ([self.subviews[i] isEqual:button]) {
  170. _nowIndex = i;
  171. }
  172. }
  173. for (UIButton * button in self.subviews) {
  174. button.backgroundColor = kWhiteColor;
  175. [button setTitleColor:kAppGrayColor1 forState:UIControlStateNormal];
  176. }
  177. _selectIndex = _nowIndex;
  178. button.backgroundColor = kAppGrayColor1;
  179. [button setTitleColor:kWhiteColor forState:UIControlStateNormal];
  180. if (_delegate && [_delegate respondsToSelector:@selector(choseRechargeWayWithIndex:)]) {
  181. [_delegate choseRechargeWayWithIndex:_selectIndex];
  182. }
  183. if (_delegate && [_delegate respondsToSelector:@selector(choseRechargeWayWithWayName:)]) {
  184. PayTypeModel *model = self.rechargeWayArr[_selectIndex];
  185. [_delegate choseRechargeWayWithWayName:model.class_name];
  186. }
  187. }
  188. - (NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  189. {
  190. return self.rechargeWayArr.count;
  191. }
  192. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  193. {
  194. RechargeWayCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RechargeWayCell" forIndexPath:indexPath] ;
  195. cell.model = self.rechargeWayArr[indexPath.item];
  196. cell.delegate = self;
  197. return cell ;
  198. }
  199. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  200. {
  201. [self clickCellWith:indexPath];
  202. }
  203. - (void)clickWithRechargeWayCell:(RechargeWayCell *)cell
  204. {
  205. NSIndexPath * indexPath = [_collectionView indexPathForCell:cell];
  206. [self clickCellWith:indexPath];
  207. }
  208. - (void)clickCellWith:(NSIndexPath *)indexPath
  209. {
  210. if (indexPath.item == self.selectIndex) {
  211. return;
  212. }
  213. else
  214. {
  215. self.selectIndex = indexPath.item;
  216. if (_delegate && [_delegate respondsToSelector:@selector(choseRechargeWayWithIndex:)]) {
  217. [_delegate choseRechargeWayWithIndex:_selectIndex];
  218. }
  219. if (_delegate && [_delegate respondsToSelector:@selector(choseRechargeWayWithWayName:)]) {
  220. PayTypeModel *model = self.rechargeWayArr[_selectIndex];
  221. [_delegate choseRechargeWayWithWayName:model.class_name];
  222. }
  223. }
  224. }
  225. @end