// // JXWheelView.m // shiku_im // // Created by 123 on 2020/6/6. // Copyright © 2020 Reese. All rights reserved. // #import "JXWheelView.h" @interface LotteryItem : NSObject @property(nonatomic,copy)NSNumber *time; @property(nonatomic,assign)int angle; @property(nonatomic,assign)int selectIndex; @end @implementation LotteryItem @end @interface JXWheelView () @property(nonatomic,strong)UIView* vLine; @property(nonatomic,strong)CABasicAnimation *rotateAnimation; @property(nonatomic,strong)NSArray *arrAngle; @property (nonatomic,strong)UIButton *jiantouBtn; //抽奖按钮 @property (nonatomic,strong)UIImageView *rotalIMG; //转盘背景 @end @implementation JXWheelView -(instancetype)initWithFrame:(CGRect)frame{ if (self=[super initWithFrame:frame]) { UIImageView* rotalIMG = [[UIImageView alloc] initWithFrame:self.bounds]; rotalIMG.image = [UIImage imageNamed:@"zhuanpanback"]; [self addSubview:rotalIMG]; self.rotalIMG=rotalIMG; self.jiantouBtn = [[UIButton alloc]init]; [self.jiantouBtn setImage:[UIImage imageNamed:@"nodeWheel"] forState:UIControlStateNormal]; [self addSubview:self.jiantouBtn]; [self.jiantouBtn addTarget:self action:@selector(playButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; [self.jiantouBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.rotalIMG.mas_centerX); make.centerY.mas_equalTo(self.rotalIMG.mas_centerY); //make.width.mas_equalTo(113); }]; _arrAngle = @[@[@[@"1",@"15"],@[@"165",@"195"],@[@"345",@"360"]], @[@[@"105",@"135"],@[@"285",@"315"]], @[@[@"45",@"75"],@[@"225",@"255"]],@[@[@"15",@"45"], @[@"75",@"105"],@[@"135",@"165"],@[@"195",@"225"],@[@"255",@"285"],@[@"315",@"345"]]]; _arrAngle = @[@[@[@"1",@"15"],@[@"15",@"30"],@[@"45",@"60"],@[@"60",@"75"],@[@"90",@"105"],@[@"120",@"135"],@[@"180",@"215"],@[@"260",@"305"],@[@"350",@"360"]]]; _arrAngle = @[@[@[@"0",@"45"],@[@"45",@"90"],@[@"90",@"135"],@[@"135",@"180"],@[@"180",@"225"],@[@"225",@"260"],@[@"260",@"305"],@[@"305",@"350"],@[@"350",@"360"]]]; [self statGetStartShop]; } return self; } - (void)statGetStartShop{ [SVProgressHUD show]; long time = (long)[[NSDate date] timeIntervalSince1970]; time = (time *1000 + g_server.timeDifference); NSString *salt = [NSString stringWithFormat:@"%ld", time]; [g_server getUseract_act_getTurntable:salt andToView:self]; } - (void)playButtonPressed:(UIButton *)sender { int selectIndex = (int)(1 + (arc4random() % (7))); NSLog(@"抽奖结果 %d",selectIndex); long time = (long)[[NSDate date] timeIntervalSince1970]; time = (time *1000 + g_server.timeDifference); NSString *salt = [NSString stringWithFormat:@"%ld", time]; //卷盘抽奖 [g_server getUseract_turntableDo:salt andToView:self]; LotteryItem *item = [[LotteryItem alloc] init]; item.time = [NSNumber numberWithDouble:0.2]; item.selectIndex = selectIndex; item.angle = [self getAngle:0]; [self lotteryAction:item]; } - (int)getAngle:(int)index { NSArray* arr = [_arrAngle objectAtIndex:index]; //int x = arc4random() % arr.count; int x = (int)(1 + (arc4random() % (arr.count))); NSArray* arrJu = [arr objectAtIndex:x]; int angle = arc4random() % ([[arrJu objectAtIndex:1] intValue] - [[arrJu objectAtIndex:0] intValue]) + [[arrJu objectAtIndex:0] intValue]; return x*M_PI/4; } - (void)lotteryAction:(LotteryItem*)item { if([item.time doubleValue] > 2) { if(item.angle > 180) { [UIView animateWithDuration:[item.time doubleValue] animations:^{ self.rotalIMG.transform=CGAffineTransformMakeRotation(180*M_PI/180); }]; } [UIView animateWithDuration:[item.time doubleValue] animations:^{ _vLine.transform=CGAffineTransformMakeRotation(item.angle*M_PI/180); }]; } else { [UIView animateWithDuration:[item.time doubleValue] animations:^{ self.rotalIMG.transform=CGAffineTransformMakeRotation(180*M_PI/180); }]; [UIView animateWithDuration:[item.time doubleValue] animations:^{ self.rotalIMG.transform=CGAffineTransformMakeRotation(360*M_PI/180); }]; NSNumber* _D = [NSNumber numberWithDouble:[item.time doubleValue] + 0.2]; LotteryItem* _item = [[LotteryItem alloc] init]; _item.time = _D; _item.angle = item.angle; [self performSelector:@selector(lotteryAction:) withObject:_item afterDelay:[item.time doubleValue] - [item.time doubleValue] / 2]; } } /** * 返回数据结果 */ -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{ [SVProgressHUD dismiss]; if([aDownload.action isEqualToString:act_getTurntable])//获取展示奖品 { NSMutableArray *jpDataArr=[awarJpModel mj_objectArrayWithKeyValuesArray:array1]; [self createAllBtn:jpDataArr]; } if([aDownload.action isEqualToString:act_turntableDo])//获取展示奖品 { int aaaaaa=[dict[@"rewardCode"] intValue]; [self choujiangResult:aaaaaa]; } } - (void)choujiangResult:(int )selectAAaa{ int selectIndex = (int)(selectAAaa + (arc4random() % (12))); selectIndex = selectAAaa; } /**获取抽奖数据*/ - (void)createAllBtn:(NSMutableArray *)dataArr{ dispatch_async(dispatch_get_main_queue(), ^{ for (int i = 0; i