123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- //
- // XYGiveRewardAlertViewController.m
- // Timi
- //
- // Created by 翟玉磊 on 2021/11/16.
- //
- #import "XYGiveRewardAlertViewController.h"
- #import "XYGiftAPIManager.h"
- #import "XYGiveRewardAlertGiftItemCollectionViewCell.h"
- #import "XYGiveRewardGiftModel.h"
- #import "XYUserDynamicListCellModel.h"
- #import "XYRechargeItemsViewController.h"
- /// 左右距离父屏幕边距
- #define Left_Spacing 48.0f
- /// 内容view的宽度
- #define ContentView_Width (SCREEN_WIDTH - Left_Spacing * 2)
- typedef void(^SelectedDidBlock)(BOOL success, XYUserDynamicListCellModel *dynamicModel);
- @interface XYGiveRewardAlertViewController ()<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
- @property (nonatomic, readwrite, strong) UIView *bgView;
- @property (nonatomic, readwrite, strong) UIView *contentView;
- @property (nonatomic, readwrite, strong) UILabel *titleLabel;
- @property (nonatomic, readwrite, strong) UILabel *messageLabel;
- @property (nonatomic, readwrite, strong) UICollectionView *collectionView;
- @property (nonatomic, readwrite, strong) UIButton *cancelButton;
- @property (nonatomic, readwrite, strong) UIButton *sureButton;
- @property (nonatomic, strong) XYUserDynamicListCellModel *dynamicModel;
- @property (nonatomic, readwrite, copy) SelectedDidBlock selectedDidBlock;
- @property (nonatomic, strong) NSMutableArray *dataSource;
- @end
- @implementation XYGiveRewardAlertViewController
- #pragma mark — Publiv
- + (instancetype)showWithDynamicModel:(XYUserDynamicListCellModel *)dynamicModel parentController:(UIViewController *)parentController completeBlock:(void(^)(BOOL success, XYUserDynamicListCellModel *dynamicModel))block {
-
- XYGiveRewardAlertViewController *alertVC = XYGiveRewardAlertViewController.new;
- alertVC.dynamicModel = dynamicModel;
- alertVC.selectedDidBlock = block;
- alertVC.view.hidden = YES;
- // 添加控制器
- [parentController addChildViewController:alertVC];
- [parentController.view addSubview:alertVC.view];
- [alertVC didMoveToParentViewController:parentController];
-
- return alertVC;
- }
- - (void)show {
- self.view.hidden = NO;
-
- [self showAnimation];
- [self showBackground];
- }
- - (void)dismiss {
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [self hiddenAnimation];
- });
- }
- /// 重写init方法,配置你想要的属性
- - (instancetype)init
- {
- self = [super init];
- if (self) {
-
- }
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- /// 设置
- [self _setup];
-
- /// 设置导航栏
- [self _setupNavigationItem];
-
- /// 设置子控件
- [self _setupSubViews];
-
- /// 布局子空间
- [self _makeSubViewsConstraints];
- }
- #pragma mark — Override
- - (void)bindViewModel {
-
- }
- - (void)requestRemoteData {
- [[XYGiftAPIManager new] getWishListSuccessHandler:^(ZYLResponseModel *responseModel) {
-
- [self.dataSource removeAllObjects];
- for (NSDictionary *dict in responseModel.data[@"list"]) {
- XYGiveRewardGiftModel *model = XYGiveRewardGiftModel.new;
- [model yy_modelSetWithDictionary:dict];
- [self.dataSource addObject:model];
- }
-
- // 刷新frame
- [self updateCollectionFrame];
-
- [self.collectionView reloadData];
-
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- // 默认选中状态
- [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionNone];
- if ([self.collectionView.delegate respondsToSelector:@selector(collectionView:didSelectItemAtIndexPath:)]) {
- [self.collectionView.delegate collectionView:self.collectionView didSelectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]];
- }
- });
-
- [self show];
- } failureHandler:^(ZYLNetworkError *error) {
- [SVProgressHUD showInfoWithStatus:error.domain];
- [self dismiss];
- }];
- }
- - (void)updateCollectionFrame {
- NSInteger count = self.dataSource.count;
- CGFloat itemWidth = 72.0f;
- CGFloat itemSpacing = 8.0f;
- CGFloat leftAndRight = 24.0f;
- CGFloat allWidth = leftAndRight + itemWidth * count + itemSpacing * (count - 1) + leftAndRight;
- if (allWidth > self.contentView.f_width) {
- // 不居中
- [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.contentView).offset(24.0f);
- make.right.equalTo(self.contentView).offset(-24.0f);
- }];
- }else {
- // 居中
- CGFloat x = (self.contentView.f_width - (itemWidth * count + itemSpacing * (count - 1)))/2;
- [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.contentView).offset(x);
- make.right.equalTo(self.contentView).offset(-x);
- }];
- }
- }
- #pragma mark - 事件处理Or辅助方法
- - (void)cancelButtonAction:(id)sender {
- if (self.selectedDidBlock) {
- self.selectedDidBlock(NO, self.dynamicModel);
- }
- [self dismiss];
- }
- - (void)sureButtonAction:(id)sender {
-
- NSString *giftId = @"";
- for (XYGiveRewardGiftModel *model in self.dataSource) {
- if (model.status) {
- giftId = [model.giftId copy];
- break;
- }
- }
- if (StringIsEmpty(giftId)) {
- [SVProgressHUD showInfoWithStatus:@"请选择礼物"];
- return;
- }
- [SVProgressHUD show];
- [[XYGiftAPIManager new] sendRewardGiftWithFeedId:self.dynamicModel.feedId giftId:giftId quantity:1 SuccessHandler:^(ZYLResponseModel *responseModel) {
- NSString *balance = [BaseMethod toString:responseModel.data[@"balance"]];
- if ([balance integerValue] > 0) {
- [SVProgressHUD showSuccessWithStatus:@"赠送成功"];
- NSString *totalPrice = [BaseMethod toString:responseModel.data[@"totalPrice"]];
- self.dynamicModel.sendGiftNum = totalPrice;
- self.dynamicModel.hasSendGift = YES;
- if (self.selectedDidBlock) {
- self.selectedDidBlock(YES, self.dynamicModel);
- }
- }else {
- [SVProgressHUD dismiss];
- // 余额不足
- CustomActionAlertController *al = [[CustomActionAlertController alloc] initWithTitle:kLocalizedString(@"温馨提示") message:kLocalizedString(@"账户余额不足,请前去充值") sure:kLocalizedString(@"去充值") cancel:kLocalizedString(@"取消") selctedBlock:^(NSInteger index, NSString * _Nonnull title) {
- if ([title isEqualToString:kLocalizedString(@"去充值")]) {
- [[ApplicationDelegate getCurrentController].navigationController pushViewController:XYRechargeItemsViewController.new animated:YES];
- }
- }];
- [al show];
- }
- [self dismiss];
- } failureHandler:^(ZYLNetworkError *error) {
- [SVProgressHUD showInfoWithStatus:error.domain];
- }];
- }
- #pragma mark - Animation 提示框弹出时的动画效果
- - (void)showAnimation {
-
- CAKeyframeAnimation *alertViewshowAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
- alertViewshowAnimation .duration = 0.4;
- alertViewshowAnimation .values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)],
- [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1f, 1.1f, 1.0f)],
- [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9f, 0.9f, 1.0f)],
- [NSValue valueWithCATransform3D:CATransform3DIdentity]];
- alertViewshowAnimation.keyTimes = @[@0.2f, @0.5f, @0.75f, @1.0f];
- alertViewshowAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
- [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
- [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
- [self.contentView.layer addAnimation:alertViewshowAnimation forKey:nil];
- }
- - (void)showBackground
- {
- self.bgView.alpha = 0;
- [UIView beginAnimations:@"fadeIn" context:nil];
- [UIView setAnimationDuration:0.35];
- self.bgView.alpha = 0.35;
- [UIView commitAnimations];
- }
- - (void)hiddenAnimation {
-
- [UIView animateWithDuration:.25 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
- self.contentView.transform = CGAffineTransformMakeScale(0.00, 0.00);
- self.bgView.alpha = 0;
- } completion:^(BOOL finished) {
- // 移除控制器
- [self willMoveToParentViewController:nil];
- [self.view removeFromSuperview];
- [self removeFromParentViewController];
- }];
- }
- #pragma mark - UICollectionViewDelegateFlowLayout, UICollectionViewDataSource
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return self.dataSource.count;
- }
- // The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- XYGiveRewardAlertGiftItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:XYGiveRewardAlertGiftItemCollectionViewCell.className forIndexPath:indexPath];
- [cell configureModel:self.dataSource[indexPath.row]];
- return cell;
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
-
- // 修改源数据状态
- for (NSInteger i = 0; i < self.dataSource.count; i++) {
- XYGiveRewardGiftModel *model = self.dataSource[i];
- if (indexPath.row == i) {
- model.status = YES;
- }else {
- model.status = NO;
- }
- }
- }
- #pragma mark - 初始化
- - (void)_setup{
-
- self.view.backgroundColor = Color_Clear;
- }
- #pragma mark - 设置导航栏
- - (void)_setupNavigationItem{
-
- }
- #pragma mark - 设置子控件
- - (void)_setupSubViews{
- [self.view addSubview:self.bgView];
- [self.view addSubview:self.contentView];
-
- [self.contentView addSubview:self.titleLabel];
- [self.contentView addSubview:self.messageLabel];
- [self.contentView addSubview:self.collectionView];
- [self.contentView addSubview:self.cancelButton];
- [self.contentView addSubview:self.sureButton];
-
- [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.contentView).offset(24.0f);
- make.left.equalTo(self.contentView).offset(24.0f);
- make.right.equalTo(self.contentView).offset(-24.0f);
- make.height.equalTo(@22.0f);
- }];
- [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.titleLabel.mas_bottom).offset(4.0f);
- make.left.equalTo(self.contentView).offset(24.0f);
- make.right.equalTo(self.contentView).offset(-24.0f);
- make.height.equalTo(@17.0f);
- }];
- [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.contentView).offset(24.0f);
- make.right.equalTo(self.contentView).offset(-24.0f);
- make.top.equalTo(self.messageLabel.mas_bottom).offset(24.0f);
- make.height.equalTo(@96.0f);
- }];
- [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.contentView).offset(24.0f);
- make.top.equalTo(self.collectionView.mas_bottom).offset(34.0f);
- make.height.equalTo(@36.0f);
- }];
- [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.cancelButton.mas_right).offset(16.0f);
- make.top.equalTo(self.collectionView.mas_bottom).offset(34.0f);
- make.height.equalTo(@36.0f);
- make.right.equalTo(self.contentView).offset(-24.0f);
- make.width.equalTo(self.cancelButton);
- }];
-
- // 居中
- self.contentView.center = self.view.center;
-
- [self.contentView addViewBorder:Color_Clear redian:8];
- [self.cancelButton addViewBorder:Color_Clear redian:18];
- [self.sureButton addViewBorder:Color_Clear redian:18];
- }
- #pragma mark - 布局子控件
- - (void)_makeSubViewsConstraints{
-
- }
- #pragma mark - Setter & Getter
- - (UIView *)bgView {
- if (!_bgView) {
- _bgView = [[UIView alloc] initWithFrame:SCREEN_BOUNDS];
- _bgView.backgroundColor = ColorFromHexStringWithAlpha(@"#1D1C1F", 0.35f);
- UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelButtonAction:)];
- [_bgView addGestureRecognizer:tap];
- }
- return _bgView;
- }
- - (UIView *)contentView {
- if (!_contentView) {
- _contentView = [[UIView alloc] initWithFrame:CGRectMake(Left_Spacing, (SCREEN_HEIGHT-280.0f)/2, ContentView_Width, 280.0f)];
- _contentView.backgroundColor = Color_White;
- }
- return _contentView;;
- }
- - (UILabel *)titleLabel {
- if (!_titleLabel) {
- _titleLabel = [UILabel createLabelTextColor:Color_TextFont fount:Font_B(16)];
- _titleLabel.textAlignment = NSTextAlignmentCenter;
- _titleLabel.text = @"温馨提示";
- }
- return _titleLabel;
- }
- - (UILabel *)messageLabel {
- if (!_messageLabel) {
- _messageLabel = [UILabel createLabelTextColor:ColorFromHexString(@"#FD8C3B") fount:Font(12)];
- _messageLabel.textAlignment = NSTextAlignmentCenter;
- _messageLabel.text = @"打赏一下有助于提高好感度哦";
- }
- return _messageLabel;
- }
- - (UICollectionView *)collectionView {
- if (_collectionView == nil) {
- UICollectionViewFlowLayout *flowLayout = UICollectionViewFlowLayout.new;
- flowLayout.itemSize = CGSizeMake(72.0f, 96.0f);
- flowLayout.minimumLineSpacing = 8.0f;
- flowLayout.minimumInteritemSpacing = 8.0f;
- flowLayout.sectionInset = UIEdgeInsetsMake(0, 0.0f, 0, 0.0f);
- flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
- _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
- _collectionView.delegate = self;
- _collectionView.dataSource = self;
- _collectionView.backgroundColor = Color_Clear;
- [_collectionView registerCell:XYGiveRewardAlertGiftItemCollectionViewCell.className];
- }
- return _collectionView;
- }
- - (UIButton *)cancelButton {
- if (!_cancelButton) {
- _cancelButton = [UIButton createButtonTextColor:Color_TextGray textFont:Font(14)];
- [_cancelButton setBackgroundColor:Color_Background];
- [_cancelButton addTarget:self action:@selector(cancelButtonAction:) forControlEvents:UIControlEventTouchUpInside];
- [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
- }
- return _cancelButton;
- }
- - (UIButton *)sureButton {
- if (!_sureButton) {
- _sureButton = [UIButton createButtonTextColor:Color_White textFont:Font(16)];
- [_sureButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[ColorFromHexString(@"#5D26FF"), ColorFromHexString(@"#8359FF")] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake((ContentView_Width - 16.0f - 24.0f * 2)/2, 36.0f)] forState:UIControlStateNormal];
- [_sureButton addTarget:self action:@selector(sureButtonAction:) forControlEvents:UIControlEventTouchUpInside];
- [_sureButton setTitle:@"赠送" forState:UIControlStateNormal];
- }
- return _sureButton;
- }
- - (NSMutableArray *)dataSource {
- if (_dataSource == nil) {
- _dataSource = [NSMutableArray array];
- }
- return _dataSource;
- }
- @end
|