LuckyDrawView.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. //
  2. // LuckyDrawView.m
  3. // 大转盘
  4. //
  5. // Created by 谭启宏 on 16/3/2.
  6. // Copyright © 2016年 tqh. All rights reserved.
  7. //
  8. #import "LuckyDrawView.h"
  9. //抽奖大转盘UI分为3部分:中心按钮点击,转盘,转盘内容
  10. //需要可以自定义转盘内容的数量,内容,
  11. //指针
  12. //
  13. @interface LuckyDrawView ()<UIAlertViewDelegate,CAAnimationDelegate>
  14. @property (nonatomic,strong)UIButton *jiantouBtn; //抽奖按钮
  15. @property (nonatomic,strong)UIButton *playButton; //抽奖按钮
  16. @property (nonatomic,strong)UIImageView *rotateWheel; //转盘背景
  17. @property (nonatomic,strong)CADisplayLink *link;
  18. //@property (nonatomic,assign)NSInteger number;
  19. @property (nonatomic,strong) NSMutableArray *dataArr;
  20. @property (nonatomic,assign) int startValue;
  21. @property (nonatomic,assign) int startAng;
  22. @property (nonatomic,assign) int addNumber;
  23. @property (nonatomic,weak) CABasicAnimation* animation;
  24. @end
  25. @implementation LuckyDrawView
  26. - (instancetype)initWithFrame:(CGRect)frame
  27. {
  28. self = [super initWithFrame:frame];
  29. if (self) {
  30. _dataArr=[NSMutableArray array];
  31. _startValue=0;
  32. _addNumber=2;
  33. [self commit:@[].mutableCopy];
  34. [self statGetStartShop];
  35. }
  36. return self;
  37. }
  38. - (void)commit:(NSMutableArray *)dataArr {
  39. self.numberIndex = 1;
  40. //分隔线
  41. //self.layer.borderWidth = 1;
  42. self.rotateWheel = [[UIImageView alloc]initWithFrame:self.bounds];
  43. self.rotateWheel.image = [UIImage imageNamed:@"zhuanpanback"];
  44. [self addSubview:self.rotateWheel];
  45. self.jiantouBtn = [[UIButton alloc]init];
  46. [self.jiantouBtn setImage:[UIImage imageNamed:@"nodeWheel"] forState:UIControlStateNormal];
  47. [self addSubview:self.jiantouBtn];
  48. [self.jiantouBtn addTarget:self action:@selector(playButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
  49. [self.jiantouBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  50. make.centerX.mas_equalTo(self.rotateWheel.mas_centerX);
  51. make.centerY.mas_equalTo(self.rotateWheel.mas_centerY);
  52. //make.width.mas_equalTo(113);
  53. //make.height.mas_equalTo(124);
  54. }];
  55. /*
  56. UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, M_PI * CGRectGetHeight(self.bounds)/12, CGRectGetHeight(self.bounds)/2)];
  57. //imageview.backgroundColor=[UIColor greenColor];
  58. imageview.layer.anchorPoint = CGPointMake(0.5, 1);
  59. imageview.alpha = 0.5;
  60. imageview.image = [UIImage imageNamed:@"LuckyRototeSelected"];
  61. imageview.contentMode = UIViewContentModeScaleAspectFill;
  62. imageview.center = CGPointMake(CGRectGetHeight(self.bounds)/2, CGRectGetHeight(self.bounds)/2);
  63. [self addSubview:imageview]; //这个不旋转
  64. //NSArray *images=@[@"wheelcoin1",@"wheelcoin2",@"wheelcoin3",@"wheelcoin4",@"wheelcoin5",@"wheelcoin6",@"wheelcoin5",@"wheelcoin6"];
  65. */
  66. //NSArray *images=@[@"wheelcoin1",@"wheelcoin2",@"wheelcoin3",@"wheelcoin4",@"wheelcoin5",@"wheelcoin6",@"wheelcoin5",@"wheelcoin6"];
  67. [self startRotate];
  68. }
  69. - (void)statGetStartShop{
  70. [SVProgressHUD show];
  71. long time = (long)[[NSDate date] timeIntervalSince1970];
  72. time = (time *1000 + g_server.timeDifference);
  73. NSString *salt = [NSString stringWithFormat:@"%ld", time];
  74. [g_server getUseract_act_getTurntable:salt andToView:self];
  75. }
  76. #pragma mark - 事件监听
  77. - (void)choujiangJiekou{
  78. long time = (long)[[NSDate date] timeIntervalSince1970];
  79. time = (time *1000 + g_server.timeDifference);
  80. NSString *salt = [NSString stringWithFormat:@"%ld", time];
  81. //卷盘抽奖
  82. [g_server getUseract_turntableDo:salt andToView:self];
  83. }
  84. - (void)playButtonPressed:(UIButton *)sender {
  85. sender.userInteractionEnabled=NO;
  86. // [self choujiangJiekou];
  87. //度数*返回来的数
  88. //M_PI*2/12*(13-12) 12
  89. //M_PI*2/12*2 11
  90. //M_PI*2/12*3 10
  91. //M_PI*2/12*4 9
  92. int selectIndex = (int)(1 + (arc4random() % (7)));
  93. NSLog(@"抽奖结果 %d",selectIndex);
  94. // if (![self.rotateWheel.layer animationForKey:@"zhuandong"]) {
  95. if (_addNumber<=0) {
  96. self.link.paused = YES;
  97. self.playButton.userInteractionEnabled=YES;
  98. // [self.rotateWheel.layer removeAnimationForKey:@"zhuandong"];
  99. self.rotateWheel.transform = CGAffineTransformMakeRotation(M_PI*2/8*(13-selectIndex));
  100. self.link.paused = YES;
  101. [self.rotateWheel.layer removeAnimationForKey:@"zhuandong"];
  102. self.playButton.userInteractionEnabled=YES;
  103. }else{
  104. [self performSelector:@selector(playButtonPressed:) withObject:sender afterDelay:_addNumber];
  105. }
  106. CABasicAnimation* animation = [[CABasicAnimation alloc] init];
  107. animation.keyPath = @"transform.rotation";
  108. animation.toValue = @(2 * M_PI * 5 - M_PI*2/8*(13-selectIndex));
  109. animation.duration = 5;
  110. animation.removedOnCompletion = NO;
  111. animation.fillMode = kCAFillModeForwards;
  112. animation.speed=_addNumber;
  113. [self.rotateWheel.layer addAnimation:animation forKey:@"zhuandong"];
  114. _addNumber-=0.05;
  115. return;
  116. // }
  117. }
  118. - (void)Rotate2 {
  119. [self playButtonPressed:self.playButton];
  120. }
  121. // 开始转动(一直不停的转动)
  122. - (void)startRotate
  123. {
  124. CADisplayLink* link = [CADisplayLink displayLinkWithTarget:self selector:@selector(Rotate)];
  125. [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
  126. self.link = link;
  127. }
  128. //
  129. - (void)Rotate {
  130. _startAng-=0.3;
  131. self.animation.speed=_startAng;
  132. //每次旋转6°
  133. self.rotateWheel.transform = CGAffineTransformRotate(self.rotateWheel.transform, M_PI * 2 / 8/ 60 );
  134. }
  135. - (void)repetWithAnimation:(BOOL)isYes {
  136. [UIView animateWithDuration:1 animations:^{
  137. self.rotateWheel.transform = CGAffineTransformMakeRotation(M_PI*2/8*(13-self.numberIndex));
  138. self.link.paused = YES;
  139. } completion:^(BOOL finished) {
  140. if (isYes) {
  141. [self repetWithAnimation:YES];
  142. }
  143. // UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"恭喜你!被骗了!!!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  144. // [alert show];
  145. if (_delegate && [_delegate respondsToSelector:@selector(LuckyDrawViewDidFinishWidthIndex:)]) {
  146. //[_delegate LuckyDrawViewDidFinishWidthIndex:self.numberIndex];
  147. }
  148. self.numberIndex = 1;
  149. }];
  150. }
  151. #pragma mark - UIAlertViewDelegate
  152. - (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  153. {
  154. self.link.paused = NO;
  155. }
  156. /**
  157. * 返回数据结果
  158. */
  159. -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  160. [SVProgressHUD dismiss];
  161. if([aDownload.action isEqualToString:act_getTurntable])//获取展示奖品
  162. {
  163. NSMutableArray *jpDataArr=[awarJpModel mj_objectArrayWithKeyValuesArray:array1];
  164. [self createAllBtn:jpDataArr];
  165. }
  166. if([aDownload.action isEqualToString:act_turntableDo])//获取展示奖品
  167. {
  168. int aaaaaa=[dict[@"rewardCode"] intValue];
  169. [self choujiangResult:aaaaaa];
  170. }
  171. }
  172. /*
  173. float quadraticEaseInOut(float t)
  174. {
  175. return (t < 0.5)? (2 * t * t): (-2 * t * t) + (4 * t) - 1;
  176. }
  177. float bounceEaseOut(float t)
  178. {
  179. if (t < 4/11.0) {
  180. return (121 * t * t)/16.0;
  181. } else if (t < 8/11.0) {
  182. return (363/40.0 * t * t) - (99/10.0 * t) + 17/5.0;
  183. } else if (t < 9/10.0) {
  184. return (4356/361.0 * t * t) - (35442/1805.0 * t) + 16061/1805.0;
  185. }
  186. return (54/5.0 * t * t) - (513/25.0 * t) + 268/25.0;
  187. }
  188. float interpolate(float from, float to, float time)
  189. {
  190. return (to - from) * time + from;
  191. }
  192. - (id)interpolateFromValue:(id)fromValue toValue:(id)toValue time:(float)time
  193. {
  194. if ([fromValue isKindOfClass:[NSValue class]]) {
  195. //get type
  196. const char *type = [fromValue objCType];
  197. if (strcmp(type, @encode(CGPoint)) == 0) {
  198. CGPoint from = [fromValue CGPointValue];
  199. CGPoint to = [toValue CGPointValue];
  200. CGPoint result = CGPointMake(interpolate(from.x, to.x, time), interpolate(from.y, to.y, time));
  201. return [NSValue valueWithCGPoint:result];
  202. }
  203. }
  204. //provide safe default implementation
  205. return (time < 0.5)? fromValue: toValue;
  206. }
  207. - (void)animate
  208. {
  209. //reset ball to top of screen
  210. self.rotateWheel.center = CGPointMake(150, 32);
  211. //set up animation parameters
  212. NSValue *fromValue = [NSValue valueWithCGPoint:CGPointMake(150, 32)];
  213. NSValue *toValue = [NSValue valueWithCGPoint:CGPointMake(150, 268)];
  214. CFTimeInterval duration = 1.0;
  215. //generate keyframes
  216. NSInteger numFrames = duration * 60;
  217. NSMutableArray *frames = [NSMutableArray array];
  218. for (int i = 0; i < numFrames; i++) {
  219. float time = 1/(float)numFrames * i;
  220. //apply easing
  221. time = bounceEaseOut(time);
  222. //add keyframe
  223. [frames addObject:[self interpolateFromValue:fromValue toValue:toValue time:time]];
  224. }
  225. //create keyframe animation
  226. CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
  227. animation.keyPath = @"position";
  228. animation.duration = 1.0;
  229. animation.delegate = self;
  230. animation.values = frames;
  231. //apply animation
  232. [self.rotateWheel.layer addAnimation:animation forKey:nil];
  233. }
  234. */
  235. - (void)choujiangResult:(int )selectAAaa{
  236. int selectIndex = (int)(selectAAaa + (arc4random() % (12)));
  237. selectIndex = selectAAaa;
  238. NSLog(@"抽奖结果 %d",selectIndex);
  239. if (![self.rotateWheel.layer animationForKey:@"zhuandong"]) {
  240. CABasicAnimation* animation = [[CABasicAnimation alloc] init];
  241. animation.keyPath = @"transform.rotation";
  242. // animation.toValue = @(2 * M_PI * 5 - M_PI*2/8*(selectIndex-self.numberIndex));
  243. double endValue = _startValue+(2 * M_PI * 5 - M_PI*2/8*(selectIndex));
  244. if (selectAAaa%2==0) {
  245. selectAAaa-=2;
  246. }
  247. if (selectIndex==1) {
  248. endValue=_startValue + (selectAAaa* M_PI/4)+2*M_PI+135;
  249. } if (selectIndex==5) {
  250. endValue=_startValue + (selectAAaa* M_PI/4)+2*M_PI-135;
  251. }
  252. //这里的endValue单位是弧度,2PI是一圈
  253. animation.fromValue = @(_startValue);
  254. animation.toValue = @(endValue);
  255. //旋转时长
  256. //animation.duration = (endValue - _startValue)/(M_PI*2);
  257. //animation.autoreverses = NO;
  258. animation.timeOffset = 0.0;
  259. animation.beginTime = 0.0;
  260. animation.speed=10;
  261. animation.duration = 2;
  262. [self.rotateWheel.layer addAnimation:animation forKey:@"zhuandong"];
  263. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(animation.duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  264. self.jiantouBtn.userInteractionEnabled=YES;
  265. self.rotateWheel.transform = CGAffineTransformMakeRotation(M_PI*2/8*(selectIndex-self.numberIndex));
  266. self.link.paused = YES;
  267. [self.rotateWheel.layer removeAnimationForKey:@"zhuandong"];
  268. //UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"恭喜你!被点了!!!" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
  269. //[alert show];
  270. // if (_delegate && [_delegate respondsToSelector:@selector(LuckyDrawViewDidFinishWidthIndex:)]) {
  271. // [_delegate LuckyDrawViewDidFinishWidthIndex:self.numberIndex];
  272. // }
  273. self.numberIndex = 1;
  274. });
  275. }
  276. }
  277. /**获取抽奖数据*/
  278. - (void)createAllBtn:(NSMutableArray *)dataArr{
  279. dispatch_async(dispatch_get_main_queue(), ^{
  280. for (int i = 0; i <dataArr.count; i ++) {
  281. awarJpModel *model=dataArr[i];
  282. UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,M_PI * CGRectGetHeight(self.bounds)/8,
  283. CGRectGetHeight(self.bounds)/2)];
  284. label.layer.anchorPoint = CGPointMake(0.5, 1);
  285. label.center = CGPointMake(CGRectGetHeight(self.bounds)/2, CGRectGetHeight(self.bounds)/2);
  286. label.text = [NSString stringWithFormat:@"%@",model.rewardNum];
  287. CGFloat angle = M_PI * 2 / 8 * i;
  288. label.textAlignment = NSTextAlignmentCenter;
  289. label.font = [UIFont systemFontOfSize:12];
  290. label.transform = CGAffineTransformMakeRotation(angle);
  291. [self.rotateWheel addSubview:label];
  292. UILabel *labelText = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, M_PI * CGRectGetHeight(self.bounds)/8 - 20, 40)];
  293. labelText.text = model.rewardName;
  294. labelText.font = [UIFont systemFontOfSize:10];
  295. labelText.numberOfLines = 2;
  296. labelText.textAlignment = NSTextAlignmentCenter;
  297. [label addSubview:labelText];
  298. }
  299. });
  300. }
  301. -(int) didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{
  302. [SVProgressHUD dismiss];
  303. NSString * errorCode = [NSString stringWithFormat:@"%@",[dict objectForKey:@"resultMsg"]];
  304. if([errorCode isEqualToString:@"权限验证失败"])
  305. {
  306. }
  307. self.jiantouBtn.userInteractionEnabled=YES;
  308. return 1;
  309. }
  310. @end