MONewComerGiftView.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. //
  2. // MONewComerGiftView.m
  3. // MiMoLive
  4. //
  5. // Created by MiMo on 2025/6/8.
  6. //
  7. #import "MONewComerGiftView.h"
  8. #import "UILabel+MOBezierAnimation.h"
  9. #import "MOUserGift.h"
  10. @interface MONewComerGiftItemView ()
  11. @property (nonatomic, strong) UIView *backgroundView;
  12. @property (nonatomic, strong) UIImageView *iconImgView;
  13. @property (nonatomic, strong) UILabel *amountLabel;
  14. @property (nonatomic, strong) UILabel *nameLabel;
  15. @end
  16. @implementation MONewComerGiftItemView
  17. - (instancetype)initWithFrame:(CGRect)frame {
  18. if (self = [super initWithFrame:frame]) {
  19. [self setupUI];
  20. }
  21. return self;
  22. }
  23. - (void)layoutSubviews {
  24. [super layoutSubviews];
  25. [self.backgroundView setupGradientLayerWithView:self.backgroundView
  26. startColor:[MOTools colorWithHexString:@"#FFFBFD"]
  27. endColor:[MOTools colorWithHexString:@"#FFE4F2"]
  28. layerName:@"gradientLayer"
  29. startPoint:(CGPoint) { 0.5, 0 }
  30. endPoint:(CGPoint){0.5, 1}];
  31. }
  32. - (void)setGiftModel:(MOUserGift *)giftModel {
  33. _giftModel = giftModel;
  34. [self.iconImgView sd_setImageWithURL:[NSURL URLWithString:giftModel.image]];
  35. self.nameLabel.text = [NSString stringWithFormat:@"%@", giftModel.name];
  36. NSString *amountText = nil;
  37. if (giftModel.type == 1) {//钻石 *num
  38. amountText = [NSString stringWithFormat:@"* %zd", giftModel.amount];
  39. self.nameLabel.hidden = YES;
  40. [self.amountLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
  41. make.top.equalTo(self.backgroundView.mas_bottom).offset(2);
  42. make.left.right.mas_equalTo(0);
  43. make.height.mas_equalTo(20);
  44. }];
  45. } else if (giftModel.type == 2) {//礼物 *num
  46. amountText = [NSString stringWithFormat:@"* %zd", giftModel.amount];
  47. } else if (giftModel.type == 3) {//道具 *d
  48. amountText = [NSString stringWithFormat:@"* %zdd", giftModel.amount];
  49. } else if (giftModel.type == 4) {//勋章 *d
  50. amountText = [NSString stringWithFormat:@"* %zdd", giftModel.amount];
  51. }
  52. self.amountLabel.text = amountText;
  53. }
  54. - (void)setupUI {
  55. [self addSubview:self.backgroundView];
  56. [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
  57. make.top.mas_equalTo(0);
  58. make.centerX.mas_equalTo(0);
  59. make.width.mas_equalTo(71);
  60. make.height.mas_equalTo(71);
  61. }];
  62. [self addSubview:self.iconImgView];
  63. [self.iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  64. make.center.equalTo(self.backgroundView);
  65. make.width.mas_equalTo(43);
  66. make.height.mas_equalTo(43);
  67. }];
  68. [self addSubview:self.nameLabel];
  69. [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  70. make.top.equalTo(self.backgroundView.mas_bottom).offset(2);
  71. make.left.right.mas_equalTo(0);
  72. make.height.mas_equalTo(20);
  73. }];
  74. [self addSubview:self.amountLabel];
  75. [self.amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  76. make.top.equalTo(self.nameLabel.mas_bottom);
  77. make.left.right.mas_equalTo(0);
  78. make.height.mas_equalTo(20);
  79. }];
  80. }
  81. - (UIView *)backgroundView {
  82. if (!_backgroundView) {
  83. _backgroundView = [[UIView alloc] init];
  84. _backgroundView.layer.masksToBounds = YES;
  85. _backgroundView.layer.borderColor = [MOTools colorWithHexString:@"#FFA2D1"].CGColor;
  86. _backgroundView.layer.borderWidth = 1;
  87. _backgroundView.layer.cornerRadius = 9;
  88. }
  89. return _backgroundView;
  90. }
  91. - (UIImageView *)iconImgView {
  92. if (!_iconImgView) {
  93. _iconImgView = [[UIImageView alloc] init];
  94. _iconImgView.contentMode = UIViewContentModeScaleAspectFill;
  95. }
  96. return _iconImgView;
  97. }
  98. - (UILabel *)amountLabel {
  99. if (!_amountLabel) {
  100. _amountLabel = [[UILabel alloc] init];
  101. _amountLabel.font = [MOTextTools mediumFont:13];
  102. _amountLabel.textColor = kBaseTextColor_2;
  103. _amountLabel.textAlignment = NSTextAlignmentCenter;
  104. }
  105. return _amountLabel;
  106. }
  107. - (UILabel *)nameLabel {
  108. if (!_nameLabel) {
  109. _nameLabel = [[UILabel alloc] init];
  110. _nameLabel.font = [MOTextTools mediumFont:13];
  111. _nameLabel.textColor = kBaseTextColor_2;
  112. _nameLabel.textAlignment = NSTextAlignmentCenter;
  113. }
  114. return _nameLabel;
  115. }
  116. @end
  117. #define kMONewComerGiftViewWidth 297
  118. #define kMOActivityRewardViewHeight 393
  119. #import "MONewComerGiftView.h"
  120. @interface MONewComerGiftView ()
  121. @property (nonatomic, strong) UIView *bgView;
  122. @property (nonatomic, strong) UIImageView *bgImgView;
  123. @property (nonatomic, strong) UIImageView *giftIconView;
  124. @property (nonatomic, strong) UILabel *titleLabel;
  125. @property (nonatomic, strong) UILabel *descLabel;
  126. @property (nonatomic, strong) UIView *contentView;
  127. @property (nonatomic, strong) UIButton *getButton;
  128. @property (nonatomic, strong) UIButton *closeButton;
  129. @end
  130. @implementation MONewComerGiftView
  131. - (instancetype)init {
  132. if (self = [super init]) {
  133. [self setupUI];
  134. }
  135. return self;
  136. }
  137. - (void)setupUI {
  138. [self addSubview:self.bgView];
  139. [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  140. make.edges.mas_equalTo(0);
  141. }];
  142. [self addSubview:self.bgImgView];
  143. [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  144. make.centerX.mas_equalTo(0);
  145. make.centerY.mas_equalTo(-25);
  146. make.size.mas_equalTo(CGSizeMake(kMONewComerGiftViewWidth, kMOActivityRewardViewHeight));
  147. }];
  148. [self.bgImgView addSubview:self.giftIconView];
  149. [self.giftIconView mas_makeConstraints:^(MASConstraintMaker *make) {
  150. make.top.equalTo(self.bgImgView).offset(-26);
  151. make.right.equalTo(self.bgImgView);
  152. make.size.mas_equalTo(CGSizeMake(101, 103));
  153. }];
  154. [self.bgImgView addSubview:self.titleLabel];
  155. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  156. make.left.mas_equalTo(16);
  157. make.top.mas_equalTo(14);
  158. make.right.mas_equalTo(-110);
  159. }];
  160. [self.bgImgView addSubview:self.descLabel];
  161. [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  162. make.left.mas_equalTo(16);
  163. make.top.equalTo(self.titleLabel.mas_bottom);
  164. make.right.mas_equalTo(-90);
  165. }];
  166. [self.bgImgView addSubview:self.contentView];
  167. [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
  168. make.top.mas_equalTo(68);
  169. make.left.mas_equalTo(13);
  170. make.right.mas_equalTo(-13);
  171. make.bottom.mas_equalTo(-69);
  172. }];
  173. [self.bgImgView addSubview:self.getButton];
  174. [self.getButton mas_makeConstraints:^(MASConstraintMaker *make) {
  175. make.centerX.mas_equalTo(0);
  176. make.width.mas_equalTo(255);
  177. make.height.mas_equalTo(44);
  178. make.bottom.mas_equalTo(-15);
  179. }];
  180. [self addSubview:self.closeButton];
  181. [self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
  182. make.top.mas_equalTo(self.bgImgView.mas_bottom).offset(17);
  183. make.centerX.mas_equalTo(0);
  184. make.size.mas_equalTo(CGSizeMake(32, 32));
  185. }];
  186. [self setNeedsLayout];
  187. [self layoutIfNeeded];
  188. [self.titleLabel setGradientWithColors:@[(id)[MOTools colorWithHexString:@"#FF4DA6" alpha:1.0].CGColor,(id)[MOTools colorWithHexString:@"#4363FF" alpha:1.0].CGColor] startPoint:CGPointMake(0.0, 0.0) endPoint:CGPointMake(1.0, 0.0)];
  189. [self.contentView setupGradientLayerWithView:self.contentView
  190. startColor:[MOTools colorWithHexString:@"#FFFFFF" alpha:0.5]
  191. endColor:[MOTools colorWithHexString:@"#FFFFFF" alpha:0.8]
  192. layerName:@"gradientLayer"
  193. startPoint:(CGPoint) { 0.5, 0 }
  194. endPoint:(CGPoint){0.5, 1}];
  195. }
  196. - (void)setModel:(MODialogsData *)model {
  197. _model = model;
  198. [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  199. NSArray *dataList = model.userGifts;
  200. NSInteger count = dataList.count;
  201. if (count == 0) return;
  202. CGFloat topPadding = 12;
  203. CGFloat headPadding = 8;//相邻item上下间距固定8
  204. CGFloat leftPadding = (count > 4 && count <= 6) ? 12 : 19;
  205. CGFloat interItemSpacing = (count > 4 && count <= 6) ? 18 : 15;//相邻item左右间距
  206. CGFloat itemWidth = (count > 4 && count <= 6) ? 71 : 110;
  207. CGFloat itemHeight = 112;
  208. NSInteger numberOfColumns = (count > 4 && count <= 6) ? 3 : 2;
  209. for (NSInteger i = 0; i < count; i++) {
  210. NSInteger row = i / numberOfColumns;
  211. NSInteger column = i % numberOfColumns;
  212. CGFloat x = column == 0 ? leftPadding : (leftPadding + column * (itemWidth + interItemSpacing));
  213. CGFloat y = topPadding + row * (itemHeight + headPadding);
  214. MONewComerGiftItemView *itemView = [[MONewComerGiftItemView alloc] initWithFrame:CGRectMake(x, y, itemWidth, itemHeight)];
  215. itemView.giftModel = dataList[i];
  216. [self.contentView addSubview:itemView];
  217. }
  218. }
  219. - (void)getBtnAction {
  220. if (self.getBlock) {
  221. self.getBlock();
  222. }
  223. [self dismiss];
  224. }
  225. - (void)show {
  226. UIWindow *keyWindow = [[UIApplication sharedApplication] delegate].window;
  227. [keyWindow addSubview:self];
  228. self.frame = CGRectMake(0.0, 0.0, SCREENWIDTH, SCREENHEIGHT);
  229. //动画效果
  230. self.bgImgView.transform = CGAffineTransformMakeScale(1.3, 1.3);
  231. self.bgImgView.alpha = 0;
  232. [UIView animateWithDuration:0.2 animations:^ {
  233. self.bgImgView.transform = CGAffineTransformMakeScale(1.0, 1.0);
  234. self.bgImgView.alpha = 1;
  235. } completion:nil];
  236. }
  237. - (void)dismiss {
  238. [UIView animateWithDuration:0.2 animations:^ {
  239. self.bgImgView.transform = CGAffineTransformMakeScale(1.3, 1.3);
  240. self.bgImgView.alpha = 0;
  241. } completion:^(BOOL finished) {
  242. if (finished) {
  243. [self removeFromSuperview];
  244. if (self.dismissBlock) {
  245. self.dismissBlock();
  246. }
  247. }
  248. }];
  249. }
  250. - (UIView *)bgView {
  251. if (!_bgView) {
  252. _bgView = [[UIView alloc] init];
  253. _bgView.backgroundColor = [MOTools colorWithHexString:@"#000000" alpha:0.5];
  254. // UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
  255. // [_bgView addGestureRecognizer:tap];
  256. }
  257. return _bgView;
  258. }
  259. - (UIImageView *)bgImgView {
  260. if (!_bgImgView) {
  261. _bgImgView = [[UIImageView alloc] init];
  262. _bgImgView.contentMode = UIViewContentModeScaleAspectFill;
  263. _bgImgView.image = [UIImage imageNamed:@"image_new_comer_gift_bg"];
  264. _bgImgView.userInteractionEnabled = YES;
  265. }
  266. return _bgImgView;
  267. }
  268. - (UIImageView *)giftIconView {
  269. if (!_giftIconView) {
  270. _giftIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_new_user_gift"]];
  271. }
  272. return _giftIconView;
  273. }
  274. - (UILabel *)titleLabel {
  275. if (!_titleLabel) {
  276. _titleLabel = [[UILabel alloc] init];
  277. _titleLabel.text = NSLocalString(@"mimo_2_new_user_gift_title");
  278. _titleLabel.font = [MOTextTools poppinsExtraBoldFont:17];
  279. _titleLabel.numberOfLines = 1;
  280. _titleLabel.adjustsFontSizeToFitWidth = YES;
  281. }
  282. return _titleLabel;
  283. }
  284. - (UILabel *)descLabel {
  285. if (!_descLabel) {
  286. _descLabel = [[UILabel alloc] init];
  287. _descLabel.text = NSLocalString(@"mimo_2_new_user_gift_desc");
  288. _descLabel.font = [MOTextTools regularFont:12];
  289. _descLabel.textColor = kBaseTextColor_3;
  290. _descLabel.numberOfLines = 1;
  291. _descLabel.adjustsFontSizeToFitWidth = YES;
  292. }
  293. return _descLabel;
  294. }
  295. - (UIView *)contentView {
  296. if (!_contentView) {
  297. _contentView = [[UIView alloc] init];
  298. _contentView.layer.masksToBounds = YES;
  299. _contentView.layer.cornerRadius = 12;
  300. }
  301. return _contentView;
  302. }
  303. - (UIButton *)getButton {
  304. if (!_getButton) {
  305. _getButton = [[UIButton alloc] init];
  306. NSArray *colorArr = @[kBaseColorLeft,kBaseColorRight];
  307. UIImage *colorImage = [MOTools createGradientRectImageWithBounds:CGRectMake(0, 0, 255, 44.0) Colors:colorArr GradientType:0];
  308. [_getButton setBackgroundImage:colorImage forState:UIControlStateNormal];
  309. _getButton.layer.cornerRadius = 12;
  310. _getButton.layer.masksToBounds = YES;
  311. [_getButton setTitle:NSLocalString(@"mimo_2_new_user_gift_get") forState:UIControlStateNormal];
  312. [_getButton setTitleColor:[MOTools colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
  313. _getButton.titleLabel.font = [MOTextTools poppinsMediumFont:16];
  314. [_getButton addTarget:self action:@selector(getBtnAction) forControlEvents:UIControlEventTouchUpInside];
  315. }
  316. return _getButton;
  317. }
  318. - (UIButton *)closeButton {
  319. if (!_closeButton) {
  320. _closeButton = [[UIButton alloc] init];
  321. [_closeButton setImage:[UIImage imageNamed:@"icon_close_circle_white"] forState:UIControlStateNormal];
  322. [_closeButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
  323. }
  324. return _closeButton;
  325. }
  326. @end