// // LuckyDrawView.m // 大转盘 // // Created by 谭启宏 on 16/3/2. // Copyright © 2016年 tqh. All rights reserved. // #import "LuckyDrawView.h" //抽奖大转盘UI分为3部分:中心按钮点击,转盘,转盘内容 //需要可以自定义转盘内容的数量,内容, //指针 // @interface LuckyDrawView () @property (nonatomic,strong)UIButton *jiantouBtn; //抽奖按钮 @property (nonatomic,strong)UIButton *playButton; //抽奖按钮 @property (nonatomic,strong)UIImageView *rotateWheel; //转盘背景 @property (nonatomic,strong)CADisplayLink *link; //@property (nonatomic,assign)NSInteger number; @property (nonatomic,strong) NSMutableArray *dataArr; @property (nonatomic,assign) int startValue; @property (nonatomic,assign) int startAng; @property (nonatomic,assign) int addNumber; @property (nonatomic,weak) CABasicAnimation* animation; @end @implementation LuckyDrawView - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { _dataArr=[NSMutableArray array]; _startValue=0; _addNumber=2; [self commit:@[].mutableCopy]; [self statGetStartShop]; } return self; } - (void)commit:(NSMutableArray *)dataArr { self.numberIndex = 1; //分隔线 //self.layer.borderWidth = 1; self.rotateWheel = [[UIImageView alloc]initWithFrame:self.bounds]; self.rotateWheel.image = [UIImage imageNamed:@"zhuanpanback"]; [self addSubview:self.rotateWheel]; 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.rotateWheel.mas_centerX); make.centerY.mas_equalTo(self.rotateWheel.mas_centerY); //make.width.mas_equalTo(113); //make.height.mas_equalTo(124); }]; /* UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, M_PI * CGRectGetHeight(self.bounds)/12, CGRectGetHeight(self.bounds)/2)]; //imageview.backgroundColor=[UIColor greenColor]; imageview.layer.anchorPoint = CGPointMake(0.5, 1); imageview.alpha = 0.5; imageview.image = [UIImage imageNamed:@"LuckyRototeSelected"]; imageview.contentMode = UIViewContentModeScaleAspectFill; imageview.center = CGPointMake(CGRectGetHeight(self.bounds)/2, CGRectGetHeight(self.bounds)/2); [self addSubview:imageview]; //这个不旋转 //NSArray *images=@[@"wheelcoin1",@"wheelcoin2",@"wheelcoin3",@"wheelcoin4",@"wheelcoin5",@"wheelcoin6",@"wheelcoin5",@"wheelcoin6"]; */ //NSArray *images=@[@"wheelcoin1",@"wheelcoin2",@"wheelcoin3",@"wheelcoin4",@"wheelcoin5",@"wheelcoin6",@"wheelcoin5",@"wheelcoin6"]; [self startRotate]; } - (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]; } #pragma mark - 事件监听 - (void)choujiangJiekou{ 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]; } - (void)playButtonPressed:(UIButton *)sender { sender.userInteractionEnabled=NO; // [self choujiangJiekou]; //度数*返回来的数 //M_PI*2/12*(13-12) 12 //M_PI*2/12*2 11 //M_PI*2/12*3 10 //M_PI*2/12*4 9 int selectIndex = (int)(1 + (arc4random() % (7))); NSLog(@"抽奖结果 %d",selectIndex); // if (![self.rotateWheel.layer animationForKey:@"zhuandong"]) { if (_addNumber<=0) { self.link.paused = YES; self.playButton.userInteractionEnabled=YES; // [self.rotateWheel.layer removeAnimationForKey:@"zhuandong"]; self.rotateWheel.transform = CGAffineTransformMakeRotation(M_PI*2/8*(13-selectIndex)); self.link.paused = YES; [self.rotateWheel.layer removeAnimationForKey:@"zhuandong"]; self.playButton.userInteractionEnabled=YES; }else{ [self performSelector:@selector(playButtonPressed:) withObject:sender afterDelay:_addNumber]; } CABasicAnimation* animation = [[CABasicAnimation alloc] init]; animation.keyPath = @"transform.rotation"; animation.toValue = @(2 * M_PI * 5 - M_PI*2/8*(13-selectIndex)); animation.duration = 5; animation.removedOnCompletion = NO; animation.fillMode = kCAFillModeForwards; animation.speed=_addNumber; [self.rotateWheel.layer addAnimation:animation forKey:@"zhuandong"]; _addNumber-=0.05; return; // } } - (void)Rotate2 { [self playButtonPressed:self.playButton]; } // 开始转动(一直不停的转动) - (void)startRotate { CADisplayLink* link = [CADisplayLink displayLinkWithTarget:self selector:@selector(Rotate)]; [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; self.link = link; } // - (void)Rotate { _startAng-=0.3; self.animation.speed=_startAng; //每次旋转6° self.rotateWheel.transform = CGAffineTransformRotate(self.rotateWheel.transform, M_PI * 2 / 8/ 60 ); } - (void)repetWithAnimation:(BOOL)isYes { [UIView animateWithDuration:1 animations:^{ self.rotateWheel.transform = CGAffineTransformMakeRotation(M_PI*2/8*(13-self.numberIndex)); self.link.paused = YES; } completion:^(BOOL finished) { if (isYes) { [self repetWithAnimation:YES]; } // UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"恭喜你!被骗了!!!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; // [alert show]; if (_delegate && [_delegate respondsToSelector:@selector(LuckyDrawViewDidFinishWidthIndex:)]) { //[_delegate LuckyDrawViewDidFinishWidthIndex:self.numberIndex]; } self.numberIndex = 1; }]; } #pragma mark - UIAlertViewDelegate - (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { self.link.paused = NO; } /** * 返回数据结果 */ -(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]; } } /* float quadraticEaseInOut(float t) { return (t < 0.5)? (2 * t * t): (-2 * t * t) + (4 * t) - 1; } float bounceEaseOut(float t) { if (t < 4/11.0) { return (121 * t * t)/16.0; } else if (t < 8/11.0) { return (363/40.0 * t * t) - (99/10.0 * t) + 17/5.0; } else if (t < 9/10.0) { return (4356/361.0 * t * t) - (35442/1805.0 * t) + 16061/1805.0; } return (54/5.0 * t * t) - (513/25.0 * t) + 268/25.0; } float interpolate(float from, float to, float time) { return (to - from) * time + from; } - (id)interpolateFromValue:(id)fromValue toValue:(id)toValue time:(float)time { if ([fromValue isKindOfClass:[NSValue class]]) { //get type const char *type = [fromValue objCType]; if (strcmp(type, @encode(CGPoint)) == 0) { CGPoint from = [fromValue CGPointValue]; CGPoint to = [toValue CGPointValue]; CGPoint result = CGPointMake(interpolate(from.x, to.x, time), interpolate(from.y, to.y, time)); return [NSValue valueWithCGPoint:result]; } } //provide safe default implementation return (time < 0.5)? fromValue: toValue; } - (void)animate { //reset ball to top of screen self.rotateWheel.center = CGPointMake(150, 32); //set up animation parameters NSValue *fromValue = [NSValue valueWithCGPoint:CGPointMake(150, 32)]; NSValue *toValue = [NSValue valueWithCGPoint:CGPointMake(150, 268)]; CFTimeInterval duration = 1.0; //generate keyframes NSInteger numFrames = duration * 60; NSMutableArray *frames = [NSMutableArray array]; for (int i = 0; i < numFrames; i++) { float time = 1/(float)numFrames * i; //apply easing time = bounceEaseOut(time); //add keyframe [frames addObject:[self interpolateFromValue:fromValue toValue:toValue time:time]]; } //create keyframe animation CAKeyframeAnimation *animation = [CAKeyframeAnimation animation]; animation.keyPath = @"position"; animation.duration = 1.0; animation.delegate = self; animation.values = frames; //apply animation [self.rotateWheel.layer addAnimation:animation forKey:nil]; } */ - (void)choujiangResult:(int )selectAAaa{ int selectIndex = (int)(selectAAaa + (arc4random() % (12))); selectIndex = selectAAaa; NSLog(@"抽奖结果 %d",selectIndex); if (![self.rotateWheel.layer animationForKey:@"zhuandong"]) { CABasicAnimation* animation = [[CABasicAnimation alloc] init]; animation.keyPath = @"transform.rotation"; // animation.toValue = @(2 * M_PI * 5 - M_PI*2/8*(selectIndex-self.numberIndex)); double endValue = _startValue+(2 * M_PI * 5 - M_PI*2/8*(selectIndex)); if (selectAAaa%2==0) { selectAAaa-=2; } if (selectIndex==1) { endValue=_startValue + (selectAAaa* M_PI/4)+2*M_PI+135; } if (selectIndex==5) { endValue=_startValue + (selectAAaa* M_PI/4)+2*M_PI-135; } //这里的endValue单位是弧度,2PI是一圈 animation.fromValue = @(_startValue); animation.toValue = @(endValue); //旋转时长 //animation.duration = (endValue - _startValue)/(M_PI*2); //animation.autoreverses = NO; animation.timeOffset = 0.0; animation.beginTime = 0.0; animation.speed=10; animation.duration = 2; [self.rotateWheel.layer addAnimation:animation forKey:@"zhuandong"]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(animation.duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.jiantouBtn.userInteractionEnabled=YES; self.rotateWheel.transform = CGAffineTransformMakeRotation(M_PI*2/8*(selectIndex-self.numberIndex)); self.link.paused = YES; [self.rotateWheel.layer removeAnimationForKey:@"zhuandong"]; //UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"恭喜你!被点了!!!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; //[alert show]; // if (_delegate && [_delegate respondsToSelector:@selector(LuckyDrawViewDidFinishWidthIndex:)]) { // [_delegate LuckyDrawViewDidFinishWidthIndex:self.numberIndex]; // } self.numberIndex = 1; }); } } /**获取抽奖数据*/ - (void)createAllBtn:(NSMutableArray *)dataArr{ dispatch_async(dispatch_get_main_queue(), ^{ for (int i = 0; i