123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- //
- // XYRotaryTableViewController.m
- // Timi
- //
- // Created by 翟玉磊 on 2021/4/15.
- //
- #import "XYRotaryTableViewController.h"
- #import "XYRotaryTableView.h"
- #import "XYChatRoomRotaryTableItemModel.h"
- #import "XYRotarytableWinningView.h"
- @interface XYRotaryTableViewController ()<XYRotaryTableViewDelegate>
- @property (nonatomic, strong) UIView *bgView;
- @property (nonatomic, strong) UIButton *closeButton;
- @property (nonatomic, strong) XYRotaryTableView *rotaryTableView;
- @property (nonatomic, strong) XYRotarytableWinningView *winningView;
- // 奖项item
- @property (nonatomic, strong) NSMutableArray *lotteryItems;
- // 中奖索引
- @property (nonatomic, assign) NSInteger lotteryResult;
- // 中奖描述
- @property (nonatomic, copy) NSString *lotteryReward;
- @property (nonatomic, strong) NSTimer *timer;
- @property (nonatomic, strong) XYLiveRoomMessageInfo *msgInfo;
- // 当前小时的秒数 默认5s
- @property (nonatomic, assign) NSInteger currentTime;
- @end
- @implementation XYRotaryTableViewController
- - (void)dealloc
- {
- [self clear];
- }
- #pragma mark — Public
- - (void)clear {
- if (_timer) {
- [_timer invalidate];
- _timer = nil;
- }
- if (_rotaryTableView) {
- [_rotaryTableView clear];
- [_rotaryTableView removeFromSuperview];
- _rotaryTableView = nil;
- }
- if (_winningView) {
- [_winningView clear];
- [_winningView removeFromSuperview];
- _winningView = nil;
- }
- for (UIView *view in self.view.subviews) {
- [view removeFromSuperview];
- }
- [self.view removeFromSuperview];
- [self removeFromParentViewController];
- }
- - (void)showWithController:(UIViewController *)controler view:(nonnull UIView *)view {
-
- [controler addChildViewController:self];
- [view addSubview:self.view];
-
- [UIView animateWithDuration:.3 animations:^{
- self.bgView.alpha = 1;
- } completion:^(BOOL finished) {
-
- }];
- }
- - (void)dismiss {
- [UIView animateWithDuration:.3 animations:^{
- self.bgView.alpha = 0;
- } completion:^(BOOL finished) {
- [self clear];
- if (self.delegate && [self.delegate respondsToSelector:@selector(closeRotaryTable)]) {
- [self.delegate closeRotaryTable];
- }
- }];
- }
- - (void)reset {
- if (_timer) {
- [_timer invalidate];
- _timer = nil;
- }
- if (_winningView) {
- [_winningView clear];
- [_winningView removeFromSuperview];
- _winningView = nil;
- }
- self.rotaryTableView.state = XYRotaryTableStateNormal;
- }
- /// 中奖结果
- - (void)lotteryResultMessageInfo:(XYLiveRoomMessageInfo *)info {
-
- self.msgInfo = info;
-
- // 中奖结果
- NSInteger lotteryResult = [info.msgDict[@"lotteryResult"] integerValue];
- // 中奖描述
- NSString *lotteryReward = [BaseMethod toString:info.msgDict[@"lotteryReward"]];
- // 动画时间
- NSInteger animationDuration = [info.msgDict[@"animationDuration"] integerValue]/1000;
- self.lotteryResult = lotteryResult;
- self.lotteryReward = lotteryReward;
-
- self.rotaryTableView.animationDuration = animationDuration;
-
- [self.rotaryTableView setupRotaryTableResult:lotteryResult];
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
-
- [self setupUI];
- }
- - (void)requestRemoteData {
- [[XYVoiceRoomAPIManager new] getTurntableConfigWithRoomId:self.roomId successHandler:^(ZYLResponseModel *responseModel) {
-
- [self.lotteryItems removeAllObjects];
- NSMutableArray *keysArray = [NSMutableArray array];
- for (NSDictionary *dict in responseModel.data[@"list"]) {
- XYChatRoomRotaryTableItemModel *model = XYChatRoomRotaryTableItemModel.new;
- [model yy_modelSetWithDictionary:dict];
- [self.lotteryItems addObject:model];
-
- [keysArray addObject:[BaseMethod toString:dict[@"itemKey"]]];
- }
-
- [self.rotaryTableView setupRotaryTableItems:keysArray];
- } failureHandler:^(ZYLNetworkError *error) {
- [SVProgressHUD showInfoWithStatus:error.domain];
- }];
- }
- - (void)setupUI {
-
- self.view.backgroundColor = Color_Clear;
-
- [self.view addSubview:self.bgView];
- [self.view addSubview:self.rotaryTableView];
- [self.view addSubview:self.closeButton];
- }
- #pragma mark — Action
- - (void)closeRotaryTable {
- if ([XYChatRoomManager sharedInstance].chatRoomController.infoManager.isCurrentHost) {
- // 是主持则请求关闭转盘接口
- [[XYVoiceRoomAPIManager new] setBigAdventureEnabledWithRoomId:[XYChatRoomManager sharedInstance].roomId bigAdventureEnabled:NO successHandler:^(ZYLResponseModel *responseModel) {
- } failureHandler:^(ZYLNetworkError *error) {
- [SVProgressHUD showInfoWithStatus:error.domain];
- }];
- }else {
- // 非主持直接关闭就可以
- [self dismiss];
- }
- }
- - (void)closeButtonAction:(id)sender {
- [self closeRotaryTable];
- }
- #pragma mark — XYRotaryTableViewDelegate
- /// 点击抽奖
- - (void)turntableLotteryWithRotaryTable:(XYRotaryTableView *)rotaryTable {
- if (_timer) {
- [_timer invalidate];
- _timer = nil;
- }
- if (_winningView) {
- [_winningView endAnimation];
- }
-
- if (rotaryTable.state == XYRotaryTableStateFinish) {
- // 重新开一局
- [[XYVoiceRoomAPIManager new] setBigAdventureEnabledWithRoomId:self.roomId bigAdventureEnabled:YES successHandler:^(ZYLResponseModel *responseModel) {
- // todo
- // 利用消息显示转盘
-
- } failureHandler:^(ZYLNetworkError *error) {
- [SVProgressHUD showInfoWithStatus:error.domain];
- }];
- }else {
- [[XYVoiceRoomAPIManager new] turntableLotteryWithRoomId:self.roomId successHandler:^(ZYLResponseModel *responseModel) {
- self.rotaryTableView.state = XYRotaryTableStateReady;
- } failureHandler:^(ZYLNetworkError *error) {
- [SVProgressHUD showInfoWithStatus:error.domain];
- }];
- }
- }
- /// 转盘开始旋转回调
- - (void)didBeginRotatingWithRotaryTable:(XYRotaryTableView *)rotaryTable {
- NSLog(@"转盘开始旋转");
- }
- /// 转盘结束旋转回调
- - (void)didEndRotatingWithRotaryTable:(XYRotaryTableView *)rotaryTable {
- NSLog(@"转盘结束旋转");
-
- // 1.显示奖励弹窗
- if (_winningView) {
- [_winningView clear];
- _winningView = nil;
- }
- _winningView = [[XYRotarytableWinningView alloc] initWithFrame:CGRectMake(0, self.rotaryTableView.f_top + 40.0f, self.view.f_width, 44.0f)];
- [self.view addSubview:_winningView];
- [_winningView startAnimationWithText:StringIsEmpty(self.lotteryReward)?@"谢谢参与":self.lotteryReward];
-
- // 2.回调显示弹幕
- if (self.delegate && [self.delegate respondsToSelector:@selector(rotaryTableFinishWithLotteryResultMsg:)]) {
- [self.delegate rotaryTableFinishWithLotteryResultMsg:self.msgInfo];
- }
-
- if (![XYChatRoomManager sharedInstance].chatRoomController.infoManager.isCurrentHost) {
- // 3.关闭转盘
- if (_timer) {
- [_timer invalidate];
- }
- self.currentTime = 5;
- [self.rotaryTableView setupHideTime:self.currentTime];
- WeakSelf
- _timer = [NSTimer scheduledTimerWithTimeInterval:1 block:^(NSTimer * _Nonnull timer) {
- [weakSelf timerAction];
- } repeats:YES];
- }
- }
- - (void)timerAction {
-
- self.currentTime--;
- if (self.currentTime <= 0) {
- [self dismiss];
- }
- [self.rotaryTableView setupHideTime:self.currentTime];
- }
- #pragma mark — Getter
- - (UIView *)bgView {
- if (!_bgView) {
- _bgView = [[UIView alloc] initWithFrame:self.view.bounds];
- _bgView.backgroundColor = ColorFromHexStringWithAlpha(@"#000000", 0.3f);
- _bgView.alpha = 0;
- }
- return _bgView;
- }
- - (UIButton *)closeButton {
- if (!_closeButton) {
- _closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
- _closeButton.frame = CGRectMake(self.view.f_width - 20.0f - 40.0f, self.rotaryTableView.f_y, 40.0f, 40.0f);
- [_closeButton setImage:ImageNamed(@"xy-chatroom-rotarytable-close") forState:UIControlStateNormal];
- _closeButton.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
- _closeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
- [_closeButton addTarget:self action:@selector(closeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
- }
- return _closeButton;
- }
- - (XYRotaryTableView *)rotaryTableView {
- if (!_rotaryTableView) {
- _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)];
- _rotaryTableView.centerX = self.view.centerX;
- _rotaryTableView.delegate = self;
- if ([XYChatRoomManager sharedInstance].chatRoomController.infoManager.isCurrentHost) {
- // 是不是主持人 只有主持人有操作权限
- _rotaryTableView.enabled = YES;
- }else {
- _rotaryTableView.enabled = NO;
- }
- }
- return _rotaryTableView;
- }
- - (NSMutableArray *)lotteryItems {
- if (!_lotteryItems) {
- _lotteryItems = [NSMutableArray array];
- }
- return _lotteryItems;
- }
- @end
|