// // YBGiftView.m // YBVideo // // Created by YB007 on 2019/8/19. // Copyright © 2019 cat. All rights reserved. // #import "YBGiftView.h" #import "YBGiftCell.h" #import "YBGiftModel.h" #import "YBRechargeVC.h" @interface CollectionCellWhite : UICollectionViewCell @end @implementation CollectionCellWhite - (instancetype)initWithFrame:(CGRect)frame andPlayDic:(NSDictionary *)zhuboDic{ self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor clearColor]; } return self; } @end @interface YBGiftView() { NSArray *_dataArray; //礼物+金币 NSArray *_giftList; //礼物 NSString *_coinNum; //金币数量 NSInteger _pageCount; NSMutableArray *_selArray; BOOL isRight; NSTimer *_continueTimer; int _intervalTime; CGFloat _collectionHeght; int _perRowNums; int _rowNums; NSString *_methodUrl; } @property(nonatomic,strong)UILabel *coinL; @property(nonatomic,strong)YBGiftModel *selModel; @property(nonatomic,strong)NSArray *giftModels; @property(nonatomic,strong)CABasicAnimation *animation; /** * 新加礼物组功能: 父视图为 _bottomMixBg */ @property(nonatomic,strong)UIImageView *multipleIV; //礼物组 @property(nonatomic,strong)NSArray *mutipleGiftA; @property(nonatomic,strong)UIView *multipleGiftCountBg; //礼物组数量选择背景 @property(nonatomic,strong)UIButton *multipleCountBtn; //礼物组数量选择按钮 @property(nonatomic,strong)UIButton *multipleSendBtn; //礼物组赠送按钮 @property(nonatomic,assign)ShowTypeOfGift typeOfGift; @end @implementation YBGiftView - (void)awakeFromNib { [super awakeFromNib]; //添加背包礼物,_giftTitleL被YBGiftPage中的tabbar代替 _giftTitleL.text = YZMsg(@"打赏礼物"); _giftTitleL.hidden = YES; _sendBtn.hidden = NO; [_sendBtn setTitle:YZMsg(@"赠送") forState:0]; _continueSendBtn.hidden = YES; _continueSendBtn.titleLabel.numberOfLines = 0; [_continueSendBtn setBackgroundImage:[UIImage imageNamed:@"gift_continus"] forState:0]; [_sendBtn setBackgroundImage:[PublicObj getImgWithColor:RGB_COLOR(@"#dcdcdc", 1)] forState:0]; [_sendBtn setBackgroundImage:[PublicObj getImgWithColor:Pink_Cor] forState:UIControlStateSelected]; _perRowNums = 5; _rowNums = 2; _pageCount = 0; _intervalTime = 5; _selArray = [NSMutableArray array]; //礼物组 [_bottomMixBg addSubview:self.multipleIV]; [_bottomMixBg bringSubviewToFront:self.continueSendBtn]; [_multipleIV mas_makeConstraints:^(MASConstraintMaker *make) { make.centerY.equalTo(_bottomMixBg); make.right.equalTo(_bottomMixBg).offset(-15); make.width.mas_equalTo(94); make.height.mas_equalTo(26); }]; _mutipleGiftA = @[@"1314",@"520",@"100",@"88",@"66",@"10",@"1"]; } - (NSArray *)giftModels { // if (!_giftModels) { NSMutableArray *m_array = [NSMutableArray array]; for (NSDictionary*dic in _giftList) { YBGiftModel *model = [YBGiftModel modelWithDic:dic]; [m_array addObject:model]; // } _giftModels = [m_array copy]; } return _giftModels; } +(instancetype)showTypeOfGiftViewComplete:(RewardGiftBlock)complete { YBGiftView *gV = [[[NSBundle mainBundle]loadNibNamed:@"YBGiftView" owner:nil options:nil]objectAtIndex:0]; if (complete) { gV.giftEvent = ^(int eventCode,NSDictionary *giftDic) { complete(eventCode,giftDic); }; } [gV setUpView]; return gV; } -(void)setUpView{ CGFloat subViewHeight = (_window_width*322/375+ShowDiff); if (IS_IPHONE_5) { subViewHeight = (_window_width*322/375+ShowDiff)+15; } self.frame = CGRectMake(0, _window_height-subViewHeight, _window_width, subViewHeight); NSLog(@"=:%@",self.bgView); _bgViewHeight.constant = subViewHeight; _bgView.backgroundColor = UIColor.clearColor; _collectionHeght = _window_width*220/375; [_bgView layoutIfNeeded]; // _GiftLayout.itemCountPerRow = _perRowNums; _GiftLayout.rowCount = _rowNums; _GiftLayout.minimumLineSpacing = 0; _GiftLayout.minimumInteritemSpacing = 0; _GiftLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; _giftCollectionView.delegate = self; _giftCollectionView.dataSource = self; [_giftCollectionView registerNib:[UINib nibWithNibName:@"YBGiftCell" bundle:nil] forCellWithReuseIdentifier:@"YBGiftCell"]; [_giftCollectionView registerClass:[CollectionCellWhite class] forCellWithReuseIdentifier:@"CellWhite"]; } -(void)requestGiftData:(NSString *)giftUrl { _methodUrl = giftUrl; _typeOfGift = ShowTypeOfGift_Normal; if ([giftUrl containsString:@"Backpack.getBackpack"]) { _typeOfGift = ShowTypeOfGift_Bag; } [MBProgressHUD showMessage:@""]; [self pullData]; } -(void)pullData { [YBNetworking postWithUrl:_methodUrl Dic:nil Suc:^(int code, id info, NSString *msg) { [MBProgressHUD hideHUD]; if (code == 0) { _dataArray = [NSArray arrayWithArray:info]; NSDictionary *dataDic = @{}; if ([[_dataArray firstObject] isKindOfClass:[NSDictionary class]]) { dataDic = [_dataArray firstObject]; } if (!_selArray) { _selArray = [NSMutableArray array]; } if ([[dataDic valueForKey:@"giftlist"]isKindOfClass:[NSArray class]]) { _giftList = [NSArray arrayWithArray:[dataDic valueForKey:@"giftlist"]]; } _coinNum = minstr([dataDic valueForKey:@"coin"]); _coinL.text = minstr(_coinNum); [Config saveUserCoin:_coinNum]; _pageCount = _giftList.count; while (_pageCount % 8 !=0) { ++_pageCount; } for (int i = 0 ; i<_giftList.count; i++) { [_selArray addObject:@"0"]; } if (_giftList.count > 0) { _giftPage.hidden = NO; if ((int)_giftList.count % (_perRowNums*_rowNums) == 0) { _giftPage.numberOfPages = _giftList.count/(_perRowNums*_rowNums); } else{ _giftPage.numberOfPages = _giftList.count/(_perRowNums*_rowNums) + 1; } }else { _giftPage.hidden = YES; } [_giftCollectionView reloadData]; if (_giftList.count <= 0) { [PublicView showTextNoData:_giftCollectionView text1:@"" text2:YZMsg(@"暂无数据") centerY:0.6]; }else { [PublicView hiddenTextNoData:_giftCollectionView]; } if (_typeOfGift == ShowTypeOfGift_Normal) { //金币列表 [self setCoinListUI]; } }else { [MBProgressHUD showPop:msg]; } } Fail:^(id fail) { [MBProgressHUD hideHUD]; }]; } -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ return _pageCount; } -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ return 1; } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.item >= self.giftModels.count) { UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CellWhite" forIndexPath:indexPath]; return cell; } else { YBGiftCell *cell = (YBGiftCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"YBGiftCell" forIndexPath:indexPath]; YBGiftModel *model = self.giftModels[indexPath.item]; [collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone]; cell.typeOfGift = _typeOfGift; cell.model = model; NSString *selFlag = [NSString stringWithFormat:@"%@",_selArray[indexPath.item]]; if ([selFlag isEqual:@"1"]) { cell.flagIV.hidden = NO; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [cell.iconIV.layer addAnimation:self.animation forKey:nil]; }); } else{ cell.flagIV.hidden = YES; [cell.iconIV.layer removeAllAnimations]; } return cell; } } - (void)addAnimation:(NSIndexPath *)indexpath{ for (YBGiftCell *cell in _giftCollectionView.visibleCells) { if ([[_giftCollectionView indexPathForCell:cell] isEqual:indexpath]) { [cell.iconIV.layer addAnimation:self.animation forKey:nil]; }else{ [cell.iconIV.layer removeAllAnimations]; } } } - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;{ [_giftCollectionView reloadData]; } -(CABasicAnimation *)animation{ if (!_animation) { _animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; _animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; _animation.duration = 0.88; _animation.repeatCount = 9999999; _animation.autoreverses = YES; _animation.fromValue = [NSNumber numberWithFloat:1]; _animation.toValue = [NSNumber numberWithFloat:0.8]; } return _animation; } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.item>=self.giftModels.count) { return; } if (self.giftViewSwitchEvent) { self.giftViewSwitchEvent(self); } _sendBtn.selected = YES; [_selArray removeAllObjects]; for (int i=0; i0){ NSLog(@"右滑"); isRight = YES; }else{ NSLog(@"左滑"); isRight = NO; } NSLog(@"scrollViewWillEndDragging"); if (isRight) { _giftPage.currentPage+=1; } else{ _giftPage.currentPage-=1; } } -(void)cancelGiftSel { if (_selArray.count <= 0 || !_selModel) { return; } [_selArray removeAllObjects]; for (int i=0; i