XYRotaryTableViewController.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //
  2. // XYRotaryTableViewController.m
  3. // Timi
  4. //
  5. // Created by 翟玉磊 on 2021/4/15.
  6. //
  7. #import "XYRotaryTableViewController.h"
  8. #import "XYRotaryTableView.h"
  9. #import "XYChatRoomRotaryTableItemModel.h"
  10. #import "XYRotarytableWinningView.h"
  11. @interface XYRotaryTableViewController ()<XYRotaryTableViewDelegate>
  12. @property (nonatomic, strong) UIView *bgView;
  13. @property (nonatomic, strong) UIButton *closeButton;
  14. @property (nonatomic, strong) XYRotaryTableView *rotaryTableView;
  15. @property (nonatomic, strong) XYRotarytableWinningView *winningView;
  16. // 奖项item
  17. @property (nonatomic, strong) NSMutableArray *lotteryItems;
  18. // 中奖索引
  19. @property (nonatomic, assign) NSInteger lotteryResult;
  20. // 中奖描述
  21. @property (nonatomic, copy) NSString *lotteryReward;
  22. @property (nonatomic, strong) NSTimer *timer;
  23. @property (nonatomic, strong) XYLiveRoomMessageInfo *msgInfo;
  24. // 当前小时的秒数 默认5s
  25. @property (nonatomic, assign) NSInteger currentTime;
  26. @end
  27. @implementation XYRotaryTableViewController
  28. - (void)dealloc
  29. {
  30. [self clear];
  31. }
  32. #pragma mark — Public
  33. - (void)clear {
  34. if (_timer) {
  35. [_timer invalidate];
  36. _timer = nil;
  37. }
  38. if (_rotaryTableView) {
  39. [_rotaryTableView clear];
  40. [_rotaryTableView removeFromSuperview];
  41. _rotaryTableView = nil;
  42. }
  43. if (_winningView) {
  44. [_winningView clear];
  45. [_winningView removeFromSuperview];
  46. _winningView = nil;
  47. }
  48. for (UIView *view in self.view.subviews) {
  49. [view removeFromSuperview];
  50. }
  51. [self.view removeFromSuperview];
  52. [self removeFromParentViewController];
  53. }
  54. - (void)showWithController:(UIViewController *)controler view:(nonnull UIView *)view {
  55. [controler addChildViewController:self];
  56. [view addSubview:self.view];
  57. [UIView animateWithDuration:.3 animations:^{
  58. self.bgView.alpha = 1;
  59. } completion:^(BOOL finished) {
  60. }];
  61. }
  62. - (void)dismiss {
  63. [UIView animateWithDuration:.3 animations:^{
  64. self.bgView.alpha = 0;
  65. } completion:^(BOOL finished) {
  66. [self clear];
  67. if (self.delegate && [self.delegate respondsToSelector:@selector(closeRotaryTable)]) {
  68. [self.delegate closeRotaryTable];
  69. }
  70. }];
  71. }
  72. - (void)reset {
  73. if (_timer) {
  74. [_timer invalidate];
  75. _timer = nil;
  76. }
  77. if (_winningView) {
  78. [_winningView clear];
  79. [_winningView removeFromSuperview];
  80. _winningView = nil;
  81. }
  82. self.rotaryTableView.state = XYRotaryTableStateNormal;
  83. }
  84. /// 中奖结果
  85. - (void)lotteryResultMessageInfo:(XYLiveRoomMessageInfo *)info {
  86. self.msgInfo = info;
  87. // 中奖结果
  88. NSInteger lotteryResult = [info.msgDict[@"lotteryResult"] integerValue];
  89. // 中奖描述
  90. NSString *lotteryReward = [BaseMethod toString:info.msgDict[@"lotteryReward"]];
  91. // 动画时间
  92. NSInteger animationDuration = [info.msgDict[@"animationDuration"] integerValue]/1000;
  93. self.lotteryResult = lotteryResult;
  94. self.lotteryReward = lotteryReward;
  95. self.rotaryTableView.animationDuration = animationDuration;
  96. [self.rotaryTableView setupRotaryTableResult:lotteryResult];
  97. }
  98. - (void)viewDidLoad {
  99. [super viewDidLoad];
  100. // Do any additional setup after loading the view.
  101. [self setupUI];
  102. }
  103. - (void)requestRemoteData {
  104. [[XYVoiceRoomAPIManager new] getTurntableConfigWithRoomId:self.roomId successHandler:^(ZYLResponseModel *responseModel) {
  105. [self.lotteryItems removeAllObjects];
  106. NSMutableArray *keysArray = [NSMutableArray array];
  107. for (NSDictionary *dict in responseModel.data[@"list"]) {
  108. XYChatRoomRotaryTableItemModel *model = XYChatRoomRotaryTableItemModel.new;
  109. [model yy_modelSetWithDictionary:dict];
  110. [self.lotteryItems addObject:model];
  111. [keysArray addObject:[BaseMethod toString:dict[@"itemKey"]]];
  112. }
  113. [self.rotaryTableView setupRotaryTableItems:keysArray];
  114. } failureHandler:^(ZYLNetworkError *error) {
  115. [SVProgressHUD showInfoWithStatus:error.domain];
  116. }];
  117. }
  118. - (void)setupUI {
  119. self.view.backgroundColor = Color_Clear;
  120. [self.view addSubview:self.bgView];
  121. [self.view addSubview:self.rotaryTableView];
  122. [self.view addSubview:self.closeButton];
  123. }
  124. #pragma mark — Action
  125. - (void)closeRotaryTable {
  126. if ([XYChatRoomManager sharedInstance].chatRoomController.infoManager.isCurrentHost) {
  127. // 是主持则请求关闭转盘接口
  128. [[XYVoiceRoomAPIManager new] setBigAdventureEnabledWithRoomId:[XYChatRoomManager sharedInstance].roomId bigAdventureEnabled:NO successHandler:^(ZYLResponseModel *responseModel) {
  129. } failureHandler:^(ZYLNetworkError *error) {
  130. [SVProgressHUD showInfoWithStatus:error.domain];
  131. }];
  132. }else {
  133. // 非主持直接关闭就可以
  134. [self dismiss];
  135. }
  136. }
  137. - (void)closeButtonAction:(id)sender {
  138. [self closeRotaryTable];
  139. }
  140. #pragma mark — XYRotaryTableViewDelegate
  141. /// 点击抽奖
  142. - (void)turntableLotteryWithRotaryTable:(XYRotaryTableView *)rotaryTable {
  143. if (_timer) {
  144. [_timer invalidate];
  145. _timer = nil;
  146. }
  147. if (_winningView) {
  148. [_winningView endAnimation];
  149. }
  150. if (rotaryTable.state == XYRotaryTableStateFinish) {
  151. // 重新开一局
  152. [[XYVoiceRoomAPIManager new] setBigAdventureEnabledWithRoomId:self.roomId bigAdventureEnabled:YES successHandler:^(ZYLResponseModel *responseModel) {
  153. // todo
  154. // 利用消息显示转盘
  155. } failureHandler:^(ZYLNetworkError *error) {
  156. [SVProgressHUD showInfoWithStatus:error.domain];
  157. }];
  158. }else {
  159. [[XYVoiceRoomAPIManager new] turntableLotteryWithRoomId:self.roomId successHandler:^(ZYLResponseModel *responseModel) {
  160. self.rotaryTableView.state = XYRotaryTableStateReady;
  161. } failureHandler:^(ZYLNetworkError *error) {
  162. [SVProgressHUD showInfoWithStatus:error.domain];
  163. }];
  164. }
  165. }
  166. /// 转盘开始旋转回调
  167. - (void)didBeginRotatingWithRotaryTable:(XYRotaryTableView *)rotaryTable {
  168. NSLog(@"转盘开始旋转");
  169. }
  170. /// 转盘结束旋转回调
  171. - (void)didEndRotatingWithRotaryTable:(XYRotaryTableView *)rotaryTable {
  172. NSLog(@"转盘结束旋转");
  173. // 1.显示奖励弹窗
  174. if (_winningView) {
  175. [_winningView clear];
  176. _winningView = nil;
  177. }
  178. _winningView = [[XYRotarytableWinningView alloc] initWithFrame:CGRectMake(0, self.rotaryTableView.f_top + 40.0f, self.view.f_width, 44.0f)];
  179. [self.view addSubview:_winningView];
  180. [_winningView startAnimationWithText:StringIsEmpty(self.lotteryReward)?@"谢谢参与":self.lotteryReward];
  181. // 2.回调显示弹幕
  182. if (self.delegate && [self.delegate respondsToSelector:@selector(rotaryTableFinishWithLotteryResultMsg:)]) {
  183. [self.delegate rotaryTableFinishWithLotteryResultMsg:self.msgInfo];
  184. }
  185. if (![XYChatRoomManager sharedInstance].chatRoomController.infoManager.isCurrentHost) {
  186. // 3.关闭转盘
  187. if (_timer) {
  188. [_timer invalidate];
  189. }
  190. self.currentTime = 5;
  191. [self.rotaryTableView setupHideTime:self.currentTime];
  192. WeakSelf
  193. _timer = [NSTimer scheduledTimerWithTimeInterval:1 block:^(NSTimer * _Nonnull timer) {
  194. [weakSelf timerAction];
  195. } repeats:YES];
  196. }
  197. }
  198. - (void)timerAction {
  199. self.currentTime--;
  200. if (self.currentTime <= 0) {
  201. [self dismiss];
  202. }
  203. [self.rotaryTableView setupHideTime:self.currentTime];
  204. }
  205. #pragma mark — Getter
  206. - (UIView *)bgView {
  207. if (!_bgView) {
  208. _bgView = [[UIView alloc] initWithFrame:self.view.bounds];
  209. _bgView.backgroundColor = ColorFromHexStringWithAlpha(@"#000000", 0.3f);
  210. _bgView.alpha = 0;
  211. }
  212. return _bgView;
  213. }
  214. - (UIButton *)closeButton {
  215. if (!_closeButton) {
  216. _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
  217. _closeButton.frame = CGRectMake(self.view.f_width - 20.0f - 40.0f, self.rotaryTableView.f_y, 40.0f, 40.0f);
  218. [_closeButton setImage:ImageNamed(@"xy-chatroom-rotarytable-close") forState:UIControlStateNormal];
  219. _closeButton.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
  220. _closeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
  221. [_closeButton addTarget:self action:@selector(closeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  222. }
  223. return _closeButton;
  224. }
  225. - (XYRotaryTableView *)rotaryTableView {
  226. if (!_rotaryTableView) {
  227. _rotaryTableView = [[XYRotaryTableView alloc] initWithFrame:CGRectMake(0, self.view.f_heigh - HOME_INDICATOR_HEIGHT - 20.0f - 44.0f - 40.0f - 300.0f, 300.0f, 300.0f)];
  228. _rotaryTableView.centerX = self.view.centerX;
  229. _rotaryTableView.delegate = self;
  230. if ([XYChatRoomManager sharedInstance].chatRoomController.infoManager.isCurrentHost) {
  231. // 是不是主持人 只有主持人有操作权限
  232. _rotaryTableView.enabled = YES;
  233. }else {
  234. _rotaryTableView.enabled = NO;
  235. }
  236. }
  237. return _rotaryTableView;
  238. }
  239. - (NSMutableArray *)lotteryItems {
  240. if (!_lotteryItems) {
  241. _lotteryItems = [NSMutableArray array];
  242. }
  243. return _lotteryItems;
  244. }
  245. @end