ExchangeView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // ExchangeView.m
  3. // BuguLive
  4. //
  5. // Created by 王珂 on 17/3/14.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. //
  8. #import "ExchangeView.h"
  9. @interface ExchangeView()<UITextFieldDelegate>
  10. @property (nonatomic, strong) UIView *headerView;
  11. @property (nonatomic, strong) UIButton *rechargeBtn; //充值按钮
  12. @property (nonatomic, strong) UIButton *exchangeBtn; //兑换按钮
  13. @property (nonatomic, strong) UIButton *closeBtn; //关闭按钮
  14. @property (nonatomic, strong) UIView *lineView; //充值与兑换之间的分割线
  15. @property (nonatomic, strong) UILabel *diamondsLabel; //钻石余额
  16. @property (nonatomic, strong) UILabel *rateLabel; //兑换比例
  17. @property (nonatomic, strong) UITextField *textField;
  18. @property (nonatomic, strong) UILabel *gameCoinLabel;
  19. @property (nonatomic, strong) UIView *separeLineView;
  20. @property (nonatomic, strong) UIView *verticalLineView;
  21. @property (nonatomic, strong) UIButton *cancaleBtn; //取消按钮
  22. @property (nonatomic, strong) UIButton *makeSureBtn; //确定按钮
  23. @property (nonatomic, strong) UIView *selectLineView; //选中的分割线
  24. @end
  25. @implementation ExchangeView
  26. - (instancetype)initWithFrame:(CGRect)frame
  27. {
  28. if (self = [super initWithFrame:frame])
  29. {
  30. CGFloat bthOrignx = (self.width/2-80)/2;
  31. _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 54)];
  32. _headerView.backgroundColor = kAppRechargeSelectColor;
  33. [self addSubview:_headerView];
  34. _rechargeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  35. _rechargeBtn.titleLabel.font = kAppLargeTextFont;
  36. _rechargeBtn.frame = CGRectMake(bthOrignx, 30, 80, 18);
  37. [_rechargeBtn setTitle:ASLocalizedString(@"充值")forState:UIControlStateNormal];
  38. [_rechargeBtn setTitleColor:kAppGrayColor3 forState:UIControlStateNormal];
  39. [_rechargeBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
  40. [self addSubview:_rechargeBtn];
  41. _lineView = [[UIView alloc] initWithFrame:CGRectMake(self.width/2-0.5, 30, 1, 18)];
  42. _lineView.backgroundColor = kAppSpaceColor;
  43. [self addSubview:_lineView];
  44. _exchangeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  45. _exchangeBtn.titleLabel.font = kAppLargeTextFont;
  46. _exchangeBtn.frame = CGRectMake(self.width/2+bthOrignx, 30, 80, 18);
  47. [_exchangeBtn setTitle:ASLocalizedString(@"兑换")forState:UIControlStateNormal];
  48. [_exchangeBtn setTitleColor:kAppGrayColor1 forState:UIControlStateNormal];
  49. [_exchangeBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
  50. [self addSubview:_exchangeBtn];
  51. _closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  52. _closeBtn.frame = CGRectMake(self.width-30, 10, 20, 20);
  53. [_closeBtn setImage:[UIImage imageNamed:@"com_close_2"] forState:UIControlStateNormal];
  54. [_closeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  55. [_closeBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
  56. [self addSubview:_closeBtn];
  57. _selectLineView= [[UIView alloc] initWithFrame:CGRectMake(self.width/2+(self.width/2-50)/2, CGRectGetMaxY(_rechargeBtn.frame)+10, 50, 2)];
  58. _selectLineView.backgroundColor = kAppGrayColor1;
  59. [self addSubview:_selectLineView];
  60. _diamondsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_selectLineView.frame)+24, self.width, 15)];
  61. _diamondsLabel.text = [NSString stringWithFormat:ASLocalizedString(@"%@余额:1239398"),self.BuguLive.appModel.diamond_name];
  62. _diamondsLabel.font = kAppSmallTextFont;
  63. _diamondsLabel.textColor = kAppGrayColor3;
  64. _diamondsLabel.textAlignment = NSTextAlignmentCenter;
  65. [self addSubview:_diamondsLabel];
  66. _rateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(_diamondsLabel.frame)+24, self.width-20, 14)];
  67. _rateLabel.font = kAppSmallTextFont;
  68. _rateLabel.textColor = kAppGrayColor3;
  69. _rateLabel.text = ASLocalizedString(@"兑换比例:20");
  70. [self addSubview:_rateLabel];
  71. _textField = [[UITextField alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(_rateLabel.frame)+10, self.width-30, 25)];
  72. // _textField.borderStyle = UITextBorderStyleRoundedRect;
  73. _textField.placeholder = ASLocalizedString(@"请输入金额");
  74. _textField.keyboardType = UIKeyboardTypeNumberPad;
  75. _textField.delegate = self;
  76. _textField.font = kAppSmallTextFont;
  77. [self addSubview:_textField];
  78. [_textField addTarget:self action:@selector(changeGameCoinLabel) forControlEvents:UIControlEventEditingChanged];
  79. UIView * linView = [[UIView alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(_textField.frame), self.width-20, 0.5)];
  80. linView.backgroundColor = kAppSpaceColor;
  81. [self addSubview:linView];
  82. _gameCoinLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(_textField.frame)+15, self.width-20, 15)];
  83. _gameCoinLabel.text = ASLocalizedString(@"0游戏币");
  84. _gameCoinLabel.textColor = kAppGrayColor3;
  85. _gameCoinLabel.font = kAppSmallTextFont;
  86. _gameCoinLabel.textAlignment = NSTextAlignmentRight;
  87. [self addSubview:_gameCoinLabel];
  88. _separeLineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height-45.5, self.width, 0.5)];
  89. _separeLineView.alpha = 0.5;
  90. _separeLineView.backgroundColor = kAppSpaceColor;
  91. [self addSubview:_separeLineView];
  92. // _verticalLineView = [[UIView alloc] initWithFrame:CGRectMake(self.width/2-0.25, self.height-45, 0.5, 45)];
  93. // _verticalLineView.alpha = 0.5;
  94. // _verticalLineView.backgroundColor = kAppSpaceColor;
  95. // [self addSubview:_verticalLineView];
  96. CGFloat marginX = (self.width-200)/3;
  97. _cancaleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  98. _cancaleBtn.frame = CGRectMake(marginX, self.height-35, 100, 25);
  99. _cancaleBtn.layer.cornerRadius = 12.5;
  100. _cancaleBtn.layer.masksToBounds = YES;
  101. _cancaleBtn.layer.borderWidth = 0.5;
  102. _cancaleBtn.layer.borderColor = kAppGrayColor1.CGColor;
  103. _cancaleBtn.titleLabel.font = kAppSmallTextFont;
  104. [_cancaleBtn setTitleColor:kAppGrayColor1 forState:UIControlStateNormal];
  105. [_cancaleBtn setTitle:ASLocalizedString(@"取消")forState:UIControlStateNormal];
  106. [_cancaleBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
  107. [self addSubview:_cancaleBtn];
  108. _makeSureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  109. _makeSureBtn.frame = CGRectMake(CGRectGetMaxX(_cancaleBtn.frame)+marginX, self.height-35, 100, 25);
  110. _makeSureBtn.layer.cornerRadius = 12.5;
  111. _makeSureBtn.layer.masksToBounds = YES;
  112. _makeSureBtn.backgroundColor = kAppGrayColor1;
  113. _makeSureBtn.titleLabel.font = kAppSmallTextFont;
  114. [_makeSureBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
  115. [_makeSureBtn setTitle:ASLocalizedString(@"确定")forState:UIControlStateNormal];
  116. [_makeSureBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
  117. [self addSubview:_makeSureBtn];
  118. self.backgroundColor = [UIColor whiteColor];
  119. self.layer.cornerRadius = 5;
  120. self.layer.masksToBounds = YES;
  121. }
  122. return self;
  123. }
  124. - (void)changeGameCoinLabel
  125. {
  126. if (_textField.text.length>8)
  127. {
  128. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"单次兑换超过上限")];
  129. return;
  130. }
  131. NSInteger rateCoin = [_textField.text integerValue];
  132. rateCoin = rateCoin*[self.model.exchange_rate floatValue];
  133. _gameCoinLabel.text = [NSString stringWithFormat:ASLocalizedString(@"%zd游戏币"),rateCoin];
  134. }
  135. - (void)clickBtn:(UIButton *)button
  136. {
  137. if (button==_rechargeBtn)
  138. {
  139. [self endEditing:YES];
  140. self.hidden = YES;
  141. //self.frame =CGRectMake(10,kScreenH, kScreenW-20, 230);
  142. self.transform = CGAffineTransformIdentity;
  143. if (_delegate && [_delegate respondsToSelector:@selector(choseRecharge:orExchange:)])
  144. {
  145. [_delegate choseRecharge:YES orExchange:NO];
  146. }
  147. }
  148. else if (button==_exchangeBtn)
  149. {
  150. }
  151. else if (button == _closeBtn || button == _cancaleBtn)
  152. {
  153. [self cancleExchange];
  154. }
  155. else if (button == _makeSureBtn)
  156. {
  157. [self exchangeCoin];
  158. }
  159. }
  160. - (void)exchangeCoin
  161. {
  162. if (_textField.text.length == 0 || [_textField.text integerValue] == 0)
  163. {
  164. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请输入有效金额")];
  165. return;
  166. }
  167. if ([[IMAPlatform sharedInstance].host getDiamonds] < [_textField.text integerValue])
  168. {
  169. [FanweMessage alert:[NSString stringWithFormat:ASLocalizedString(@"当前%@不足"),self.BuguLive.appModel.diamond_name]];
  170. return;
  171. }
  172. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  173. [parmDict setObject:@"games" forKey:@"ctl"];
  174. [parmDict setObject:@"exchangeCoin" forKey:@"act"];
  175. [parmDict setObject:[NSString stringWithFormat:@"%@",_textField.text] forKey:@"diamonds"];
  176. FWWeakify(self)
  177. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  178. {
  179. FWStrongify(self)
  180. if ([responseJson toInt:@"status"]==1)
  181. {
  182. [[NSNotificationCenter defaultCenter]postNotificationName:@"updateCoin" object:nil];
  183. [self endEditing:YES];
  184. NSInteger diamonds = self.model.diamonds -[self.textField.text integerValue];
  185. self.model.diamonds = diamonds;
  186. self.diamondsLabel.text = [NSString stringWithFormat:ASLocalizedString(@"%@余额:%zd"),self.BuguLive.appModel.diamond_name,self.model.diamonds];
  187. [UIView animateWithDuration:0.5 animations:^{
  188. self.frame =CGRectMake(10,kScreenH, kScreenW-20, 230);
  189. } completion:^(BOOL finished) {
  190. self.hidden = YES;
  191. self.textField.text = nil;
  192. self.gameCoinLabel.text = ASLocalizedString(@"0游戏币");
  193. }];
  194. }
  195. } FailureBlock:^(NSError *error)
  196. {
  197. }];
  198. }
  199. - (void)setModel:(AccountRechargeModel *)model
  200. {
  201. _model = model;
  202. self.rateLabel.text = [NSString stringWithFormat:ASLocalizedString(@"兑换比例: %@"),model.exchange_rate];
  203. self.diamondsLabel.text = [NSString stringWithFormat:ASLocalizedString(@"%@余额:%zd"),self.BuguLive.appModel.diamond_name,model.diamonds];
  204. }
  205. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  206. {
  207. if (textField == _textField)
  208. {
  209. if (![string isNumber])
  210. {
  211. return NO;
  212. }
  213. if (string.length == 0)
  214. return YES;
  215. NSInteger existedLength = textField.text.length;
  216. NSInteger selectedLength = range.length;
  217. NSInteger replaceLength = string.length;
  218. if (existedLength - selectedLength + replaceLength > 6) {
  219. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"单次兑换超过上限")];
  220. return NO;
  221. }
  222. }
  223. return YES;
  224. }
  225. - (void)cancleExchange
  226. {
  227. [self endEditing:YES];
  228. [UIView animateWithDuration:0.5 animations:^{
  229. //self.frame =CGRectMake(10,kScreenH, kScreenW-20, 230);
  230. self.transform = CGAffineTransformIdentity;
  231. } completion:^(BOOL finished) {
  232. self.hidden = YES;
  233. _textField.text = nil;
  234. _gameCoinLabel.text = ASLocalizedString(@"0游戏币");
  235. }];
  236. }
  237. @end