YBGiftPage.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. //
  2. // YBGiftPage.m
  3. // YBVideo
  4. //
  5. // Created by YB007 on 2020/10/10.
  6. // Copyright © 2020 cat. All rights reserved.
  7. //
  8. #import "YBGiftPage.h"
  9. #import "TYTabPagerBar.h"
  10. #import "TYPagerView.h"
  11. @interface YBGiftPage()<TYPagerViewDataSource,TYPagerViewDelegate,TYTabPagerBarDataSource,TYTabPagerBarDelegate,UIGestureRecognizerDelegate>
  12. @property(nonatomic,strong)TYTabPagerBar *tabBar;
  13. @property(nonatomic,strong)TYPagerView *pageView;
  14. @property(nonatomic,strong)NSArray *pageData;
  15. @property(nonatomic,strong)YBGiftView *normalGiftView;
  16. @property(nonatomic,strong)YBGiftView *bagGiftView;
  17. @end
  18. @implementation YBGiftPage
  19. +(instancetype)showGiftViewComplete:(RewardGiftBlock)complete {
  20. YBGiftPage *gV = [[YBGiftPage alloc]init];
  21. gV.giftEvent = complete;
  22. [gV setUpView];
  23. return gV;
  24. }
  25. -(void)requestGiftData{
  26. [_normalGiftView requestGiftData:@"Live.getGiftList"];
  27. [_bagGiftView requestGiftData:@"Backpack.getBackpack"];
  28. }
  29. -(void)setUpView{
  30. self.frame = [UIScreen mainScreen].bounds;
  31. UITapGestureRecognizer *tagGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
  32. tagGes.delegate = self;
  33. [self addGestureRecognizer:tagGes];
  34. //注意这里的高度和YBGiftView中bgView高度[_bgViewHeight]相同
  35. CGFloat subViewHeight = (_window_width*322/375+ShowDiff);
  36. if (IS_IPHONE_5) {
  37. subViewHeight = (_window_width*322/375+ShowDiff)+15;
  38. }
  39. _pageView = [[TYPagerView alloc]init];
  40. _pageView.frame = CGRectMake(0, _window_height - subViewHeight, _window_width, subViewHeight);
  41. _pageView.layout.autoMemoryCache = NO;
  42. _pageView.dataSource = self;
  43. _pageView.delegate = self;
  44. [_pageView.layout registerClass:[UIView class] forItemWithReuseIdentifier:@"cellId"];
  45. _pageView.scrollView.bounces = NO;
  46. _pageView.backgroundColor = UIColor.whiteColor;
  47. [self addSubview:_pageView];
  48. _tabBar = [[TYTabPagerBar alloc]init];
  49. _tabBar.frame = CGRectMake(10, _window_height - subViewHeight, _window_width/2, 44);
  50. _tabBar.layout.barStyle = TYPagerBarStyleProgressElasticView;
  51. _tabBar.dataSource = self;
  52. _tabBar.delegate = self;
  53. _tabBar.layout.cellWidth = 0;
  54. _tabBar.layout.selectedTextColor = RGB_COLOR(@"#000000", 1);
  55. _tabBar.layout.normalTextColor = RGB_COLOR(@"#969696", 1);
  56. _tabBar.layout.progressColor = Pink_Cor;
  57. _tabBar.layout.progressHeight = 3;
  58. _tabBar.layout.progressRadius = 1.5;
  59. _tabBar.layout.progressVerEdging = 5;
  60. _tabBar.backgroundColor = UIColor.whiteColor;
  61. [_tabBar registerClass:[TYTabPagerBarCell class] forCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier]];
  62. [self addSubview:_tabBar];
  63. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_pageView.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerTopLeft cornerRadii:CGSizeMake(10, 10)];
  64. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  65. maskLayer.frame = _pageView.bounds;
  66. maskLayer.path = maskPath.CGPath;
  67. _pageView.layer.mask = maskLayer;
  68. _pageData = @[YZMsg(@"打赏礼物"),YZMsg(@"我的背包")];
  69. YBWeakSelf;
  70. _normalGiftView = [YBGiftView showTypeOfGiftViewComplete:^(int eventCode,NSDictionary *giftDic) {
  71. if (eventCode == -1) {
  72. [weakSelf dismiss];
  73. }
  74. if (weakSelf.giftEvent) {
  75. weakSelf.giftEvent(eventCode, giftDic);
  76. }
  77. }];
  78. _normalGiftView.giftViewSwitchEvent = ^(id currentView) {
  79. [weakSelf cancelSelFlag:currentView];
  80. };
  81. _bagGiftView = [YBGiftView showTypeOfGiftViewComplete:^(int eventCode,NSDictionary *giftDic) {
  82. if (eventCode == -1) {
  83. [weakSelf dismiss];
  84. }
  85. if (weakSelf.giftEvent) {
  86. weakSelf.giftEvent(eventCode, giftDic);
  87. }
  88. }];
  89. _bagGiftView.giftViewSwitchEvent = ^(id currentView) {
  90. [weakSelf cancelSelFlag:currentView];
  91. };
  92. [_tabBar reloadData];
  93. [_pageView updateData];
  94. //添加手绘区域
  95. [self addSubview:self.paintedRegion];
  96. [_paintedRegion mas_makeConstraints:^(MASConstraintMaker *make) {
  97. make.centerX.equalTo(self);
  98. make.width.mas_equalTo(_window_width);
  99. make.top.equalTo(self.mas_top).offset(statusbarHeight);
  100. make.bottom.equalTo(self.pageView.mas_top).offset(45);
  101. }];
  102. _normalGiftView.paintedRegion = _paintedRegion;
  103. _bagGiftView.paintedRegion = _paintedRegion;
  104. [self showGiftPage];
  105. }
  106. #pragma mark - 必要参数
  107. - (void)setReceiveID:(NSString *)receiveID {
  108. _receiveID = receiveID;
  109. _normalGiftView.receiveID = _receiveID;
  110. _bagGiftView.receiveID = _receiveID;
  111. }
  112. - (void)setReceiveStream:(NSString *)receiveStream {
  113. _receiveStream = receiveStream;
  114. _normalGiftView.receiveStream = _receiveStream ? _receiveStream:@"";
  115. _bagGiftView.receiveStream = _receiveStream ? _receiveStream:@"";
  116. }
  117. - (void)setSendType:(SendGiftType)sendType {
  118. _sendType = sendType;
  119. _normalGiftView.sendType = _sendType;
  120. _bagGiftView.sendType = _sendType;
  121. }
  122. -(void)cancelSelFlag:(id)currentView {
  123. if (currentView == _normalGiftView) {
  124. [_bagGiftView cancelGiftSel];
  125. }else {
  126. [_normalGiftView cancelGiftSel];
  127. }
  128. }
  129. -(void)showGiftPage {
  130. [[UIApplication sharedApplication].delegate.window addSubview:self];
  131. [UIView animateWithDuration:0.3 animations:^{
  132. } completion:^(BOOL finished) {
  133. self.backgroundColor = RGB_COLOR(@"#000000", 0.4);
  134. }];
  135. }
  136. -(void)dismiss {
  137. if (self.giftEvent) {
  138. self.giftEvent(-1, @{});
  139. }
  140. [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
  141. [self removeFromSuperview];
  142. }
  143. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch; {
  144. if (_paintedRegion.showPainted) {
  145. return NO;
  146. }
  147. if ([touch.view isDescendantOfView:self.pageView] || [touch.view isDescendantOfView:self.tabBar]) {
  148. return NO;
  149. }
  150. return YES;
  151. }
  152. #pragma mark - TYTabPagerBarDataSource
  153. - (NSInteger)numberOfItemsInPagerTabBar {
  154. return _pageData.count;
  155. }
  156. - (UICollectionViewCell<TYTabPagerBarCellProtocol> *)pagerTabBar:(TYTabPagerBar *)pagerTabBar cellForItemAtIndex:(NSInteger)index {
  157. UICollectionViewCell<TYTabPagerBarCellProtocol> *cell = [pagerTabBar dequeueReusableCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier] forIndex:index];
  158. cell.titleLabel.text = _pageData[index];
  159. return cell;
  160. }
  161. #pragma mark - TYTabPagerBarDelegate
  162. - (CGFloat)pagerTabBar:(TYTabPagerBar *)pagerTabBar widthForItemAtIndex:(NSInteger)index {
  163. NSString *title = _pageData[index];
  164. return [pagerTabBar cellWidthForTitle:title];
  165. }
  166. - (void)pagerTabBar:(TYTabPagerBar *)pagerTabBar didSelectItemAtIndex:(NSInteger)index {
  167. [_pageView scrollToViewAtIndex:index animate:YES];
  168. }
  169. #pragma mark - TYPagerViewDataSource
  170. - (NSInteger)numberOfViewsInPagerView {
  171. return _pageData.count;
  172. }
  173. - (UIView *)pagerView:(TYPagerView *)pagerView viewForIndex:(NSInteger)index prefetching:(BOOL)prefetching {
  174. if (index == 0) {
  175. return _normalGiftView;
  176. } else{
  177. return _bagGiftView;
  178. }
  179. }
  180. #pragma mark - TYPagerViewDelegate
  181. - (void)pagerView:(TYPagerView *)pagerView willAppearView:(UIView *)view forIndex:(NSInteger)index {
  182. _paintedRegion.showPainted = NO;
  183. if (view == _normalGiftView) {
  184. [_normalGiftView judesSelIsPaintedGift];
  185. }
  186. if (view == _bagGiftView) {
  187. [_bagGiftView judesSelIsPaintedGift];
  188. }
  189. }
  190. - (void)pagerView:(TYPagerView *)pagerView willDisappearView:(UIView *)view forIndex:(NSInteger)index {
  191. //NSLog(@"---------willDisappearView:%ld",index);
  192. }
  193. - (void)pagerView:(TYPagerView *)pagerView transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex animated:(BOOL)animated {
  194. //NSLog(@"fromIndex:%ld, toIndex:%ld",fromIndex,toIndex);
  195. [_tabBar scrollToItemFromIndex:fromIndex toIndex:toIndex animate:animated];
  196. }
  197. - (void)pagerView:(TYPagerView *)pagerView transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex progress:(CGFloat)progress {
  198. //NSLog(@"fromIndex:%ld, toIndex:%ld progress%.3f",fromIndex,toIndex,progress);
  199. [_tabBar scrollToItemFromIndex:fromIndex toIndex:toIndex progress:progress];
  200. }
  201. #pragma mark - 手绘礼物
  202. - (RKPaintedGiftView *)paintedRegion {
  203. if (!_paintedRegion) {
  204. _paintedRegion = [[RKPaintedGiftView alloc]init];
  205. YBWeakSelf;
  206. _paintedRegion.paintedEvent = ^{
  207. [weakSelf dismiss];
  208. };
  209. }
  210. return _paintedRegion;
  211. }
  212. @end