turntableView.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. //
  2. // turntableView.m
  3. // yunbaolive
  4. //
  5. // Created by IOS1 on 2019/6/5.
  6. // Copyright © 2019 cat. All rights reserved.
  7. //
  8. #import "turntableView.h"
  9. #import "turntableRuleView.h"
  10. #import "turntableRecordView.h"
  11. #import "turntableResultView.h"
  12. @implementation turntableView{
  13. UIView *bottomView;
  14. UIImageView *turntableImgView;
  15. UIButton *startButton;
  16. NSDictionary *awards;
  17. NSArray *levellist;
  18. NSArray *turngiftlist;
  19. CGFloat startValue;
  20. CGFloat endValue;
  21. int resultIndex;
  22. NSMutableArray *typeBtnArray;
  23. NSDictionary *typeDic;
  24. UIImageView *priceView;
  25. UILabel *priceLabel;
  26. turntableRuleView *ruleView;
  27. turntableRecordView *recordView;
  28. turntableResultView *resultView;
  29. NSArray *resultGiftArray;
  30. }
  31. - (void)doHideSelf{
  32. [UIView animateWithDuration:0.3 animations:^{
  33. bottomView.y = _window_height;
  34. } completion:^(BOOL finished) {
  35. self.hidden = YES;
  36. }];
  37. }
  38. - (void)show{
  39. self.hidden = NO;
  40. [UIView animateWithDuration:0.3 animations:^{
  41. // bottomView.y = _window_height * 0.33 - ShowDiff;
  42. bottomView.y = _window_height -447 ;//- ShowDiff;
  43. }];
  44. }
  45. -(instancetype)init{
  46. if (self = [super init]) {
  47. self.frame = CGRectMake(0, 0, _window_width, _window_height);
  48. startValue = 0;
  49. [self creatUI];
  50. }
  51. return self;
  52. }
  53. - (void)creatUI{
  54. UIButton *button = [UIButton buttonWithType:0];
  55. // button.frame = CGRectMake(0, 0, _window_width, _window_height * 0.33 - ShowDiff);
  56. button.frame = CGRectMake(0, 0, _window_width, _window_height -447);
  57. [button addTarget:self action:@selector(doHideSelf) forControlEvents:UIControlEventTouchUpInside];
  58. [self addSubview:button];
  59. // bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, _window_height, _window_width, _window_height * 0.67 + ShowDiff)];
  60. bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, _window_height, _window_width, 447)];
  61. bottomView.backgroundColor = [UIColor clearColor];
  62. [self addSubview:bottomView];
  63. UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _window_width, bottomView.height)];
  64. imageV.contentMode = UIViewContentModeScaleAspectFill;
  65. imageV.image = [UIImage imageNamed:@"turntable_转盘背景"];
  66. [bottomView addSubview:imageV];
  67. UIImageView *ddd = [[UIImageView alloc]init];
  68. ddd.image = [UIImage imageNamed:@"大转盘底座"];
  69. [bottomView addSubview:ddd];
  70. UIImageView *yinyingImageView = [[UIImageView alloc]init];
  71. yinyingImageView.image = [UIImage imageNamed:@"turntable_转盘阴影"];
  72. [bottomView addSubview:yinyingImageView];
  73. [yinyingImageView mas_makeConstraints:^(MASConstraintMaker *make) {
  74. make.centerX.equalTo(bottomView);
  75. make.width.equalTo(bottomView).multipliedBy(0.89);
  76. make.centerY.equalTo(bottomView).multipliedBy(0.915);
  77. make.height.equalTo(yinyingImageView.mas_width);
  78. }];
  79. turntableImgView = [[UIImageView alloc]init];
  80. turntableImgView.image = [UIImage imageNamed:@"turntable_转盘_new"];
  81. [bottomView addSubview:turntableImgView];
  82. [turntableImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  83. make.center.equalTo(yinyingImageView);
  84. make.width.equalTo(bottomView).multipliedBy(0.73);
  85. make.height.equalTo(turntableImgView.mas_width);
  86. }];
  87. UIImageView *turntableBack = [[UIImageView alloc]init];
  88. turntableBack.image = [UIImage imageNamed:@"turntable_转盘3"];
  89. [bottomView addSubview:turntableBack];
  90. [turntableBack mas_makeConstraints:^(MASConstraintMaker *make) {
  91. make.center.equalTo(turntableImgView);
  92. make.width.equalTo(turntableImgView).multipliedBy(1.15);
  93. make.height.equalTo(turntableImgView).multipliedBy(1.15);
  94. }];
  95. [ddd mas_makeConstraints:^(MASConstraintMaker *make) {
  96. make.top.equalTo(turntableImgView.mas_centerY);
  97. make.bottom.equalTo(bottomView.mas_bottom).offset(-50);
  98. make.centerX.equalTo(bottomView);
  99. make.width.equalTo(self).multipliedBy(0.55);
  100. }];
  101. awards = @{
  102. @"0": @[
  103. @{
  104. @"min": @4,
  105. @"max":@41
  106. },
  107. ],
  108. @"1":@[
  109. @{
  110. @"min": @49,
  111. @"max":@86
  112. }
  113. ],
  114. @"2":@[
  115. @{
  116. @"min": @94,
  117. @"max":@131
  118. }
  119. ] ,
  120. @"3":@[
  121. @{
  122. @"min": @139,
  123. @"max":@176
  124. }
  125. ],
  126. @"4": @[
  127. @{
  128. @"min": @184,
  129. @"max":@221
  130. }
  131. ],
  132. @"5":@[
  133. @{
  134. @"min": @229,
  135. @"max":@266
  136. }
  137. ],
  138. @"6":@[
  139. @{
  140. @"min": @274,
  141. @"max":@311
  142. }
  143. ],
  144. @"7":@[
  145. @{
  146. @"min": @319,
  147. @"max":@356
  148. }
  149. ]
  150. };
  151. [bottomView layoutIfNeeded];
  152. [self requestData];
  153. [UIView animateWithDuration:0.3 animations:^{
  154. bottomView.y = button.bottom;
  155. }];
  156. }
  157. - (void)requestData{
  158. [YBNetworking postWithUrl:@"Turntable.GetTurntable" Dic:nil Suc:^(int code, id info, NSString *msg) {
  159. if (code == 0) {
  160. NSDictionary *infoDic = [info firstObject];
  161. levellist = [infoDic valueForKey:@"config"];
  162. turngiftlist = [infoDic valueForKey:@"list"];
  163. [self creatLastView];
  164. }
  165. } Fail:^(id fail) {
  166. }];
  167. }
  168. - (void)creatLastView{
  169. for (int i = 0; i < turngiftlist.count; i++) {
  170. NSDictionary *dic = turngiftlist[i];
  171. UIView *view = [[UIView alloc]initWithFrame:CGRectMake(turntableImgView.width/2-60, 0, 120, turntableImgView.width)];
  172. [turntableImgView addSubview:view];
  173. UIImageView *ImgV = [[UIImageView alloc]init];
  174. ImgV.contentMode = UIViewContentModeScaleAspectFit;
  175. [view addSubview:ImgV];
  176. if ([minstr([dic valueForKey:@"type"]) isEqual:@"2"]) {
  177. [ImgV sd_setImageWithURL:[NSURL URLWithString:minstr([dic valueForKey:@"thumb"])]];
  178. [ImgV mas_makeConstraints:^(MASConstraintMaker *make) {
  179. make.centerX.equalTo(view);
  180. make.centerY.equalTo(view).multipliedBy(0.45);
  181. make.width.height.mas_equalTo(35);
  182. }];
  183. }else{
  184. UILabel *label = [[UILabel alloc]init];
  185. label.textAlignment = NSTextAlignmentCenter;
  186. label.textColor = [UIColor whiteColor];
  187. label.numberOfLines = 0;
  188. label.adjustsFontSizeToFitWidth = YES;
  189. [view addSubview:label];
  190. NSString *nameStr = minstr([dic valueForKey:@"type_val"]);
  191. if ([minstr([dic valueForKey:@"type"]) isEqual:@"0"]) {
  192. label.font = [UIFont boldSystemFontOfSize:12];
  193. label.numberOfLines = 0;
  194. label.text = YZMsg(@"再接再厉");
  195. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  196. make.centerX.equalTo(view);
  197. make.centerY.equalTo(view).multipliedBy(0.5);
  198. make.width.equalTo(view.mas_width).multipliedBy(0.5);
  199. }];
  200. }else{
  201. label.text = nameStr;
  202. label.font = [UIFont systemFontOfSize:10];
  203. // ImgV.image = [UIImage imageNamed:@"logFirst_钻石"];
  204. [ImgV sd_setImageWithURL:[NSURL URLWithString:minstr([dic valueForKey:@"thumb"])]];
  205. [ImgV mas_makeConstraints:^(MASConstraintMaker *make) {
  206. make.centerX.equalTo(view);
  207. make.centerY.equalTo(view).multipliedBy(0.45);
  208. make.width.height.mas_equalTo(30);
  209. }];
  210. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  211. make.centerX.equalTo(view);
  212. make.height.mas_equalTo(20);
  213. make.top.equalTo(ImgV.mas_bottom);
  214. }];
  215. }
  216. }
  217. if (minstr([dic valueForKey:@"thumb"]).length > 6) {
  218. }else{
  219. }
  220. // if (i != 0) {
  221. NSDictionary *radiusDic = [[awards valueForKey:[NSString stringWithFormat:@"%d",i]] firstObject];
  222. CGFloat aa = ([minstr([radiusDic valueForKey:@"max"]) floatValue] - [minstr([radiusDic valueForKey:@"min"]) floatValue])/2+[minstr([radiusDic valueForKey:@"min"]) floatValue];
  223. view.transform = CGAffineTransformMakeRotation(radians(aa));
  224. // }
  225. }
  226. startButton = [UIButton buttonWithType:0];
  227. // startButton.size = CGSizeMake(_window_width*0.2, _window_width*0.2);
  228. // startButton.center = turntableImgView.center;
  229. [startButton setImage:[UIImage imageNamed:getImagename(@"turntable_开始指针")] forState:0];
  230. [startButton addTarget:self action:@selector(startButtonClick) forControlEvents:UIControlEventTouchUpInside];
  231. [bottomView addSubview:startButton];
  232. [startButton mas_makeConstraints:^(MASConstraintMaker *make) {
  233. make.center.equalTo(turntableImgView);
  234. make.width.equalTo(bottomView).multipliedBy(0.32);
  235. make.height.equalTo(startButton.mas_width);
  236. }];
  237. typeBtnArray = [NSMutableArray array];
  238. typeDic = levellist[0];
  239. // CGFloat hhhhhh = startButton.bottom + 20;
  240. for (int i = 0 ; i < levellist.count; i ++) {
  241. NSDictionary *dic = levellist[i];
  242. UIButton *btn = [UIButton buttonWithType:0];
  243. btn.titleLabel.font = [UIFont systemFontOfSize:13];
  244. btn.titleLabel.numberOfLines = 2;
  245. btn.titleLabel.textColor = [UIColor whiteColor];
  246. [btn setAttributedTitle:[self buttonAttributedString:[NSString stringWithFormat:YZMsg(@"转%@次"),minstr([dic valueForKey:@"times"])] andCoin:minstr([dic valueForKey:@"coin"])] forState:0];
  247. btn.titleLabel.textAlignment = NSTextAlignmentCenter;
  248. [btn setBackgroundImage:[UIImage imageNamed:@"turntable_btn_sel"] forState:UIControlStateSelected];
  249. [btn setBackgroundImage:[UIImage imageNamed:@"turntable_btn_nor"] forState:UIControlStateNormal];
  250. [btn addTarget:self action:@selector(typeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  251. btn.tag = 10086+i;
  252. btn.selected = NO;
  253. [bottomView addSubview:btn];
  254. [btn mas_makeConstraints:^(MASConstraintMaker *make) {
  255. make.centerY.equalTo(bottomView).multipliedBy(1.81);
  256. make.left.equalTo(bottomView).offset(_window_width*0.04+i*(_window_width*0.32));
  257. make.width.mas_equalTo(_window_width *0.28);
  258. make.height.equalTo(btn.mas_width).multipliedBy(0.43);
  259. }];
  260. [typeBtnArray addObject:btn];
  261. if (i == 0) {
  262. btn.selected = YES;
  263. }
  264. // if (i == 0) {
  265. // priceView = [[UIImageView alloc]initWithFrame:CGRectMake(btn.x+btn.width*0.135, btn.top - btn.height*0.6, btn.width*0.73, btn.height * 0.7)];
  266. // priceView.image = [UIImage imageNamed:@"turntable_price"];
  267. // [bottomView addSubview:priceView];
  268. // priceView.hidden = YES;
  269. // priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, priceView.width, priceView.height * 0.9)];
  270. // priceLabel.font = [UIFont systemFontOfSize:11];
  271. // priceLabel.textAlignment = NSTextAlignmentCenter;
  272. // [priceView addSubview:priceLabel];
  273. // [PublicObj lineViewWithFrame:CGRectMake(0, btn.bottom, _window_width, 1) andColor:RGB_COLOR(@"#343535", 1) andView:bottomView];
  274. // hhhhhh = (btn.bottom+1);
  275. // }
  276. }
  277. NSArray *array = @[YZMsg(@"游戏规则"),YZMsg(@"中奖记录")];
  278. for (int i = 0; i < array.count; i ++) {
  279. UIButton *buton = [UIButton buttonWithType:0];
  280. buton.frame = CGRectMake(15 + i * (_window_width-80-15), 20, 64, 24);
  281. [buton setTitle:array[i] forState:0];
  282. // [buton setBackgroundColor:[[UIColor blackColor]colorWithAlphaComponent:0.2]];
  283. [buton setTitleColor:[UIColor whiteColor] forState:0];
  284. buton.titleLabel.font = SYS_Font(11);
  285. buton.layer.cornerRadius = 12.0;
  286. buton.layer.borderWidth = 1;
  287. buton.layer.borderColor = [UIColor whiteColor].CGColor;
  288. buton.layer.masksToBounds = YES;
  289. [buton addTarget:self action:@selector(bottomButtonClick:) forControlEvents:UIControlEventTouchUpInside];
  290. buton.tag = 7655 + i;
  291. buton.titleLabel.adjustsFontSizeToFitWidth = YES;
  292. [bottomView addSubview:buton];
  293. }
  294. }
  295. //角度转弧度
  296. double radians(float degrees) {
  297. return degrees*M_PI/180;
  298. }
  299. - (void)startButtonClick{
  300. // if (typeDic) {
  301. [self requestReasult];
  302. // }else{
  303. // [MBProgressHUD showError:@"请选择转盘价格"];
  304. // }
  305. }
  306. - (void)requestReasult{
  307. startButton.userInteractionEnabled = NO;
  308. [YBNetworking postWithUrl:@"Turntable.Turn" Dic:@{@"id":minstr([typeDic valueForKey:@"id"]),@"liveuid":minstr([_zhuboMsg valueForKey:@"uid"]),@"stream":minstr([_zhuboMsg valueForKey:@"stream"])} Suc:^(int code, id info, NSString *msg) {
  309. if (code == 0) {
  310. NSDictionary *rDic = [info firstObject];
  311. [Config saveUserCoin:minstr([rDic valueForKey:@"coin"])];
  312. resultGiftArray = [rDic valueForKey:@"list"];
  313. resultIndex = 0;
  314. NSDictionary *infoDic;
  315. if ([resultGiftArray count] > 0) {
  316. infoDic = [resultGiftArray lastObject];
  317. if (self.delegate) {
  318. [self.delegate turntableZhongjianla];
  319. }
  320. }else{
  321. infoDic = @{@"type":@"0"};
  322. }
  323. for (int i = 0; i < turngiftlist.count; i ++) {
  324. NSDictionary *dic = turngiftlist[i];
  325. if ([minstr([infoDic valueForKey:@"type"]) isEqual:@"0"]) {
  326. if ([minstr([dic valueForKey:@"type"]) isEqual:@"0"]) {
  327. resultIndex = i;
  328. }
  329. }else{
  330. if ([minstr([infoDic valueForKey:@"id"]) isEqual:minstr([dic valueForKey:@"id"])]) {
  331. resultIndex = i;
  332. }
  333. }
  334. }
  335. //resultIndex = arc4random()%8;
  336. endValue = [self fetchResult];
  337. CABasicAnimation* rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  338. rotationAnimation.delegate = self;
  339. rotationAnimation.fromValue = @(startValue);
  340. rotationAnimation.toValue = @(endValue);
  341. rotationAnimation.duration = 3.0f;
  342. rotationAnimation.autoreverses = NO;
  343. rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  344. rotationAnimation.removedOnCompletion = NO;
  345. rotationAnimation.fillMode = kCAFillModeBoth;
  346. [turntableImgView.layer addAnimation:rotationAnimation forKey:@"revItUpAnimation"];
  347. }else{
  348. startButton.userInteractionEnabled = YES;
  349. [MBProgressHUD showError:msg];
  350. }
  351. } Fail:^(id fail) {
  352. startButton.userInteractionEnabled = YES;
  353. [MBProgressHUD showError:YZMsg(@"网络错误")];
  354. }];
  355. }
  356. - (CGFloat)fetchResult{
  357. srand((unsigned)time(0));
  358. float random;
  359. for (NSString *str in [awards allKeys]) {
  360. if ([str intValue] == resultIndex) {
  361. int bb;
  362. if (resultIndex == 0) {
  363. bb = arc4random()%2;
  364. }else{
  365. bb = 0;
  366. }
  367. NSDictionary *content = awards[str][bb];
  368. int min = [content[@"min"] intValue];
  369. int max = [content[@"max"] intValue];
  370. srand((unsigned)time(0));
  371. random = rand() % (max - min) +min;
  372. }
  373. }
  374. // return radians(random + 360*5);
  375. return (M_PI*2 - radians(random)) + M_PI*2*5;
  376. }
  377. -(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
  378. startButton.userInteractionEnabled = YES;
  379. startValue = endValue;
  380. if (startValue >= endValue) {
  381. startValue = startValue - radians(360*10);
  382. }
  383. NSLog(@"startValue = %f",startValue);
  384. NSLog(@"endValue = %f\n",endValue);
  385. // if (resultIndex == 0) {
  386. // [MBProgressHUD showError:@"谢谢参与,再接再厉"];
  387. // }else{
  388. if (resultView) {
  389. [resultView removeFromSuperview];
  390. resultView = nil;
  391. }
  392. // NSArray *aaa = @[@"",@"",@"",@"",@"",@""];
  393. if (resultGiftArray.count > 0) {
  394. resultView = [[turntableResultView alloc]initWithResultArray:resultGiftArray];
  395. [self addSubview:resultView];
  396. }
  397. // [MBProgressHUD showError:[NSString stringWithFormat:@"恭喜你,获得%@",minstr([turngiftlist[resultIndex] valueForKey:@"giftname"])]];
  398. // }
  399. }
  400. #pragma mark ============jiage=============
  401. - (void)typeBtnClick:(UIButton *)sender{
  402. // if (sender.selected) {
  403. // typeDic = nil;
  404. // sender.selected = NO;
  405. // sender.imageView.transform = CGAffineTransformMakeScale(1, 1);
  406. // priceView.hidden = YES;
  407. // return;
  408. // }
  409. for (int i = 0; i < typeBtnArray.count; i ++) {
  410. UIButton *btn = typeBtnArray[i];
  411. if (btn == sender) {
  412. btn.selected = YES;
  413. typeDic = levellist[i];
  414. // btn.imageView.transform = CGAffineTransformMakeScale(2, 2);
  415. // priceLabel.text = [NSString stringWithFormat:@"%@%@",minstr([typeDic valueForKey:@"coin"]),[common name_coin]];
  416. // priceView.centerX = btn.centerX;
  417. // priceView.hidden = NO;
  418. }else{
  419. btn.selected = NO;
  420. // btn.imageView.transform = CGAffineTransformMakeScale(1, 1);
  421. }
  422. }
  423. }
  424. - (void)bottomButtonClick:(UIButton *)sender{
  425. if (sender.tag == 7655) {
  426. if (!ruleView) {
  427. ruleView = [[turntableRuleView alloc]init];
  428. [self addSubview:ruleView];
  429. }else{
  430. ruleView.hidden = NO;
  431. [ruleView show];
  432. }
  433. }else{
  434. if (!recordView) {
  435. recordView = [[turntableRecordView alloc]init];
  436. [self addSubview:recordView];
  437. }else{
  438. recordView.hidden = NO;
  439. [recordView show];
  440. }
  441. }
  442. }
  443. - (NSAttributedString *)buttonAttributedString:(NSString *)name andCoin:(NSString *)coin{
  444. NSString *btnNameStr1 = [NSString stringWithFormat:@"%@\n%@%@",name,coin,[common name_coin]];
  445. NSMutableAttributedString *AttributedStr1 = [[NSMutableAttributedString alloc]initWithString:btnNameStr1];
  446. [AttributedStr1 addAttribute:NSForegroundColorAttributeName
  447. value:[[UIColor whiteColor] colorWithAlphaComponent:0.5]
  448. range:NSMakeRange(name.length, btnNameStr1.length-name.length)];
  449. [AttributedStr1 addAttribute:NSFontAttributeName
  450. value:[UIFont systemFontOfSize:10]
  451. range:NSMakeRange(name.length, btnNameStr1.length-name.length)];
  452. return AttributedStr1;
  453. }
  454. @end