JXWheelView.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // JXWheelView.m
  3. // shiku_im
  4. //
  5. // Created by 123 on 2020/6/6.
  6. // Copyright © 2020 Reese. All rights reserved.
  7. //
  8. #import "JXWheelView.h"
  9. @interface LotteryItem : NSObject
  10. @property(nonatomic,copy)NSNumber *time;
  11. @property(nonatomic,assign)int angle;
  12. @property(nonatomic,assign)int selectIndex;
  13. @end
  14. @implementation LotteryItem
  15. @end
  16. @interface JXWheelView ()
  17. @property(nonatomic,strong)UIView* vLine;
  18. @property(nonatomic,strong)CABasicAnimation *rotateAnimation;
  19. @property(nonatomic,strong)NSArray *arrAngle;
  20. @property (nonatomic,strong)UIButton *jiantouBtn; //抽奖按钮
  21. @property (nonatomic,strong)UIImageView *rotalIMG; //转盘背景
  22. @end
  23. @implementation JXWheelView
  24. -(instancetype)initWithFrame:(CGRect)frame{
  25. if (self=[super initWithFrame:frame]) {
  26. UIImageView* rotalIMG = [[UIImageView alloc] initWithFrame:self.bounds];
  27. rotalIMG.image = [UIImage imageNamed:@"zhuanpanback"];
  28. [self addSubview:rotalIMG];
  29. self.rotalIMG=rotalIMG;
  30. self.jiantouBtn = [[UIButton alloc]init];
  31. [self.jiantouBtn setImage:[UIImage imageNamed:@"nodeWheel"] forState:UIControlStateNormal];
  32. [self addSubview:self.jiantouBtn];
  33. [self.jiantouBtn addTarget:self action:@selector(playButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
  34. [self.jiantouBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  35. make.centerX.mas_equalTo(self.rotalIMG.mas_centerX);
  36. make.centerY.mas_equalTo(self.rotalIMG.mas_centerY);
  37. //make.width.mas_equalTo(113);
  38. }];
  39. _arrAngle = @[@[@[@"1",@"15"],@[@"165",@"195"],@[@"345",@"360"]],
  40. @[@[@"105",@"135"],@[@"285",@"315"]],
  41. @[@[@"45",@"75"],@[@"225",@"255"]],@[@[@"15",@"45"],
  42. @[@"75",@"105"],@[@"135",@"165"],@[@"195",@"225"],@[@"255",@"285"],@[@"315",@"345"]]];
  43. _arrAngle = @[@[@[@"1",@"15"],@[@"15",@"30"],@[@"45",@"60"],@[@"60",@"75"],@[@"90",@"105"],@[@"120",@"135"],@[@"180",@"215"],@[@"260",@"305"],@[@"350",@"360"]]];
  44. _arrAngle = @[@[@[@"0",@"45"],@[@"45",@"90"],@[@"90",@"135"],@[@"135",@"180"],@[@"180",@"225"],@[@"225",@"260"],@[@"260",@"305"],@[@"305",@"350"],@[@"350",@"360"]]];
  45. [self statGetStartShop];
  46. }
  47. return self;
  48. }
  49. - (void)statGetStartShop{
  50. [SVProgressHUD show];
  51. long time = (long)[[NSDate date] timeIntervalSince1970];
  52. time = (time *1000 + g_server.timeDifference);
  53. NSString *salt = [NSString stringWithFormat:@"%ld", time];
  54. [g_server getUseract_act_getTurntable:salt andToView:self];
  55. }
  56. - (void)playButtonPressed:(UIButton *)sender
  57. {
  58. int selectIndex = (int)(1 + (arc4random() % (7)));
  59. NSLog(@"抽奖结果 %d",selectIndex);
  60. long time = (long)[[NSDate date] timeIntervalSince1970];
  61. time = (time *1000 + g_server.timeDifference);
  62. NSString *salt = [NSString stringWithFormat:@"%ld", time];
  63. //卷盘抽奖
  64. [g_server getUseract_turntableDo:salt andToView:self];
  65. LotteryItem *item = [[LotteryItem alloc] init];
  66. item.time = [NSNumber numberWithDouble:0.2];
  67. item.selectIndex = selectIndex;
  68. item.angle = [self getAngle:0];
  69. [self lotteryAction:item];
  70. }
  71. - (int)getAngle:(int)index
  72. {
  73. NSArray* arr = [_arrAngle objectAtIndex:index];
  74. //int x = arc4random() % arr.count;
  75. int x = (int)(1 + (arc4random() % (arr.count)));
  76. NSArray* arrJu = [arr objectAtIndex:x];
  77. int angle = arc4random() % ([[arrJu objectAtIndex:1] intValue] - [[arrJu objectAtIndex:0] intValue]) + [[arrJu objectAtIndex:0] intValue];
  78. return x*M_PI/4;
  79. }
  80. - (void)lotteryAction:(LotteryItem*)item
  81. {
  82. if([item.time doubleValue] > 2)
  83. {
  84. if(item.angle > 180)
  85. {
  86. [UIView animateWithDuration:[item.time doubleValue] animations:^{
  87. self.rotalIMG.transform=CGAffineTransformMakeRotation(180*M_PI/180);
  88. }];
  89. }
  90. [UIView animateWithDuration:[item.time doubleValue] animations:^{
  91. _vLine.transform=CGAffineTransformMakeRotation(item.angle*M_PI/180);
  92. }];
  93. }
  94. else
  95. {
  96. [UIView animateWithDuration:[item.time doubleValue] animations:^{
  97. self.rotalIMG.transform=CGAffineTransformMakeRotation(180*M_PI/180);
  98. }];
  99. [UIView animateWithDuration:[item.time doubleValue] animations:^{
  100. self.rotalIMG.transform=CGAffineTransformMakeRotation(360*M_PI/180);
  101. }];
  102. NSNumber* _D = [NSNumber numberWithDouble:[item.time doubleValue] + 0.2];
  103. LotteryItem* _item = [[LotteryItem alloc] init];
  104. _item.time = _D;
  105. _item.angle = item.angle;
  106. [self performSelector:@selector(lotteryAction:) withObject:_item afterDelay:[item.time doubleValue] - [item.time doubleValue] / 2];
  107. }
  108. }
  109. /**
  110. * 返回数据结果
  111. */
  112. -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  113. [SVProgressHUD dismiss];
  114. if([aDownload.action isEqualToString:act_getTurntable])//获取展示奖品
  115. {
  116. NSMutableArray *jpDataArr=[awarJpModel mj_objectArrayWithKeyValuesArray:array1];
  117. [self createAllBtn:jpDataArr];
  118. }
  119. if([aDownload.action isEqualToString:act_turntableDo])//获取展示奖品
  120. {
  121. int aaaaaa=[dict[@"rewardCode"] intValue];
  122. [self choujiangResult:aaaaaa];
  123. }
  124. }
  125. - (void)choujiangResult:(int )selectAAaa{
  126. int selectIndex = (int)(selectAAaa + (arc4random() % (12)));
  127. selectIndex = selectAAaa;
  128. }
  129. /**获取抽奖数据*/
  130. - (void)createAllBtn:(NSMutableArray *)dataArr{
  131. dispatch_async(dispatch_get_main_queue(), ^{
  132. for (int i = 0; i <dataArr.count; i ++) {
  133. awarJpModel *model=dataArr[i];
  134. UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,M_PI * CGRectGetHeight(self.bounds)/8,
  135. CGRectGetHeight(self.bounds)/2)];
  136. label.layer.anchorPoint = CGPointMake(0.5, 1);
  137. label.center = CGPointMake(CGRectGetHeight(self.bounds)/2, CGRectGetHeight(self.bounds)/2);
  138. label.text = [NSString stringWithFormat:@"%@",model.rewardNum];
  139. CGFloat angle = M_PI * 2 / 8 * i;
  140. label.textAlignment = NSTextAlignmentCenter;
  141. label.font = [UIFont systemFontOfSize:12];
  142. label.transform = CGAffineTransformMakeRotation(angle);
  143. [self.rotalIMG addSubview:label];
  144. UILabel *labelText = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, M_PI * CGRectGetHeight(self.bounds)/8 - 20, 40)];
  145. labelText.text = model.rewardName;
  146. labelText.font = [UIFont systemFontOfSize:10];
  147. labelText.numberOfLines = 2;
  148. labelText.textAlignment = NSTextAlignmentCenter;
  149. [label addSubview:labelText];
  150. }
  151. });
  152. }
  153. -(int) didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{
  154. [SVProgressHUD dismiss];
  155. NSString * errorCode = [NSString stringWithFormat:@"%@",[dict objectForKey:@"resultMsg"]];
  156. if([errorCode isEqualToString:@"权限验证失败"])
  157. {
  158. }
  159. self.jiantouBtn.userInteractionEnabled=YES;
  160. return 1;
  161. }
  162. @end