MORedPacketCustomView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. //
  2. // MORedPacketCustomView.m
  3. // MiMoLive
  4. //
  5. // Created by MiMo on 2025/5/16.
  6. //
  7. #import "MORedPacketCustomView.h"
  8. #import "MORedSendConfigs.h"
  9. @interface MORedPacketCustomView ()
  10. /** 背景 */
  11. @property (nonatomic, strong) UIView *bgView;
  12. @property (nonatomic, strong) UIView *containerView;
  13. @property (nonatomic, strong) UILabel *titleLabel;
  14. @property (nonatomic, strong) UIView *customContainerView;
  15. @property (nonatomic, strong) UITextField *textField;
  16. @property (nonatomic, strong) BigBtn *minusBtn;
  17. @property (nonatomic, strong) BigBtn *plusBtn;
  18. @property (nonatomic, strong) BigBtn *cancelBtn;
  19. @property (nonatomic, strong) BigBtn *confirmBtn;
  20. @end
  21. @implementation MORedPacketCustomView
  22. - (instancetype)init {
  23. if (self = [super init]) {
  24. [self setupUI];
  25. }
  26. return self;
  27. }
  28. - (void)setupUI{
  29. UIWindow *keyWindow = [[UIApplication sharedApplication] delegate].window;
  30. [keyWindow addSubview:self.bgView];
  31. [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  32. make.top.left.bottom.right.equalTo(keyWindow);
  33. }];
  34. [keyWindow addSubview:self.containerView];
  35. [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
  36. make.centerY.equalTo(keyWindow);
  37. make.centerX.equalTo(keyWindow);
  38. make.width.mas_equalTo(305);
  39. make.height.mas_equalTo(213);
  40. }];
  41. //标题
  42. [self.containerView addSubview:self.titleLabel];
  43. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  44. make.left.equalTo(self.containerView).with.offset(16.0);
  45. make.right.equalTo(self.containerView).with.offset(-16.0);
  46. make.top.equalTo(self.containerView).with.offset(25);
  47. }];
  48. [self.containerView addSubview:self.customContainerView];
  49. [self.customContainerView mas_makeConstraints:^(MASConstraintMaker *make) {
  50. make.top.equalTo(self.titleLabel.mas_bottom).offset(25);
  51. make.centerX.mas_equalTo(0);
  52. make.width.mas_equalTo(198);
  53. make.height.mas_equalTo(49);
  54. }];
  55. [self.customContainerView addSubview:self.minusBtn];
  56. [self.minusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  57. make.centerY.mas_equalTo(0);
  58. make.left.mas_equalTo(30);
  59. make.size.mas_equalTo(CGSizeMake(10, 10));
  60. }];
  61. [self.customContainerView addSubview:self.textField];
  62. [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
  63. make.center.mas_equalTo(0);
  64. make.size.mas_equalTo(CGSizeMake(80, 19));
  65. }];
  66. [self.customContainerView addSubview:self.plusBtn];
  67. [self.plusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  68. make.centerY.mas_equalTo(0);
  69. make.right.mas_equalTo(-30);
  70. make.size.mas_equalTo(CGSizeMake(10, 10));
  71. }];
  72. //取消按钮
  73. [self.containerView addSubview:self.cancelBtn];
  74. [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  75. make.bottom.equalTo(self.containerView).offset(-25);
  76. make.left.equalTo(self.containerView).offset(25);
  77. make.width.mas_equalTo(120);
  78. make.height.mas_equalTo(44);
  79. }];
  80. //确定Button
  81. [self.containerView addSubview:self.confirmBtn];
  82. [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  83. make.left.equalTo(self.cancelBtn.mas_right).offset(14);
  84. make.width.mas_equalTo(122);
  85. make.height.mas_equalTo(46);
  86. make.centerY.equalTo(self.cancelBtn);
  87. }];
  88. }
  89. #pragma mark - my method
  90. - (void)setType:(CustomRedPacketType)type {
  91. _type = type;
  92. if (type == CustomRedPacketTypeAmount) {
  93. self.titleLabel.text = NSLocalString(@"mimo_red_envelope_diamond_title");
  94. } else {
  95. self.textField.rightView = nil;
  96. self.titleLabel.text = NSLocalString(@"mimo_red_envelope_quantity_title");
  97. }
  98. }
  99. - (void)setConfig:(MORedSendConfigs *)config {
  100. _config = config;
  101. if (self.type == CustomRedPacketTypeAmount) {
  102. self.textField.text = [NSString stringWithFormat:@"%.0f", config.minAmount];
  103. if (config.customAmountValue > 0) {
  104. self.textField.text = [NSString stringWithFormat:@"%zd", config.customAmountValue];
  105. }
  106. } else {
  107. self.textField.text = [NSString stringWithFormat:@"%.0f", config.minNum];
  108. if (config.customQuantityValue > 0) {
  109. self.textField.text = [NSString stringWithFormat:@"%zd", config.customQuantityValue];
  110. }
  111. }
  112. }
  113. //界面显示动画
  114. - (void)show {
  115. UIWindow *keyWindow = [[UIApplication sharedApplication] delegate].window;
  116. [keyWindow addSubview:self];
  117. //动画效果
  118. self.containerView.transform = CGAffineTransformMakeScale(1.3, 1.3);
  119. self.containerView.alpha = 0;
  120. [UIView animateWithDuration:0.2 animations:^ {
  121. self.containerView.transform = CGAffineTransformMakeScale(1.0, 1.0);
  122. self.containerView.alpha = 1;
  123. } completion:^(BOOL finished) {
  124. }];
  125. }
  126. //点击确认
  127. - (void)confirmBtnClick {
  128. //点击确认
  129. if (self.confirmBlock) {
  130. self.confirmBlock([self.textField.text integerValue]);
  131. }
  132. [self dismiss];
  133. }
  134. - (void)cancelBtnClick {
  135. if (self.cancelBlock) {
  136. self.cancelBlock();
  137. }
  138. [self dismiss];
  139. }
  140. - (void)minusBtnClick {
  141. NSInteger offset = 0;
  142. NSInteger limitValue = 0;
  143. if (self.type == CustomRedPacketTypeAmount) {//金额
  144. if (self.config.scope == 1) { // 普通红包
  145. offset = 1000;
  146. } else {//全服红包
  147. offset = 10000;
  148. }
  149. limitValue = self.config.minAmount;
  150. } else {//数量
  151. offset = 1;
  152. limitValue = self.config.minNum;
  153. }
  154. NSInteger value = [self.textField.text integerValue];
  155. value -= offset;
  156. if (value < limitValue) {
  157. value = limitValue;
  158. };
  159. self.textField.text = [NSString stringWithFormat:@"%zd", value];
  160. }
  161. - (void)plusBtnClick {
  162. NSInteger offset = 0;
  163. NSInteger limitValue = 0;
  164. if (self.type == CustomRedPacketTypeAmount) {//金额
  165. if (self.config.scope == 1) { // 普通红包
  166. offset = 1000;
  167. } else {//全服红包
  168. offset = 10000;
  169. }
  170. limitValue = self.config.maxAmount;
  171. } else {//数量
  172. offset = 1;
  173. limitValue = self.config.maxNum;
  174. }
  175. NSInteger value = [self.textField.text integerValue];
  176. value += offset;
  177. if (value > limitValue) {
  178. value = limitValue;
  179. };
  180. self.textField.text = [NSString stringWithFormat:@"%zd", value];
  181. }
  182. //取消掉键盘
  183. - (void)dismiss {
  184. [UIView animateWithDuration:0.2 animations:^ {
  185. self.containerView.transform = CGAffineTransformMakeScale(1.3, 1.3);
  186. self.containerView.alpha = 0;
  187. } completion:^(BOOL finished) {
  188. if (finished) {
  189. [self.bgView removeFromSuperview];
  190. [self.containerView removeFromSuperview];
  191. [self removeFromSuperview];
  192. }
  193. }];
  194. }
  195. - (void)textFieldValueChanged:(UITextField *)textField {
  196. NSInteger minNum = 0;
  197. NSInteger maxNum = 0;
  198. if (self.type == CustomRedPacketTypeAmount) {//金额
  199. minNum = self.config.minAmount;
  200. maxNum = self.config.maxAmount;
  201. } else {//数量
  202. minNum = self.config.minNum;
  203. maxNum = self.config.maxNum;
  204. }
  205. NSInteger value = [textField.text integerValue];
  206. BOOL valid = (value >= minNum && value <= maxNum);
  207. self.confirmBtn.enabled = valid;
  208. self.confirmBtn.alpha = valid ? 1.0 : 0.8;
  209. }
  210. #pragma mark - 懒加载
  211. - (UIView *)bgView {
  212. if (!_bgView) {
  213. _bgView = [[UIView alloc] init];
  214. _bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
  215. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgViewTapClick)];
  216. [_bgView addGestureRecognizer:tap];
  217. }
  218. return _bgView;
  219. }
  220. - (void)bgViewTapClick {
  221. [self dismiss];
  222. }
  223. - (UIView *)containerView {
  224. if (!_containerView) {
  225. _containerView = [[UIView alloc] init];
  226. _containerView.backgroundColor = [MOTools colorWithHexString:@"#FFF5F1" alpha:1.0];
  227. _containerView.layer.masksToBounds = YES;
  228. _containerView.layer.cornerRadius = 20.0;
  229. }
  230. return _containerView;
  231. }
  232. - (UILabel *)titleLabel {
  233. if (!_titleLabel) {
  234. _titleLabel = [[UILabel alloc] init];
  235. _titleLabel.textColor = [MOTools colorWithHexString:@"#8A3900" alpha:1.0];;
  236. _titleLabel.font = [MOTextTools getTheFontWithSize:17.0 AndFontName:kNormalContentBlodFontStr];
  237. _titleLabel.textAlignment = NSTextAlignmentCenter;
  238. _titleLabel.numberOfLines = 0;
  239. _titleLabel.text = @"";
  240. }
  241. return _titleLabel;
  242. }
  243. - (UIView *)customContainerView {
  244. if (!_customContainerView) {
  245. _customContainerView = [[UIView alloc] init];
  246. _customContainerView.backgroundColor = [MOTools colorWithHexString:@"#FFFFFF" alpha:1.0];
  247. _customContainerView.layer.masksToBounds = YES;
  248. _customContainerView.layer.cornerRadius = 13.0;
  249. }
  250. return _customContainerView;
  251. }
  252. - (BigBtn *)minusBtn {
  253. if (!_minusBtn) {
  254. _minusBtn = [[BigBtn alloc] init];
  255. [_minusBtn addTarget:self action:@selector(minusBtnClick) forControlEvents:UIControlEventTouchUpInside];
  256. [_minusBtn setImage:[UIImage imageNamed:@"icon_red_packet_minus"] forState:UIControlStateNormal];
  257. }
  258. return _minusBtn;
  259. }
  260. - (UITextField *)textField {
  261. if (!_textField) {
  262. _textField = [[UITextField alloc] init];
  263. _textField.textAlignment = NSTextAlignmentCenter;
  264. _textField.textColor = [MOTools colorWithHexString:@"#8A3900"];
  265. _textField.font = [MOTextTools getTheFontWithSize:16.0 AndFontName:kNormalContentBlodFontStr];
  266. _textField.keyboardType = UIKeyboardTypeNumberPad;
  267. [self.textField addTarget:self action:@selector(textFieldValueChanged:) forControlEvents:UIControlEventEditingChanged];
  268. UIImageView *diamondIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_red_list_diamond"]];
  269. diamondIconView.frame = CGRectMake(0, 0, 19, 14);
  270. _textField.rightView = diamondIconView;
  271. _textField.rightViewMode = UITextFieldViewModeAlways;
  272. }
  273. return _textField;
  274. }
  275. - (BigBtn *)plusBtn {
  276. if (!_plusBtn) {
  277. _plusBtn = [[BigBtn alloc] init];
  278. [_plusBtn addTarget:self action:@selector(plusBtnClick) forControlEvents:UIControlEventTouchUpInside];
  279. [_plusBtn setImage:[UIImage imageNamed:@"icon_red_packet_plus"] forState:UIControlStateNormal];
  280. }
  281. return _plusBtn;
  282. }
  283. - (BigBtn *)cancelBtn {
  284. if (!_cancelBtn) {
  285. _cancelBtn = [[BigBtn alloc] init];
  286. _cancelBtn.titleLabel.font = [MOTextTools getTheFontWithSize:15.0 AndFontName:kNormalContentFontStr];
  287. [_cancelBtn setBackgroundColor:[MOTools colorWithHexString:@"#FFFFFF" alpha:1.0]];
  288. _cancelBtn.layer.masksToBounds = YES;
  289. _cancelBtn.layer.cornerRadius = 22;
  290. [_cancelBtn setTitle:@"Cancel" forState:UIControlStateNormal];
  291. [_cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
  292. [_cancelBtn setTitleColor:[MOTools colorWithHexString:@"#666666"] forState:UIControlStateNormal];
  293. }
  294. return _cancelBtn;
  295. }
  296. - (BigBtn *)confirmBtn {
  297. if (!_confirmBtn) {
  298. _confirmBtn = [[BigBtn alloc] init];
  299. _confirmBtn.titleLabel.font = [MOTextTools getTheFontWithSize:15.0 AndFontName:kNormalContentBlodFontStr];
  300. [_confirmBtn addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
  301. [_confirmBtn setTitle:@"Save" forState:UIControlStateNormal];
  302. [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  303. [_confirmBtn setBackgroundImage:[UIImage imageNamed:@"icon_send_red_packet_btn"] forState:UIControlStateNormal];
  304. }
  305. return _confirmBtn;
  306. }
  307. @end