iOS实现新年抽奖转盘效果的思路

前端之家收集整理的这篇文章主要介绍了iOS实现新年抽奖转盘效果的思路前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

临近春节,相信不少app都会加一个新的需求——新年抽奖

不多废话,先上GIF效果

DEMO链接

1. 跑马灯效果

iOS实现新年抽奖转盘效果的思路


2. 抽奖效果

iOS实现新年抽奖转盘效果的思路


实现步骤:

一、跑马灯效果

其实很简单,就是通过以下两张图片,用NSTimer无限替换,达到跑马灯的效果

iOS实现新年抽奖转盘效果的思路


@H_404_36@


实现代码

  1. _rotaryTable = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth-366*XT)/2,218*XT,366*XT,318*XT)];
  2. _rotaryTable.tag = 100;
  3. [_rotaryTable setImage:[UIImage imageNamed:@"bg_lamp_1"]];
  4. [scrollView addSubview:_rotaryTable];
  5.  
  6. _itemBordeTImer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(itemBordeTImerEvent) userInfo:nil repeats:YES];
  7. [[NSRunLoop currentRunLoop] addTimer:_itemBordeTImer forMode:NSRunLoopCommonModes];
  1. - (void)itemBordeTImerEvent
  2. {
  3. if (_rotaryTable.tag == 100) {
  4. _rotaryTable.tag = 101;
  5. [_rotaryTable setImage:[UIImage imageNamed:@"bg_lamp_2"]];
  6. }else if (_rotaryTable.tag == 101){
  7. _rotaryTable.tag = 100;
  8. [_rotaryTable setImage:[UIImage imageNamed:@"bg_lamp_1"]];
  9. }
  10. }

二、抽奖效果

1.初始化奖品数组,以及按照 从上到下,从左到右 的顺序布局UI界面

  1. _itemTitleArray = @[@"3跳币",@"嘉年华门票",@"8跳币",@"10朵花",@"128朵花",@"2018跳币",@"528跳币",@"128跳币",@"28朵花",@"88跳币"];
  2. for (int i = 0 ; i < 4; i ++) {
  3. UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(i*82*XT,78*XT,80*XT)];
  4. [img setImage:[UIImage imageNamed:itemImgArray[i]]];
  5. [itemView addSubview:img];
  6.  
  7. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,63*XT,13*XT)];
  8. label.textAlignment = NSTextAlignmentCenter;
  9. label.textColor = [UIColor whiteColor];
  10. label.font = [UIFont systemFontOfSize:13*XT];
  11. label.text = _itemTitleArray[I];
  12. [img addSubview:label];
  13. }
  14.  
  15. for (int i = 0 ; i < 2; i ++) {
  16. UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(i*(78*XT+169*XT),84*XT,80*XT)];
  17. [img setImage:[UIImage imageNamed:itemImgArray[i+4]]];
  18. [itemView addSubview:img];
  19.  
  20. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,13*XT)];
  21. label.textAlignment = NSTextAlignmentCenter;
  22. label.textColor = [UIColor whiteColor];
  23. label.font = [UIFont systemFontOfSize:13*XT];
  24. label.text = _itemTitleArray[i+4];
  25. [img addSubview:label];
  26. }
  27.  
  28. for (int i = 0 ; i < 4; i ++) {
  29. UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(i*82*XT,168*XT,80*XT)];
  30. [img setImage:[UIImage imageNamed:itemImgArray[i+6]]];
  31. [itemView addSubview:img];
  32.  
  33. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,13*XT)];
  34. label.textAlignment = NSTextAlignmentCenter;
  35. label.textColor = [UIColor whiteColor];
  36. label.font = [UIFont systemFontOfSize:13*XT];
  37. label.text = _itemTitleArray[i+6];
  38. [img addSubview:label];
  39. }

2.点击之后开始抽奖按钮后,先快速地将选中框 正时针 转三圈,再慢速地在 一圈之内 旋转至中奖位置,请 注意 是按照 正时针 的顺序旋转,和UI布局的顺序不一致,如图所示:

iOS实现新年抽奖转盘效果的思路


  1. - (void)getLotteryInfo
  2. {
  3. // 快速旋转计数,在NSTimer的方法下自增到29时结束,代表选中框快速旋转了三圈,结束快速旋转
  4. _fastIndex = 0;
  5.  
  6. // 慢速旋转计数,在NSTimer的方法下自增到下面 _selectedIndex 的数字时,选中框到达中奖位置,结束慢速旋转
  7. _slowIndex = -1;
  8.  
  9. // 中奖位置计数,按照顺时针的顺序,如上图所示,若 _selectedIndex = 9 则获得 9 所在位置的奖品
  10. _selectedIndex = arc4random()%10;
  11.  
  12. // 根据奖品数组,获取中奖信息
  13. if (_selectedIndex<4) {
  14. _result = _itemTitleArray[_selectedIndex];
  15. }else if (_selectedIndex == 4){
  16. _result = @"2018跳币";
  17. }else if (_selectedIndex == 5){
  18. _result = @"88跳币";
  19. }else if (_selectedIndex == 6){
  20. _result = @"28朵花";
  21. }else if (_selectedIndex == 7){
  22. _result = @"128跳币";
  23. }else if (_selectedIndex == 8){
  24. _result = @"528跳币";
  25. }else if (_selectedIndex == 9){
  26. _result = @"128朵花";
  27. }
  28. _itemBorderView.hidden = NO;
  29.  
  30. // 开启快速旋转,时间间隔为0.1秒
  31. _fastTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(fastTimerEvent) userInfo:nil repeats:YES];
  32. [[NSRunLoop currentRunLoop] addTimer:_fastTimer forMode:NSRunLoopCommonModes];
  33. }

3.NSTimer 快速旋转事件

  1. - (void)fastTimerEvent
  2. {
  3. // _fastIndex 自增
  4. _fastIndex = _fastIndex + 1;
  5.  
  6. // 顺时针移动选中框的位置
  7. if (_fastIndex % 10 == 0) {
  8. [_itemBorderView setFrame:CGRectMake(-1*XT,-1*XT,80*XT,82*XT)];
  9. }else if (_fastIndex % 10 == 1){
  10. [_itemBorderView setFrame:CGRectMake(82*XT-1*XT,82*XT)];
  11. }else if (_fastIndex % 10 == 2){
  12. [_itemBorderView setFrame:CGRectMake(2*82*XT-1*XT,82*XT)];
  13. }else if (_fastIndex % 10 == 3){
  14. [_itemBorderView setFrame:CGRectMake(3*82*XT-1*XT,82*XT)];
  15. }else if (_fastIndex % 10 == 4){
  16. [_itemBorderView setFrame:CGRectMake(3*82*XT-1*XT,84*XT-1*XT,82*XT)];
  17. }else if (_fastIndex % 10 == 5){
  18. [_itemBorderView setFrame:CGRectMake(3*82*XT-1*XT,2*84*XT-1*XT,82*XT)];
  19. }else if (_fastIndex % 10 == 6){
  20. [_itemBorderView setFrame:CGRectMake(2*82*XT-1*XT,82*XT)];
  21. }else if (_fastIndex % 10 == 7){
  22. [_itemBorderView setFrame:CGRectMake(82*XT-1*XT,82*XT)];
  23. }else if (_fastIndex % 10 == 8){
  24. [_itemBorderView setFrame:CGRectMake(-1*XT,82*XT)];
  25. }else if (_fastIndex % 10 == 9){
  26. [_itemBorderView setFrame:CGRectMake(-1*XT,82*XT)];
  27. }
  28.  
  29. // _fastIndex = 29 时选中框结束快速旋转,开启慢速旋转,时间间隔为0.45秒
  30. if (_fastIndex >= 29) {
  31. [_fastTimer invalidate];
  32. _slowTimer = [NSTimer scheduledTimerWithTimeInterval:0.45 target:self selector:@selector(slowTimerEvent) userInfo:nil repeats:YES];
  33. [[NSRunLoop currentRunLoop] addTimer:_slowTimer forMode:NSRunLoopCommonModes];
  34. }
  35. }

4.NSTimer 慢速旋转事件

  1. // 慢速移动动画
  2. - (void)slowTimerEvent
  3. {
  4. // _slowIndex 自增
  5. _slowIndex = _slowIndex + 1;
  6.  
  7. // 顺时针移动转中框的位置
  8. if (_slowIndex % 10 == 0) {
  9. [_itemBorderView setFrame:CGRectMake(-1*XT,82*XT)];
  10. }else if (_slowIndex % 10 == 1){
  11. [_itemBorderView setFrame:CGRectMake(82*XT-1*XT,82*XT)];
  12. }else if (_slowIndex % 10 == 2){
  13. [_itemBorderView setFrame:CGRectMake(2*82*XT-1*XT,82*XT)];
  14. }else if (_slowIndex % 10 == 3){
  15. [_itemBorderView setFrame:CGRectMake(3*82*XT-1*XT,82*XT)];
  16. }else if (_slowIndex % 10 == 4){
  17. [_itemBorderView setFrame:CGRectMake(3*82*XT-1*XT,82*XT)];
  18. }else if (_slowIndex % 10 == 5){
  19. [_itemBorderView setFrame:CGRectMake(3*82*XT-1*XT,82*XT)];
  20. }else if (_slowIndex % 10 == 6){
  21. [_itemBorderView setFrame:CGRectMake(2*82*XT-1*XT,82*XT)];
  22. }else if (_slowIndex % 10 == 7){
  23. [_itemBorderView setFrame:CGRectMake(82*XT-1*XT,82*XT)];
  24. }else if (_slowIndex % 10 == 8){
  25. [_itemBorderView setFrame:CGRectMake(-1*XT,82*XT)];
  26. }else if (_slowIndex % 10 == 9){
  27. [_itemBorderView setFrame:CGRectMake(-1*XT,82*XT)];
  28. }
  29.  
  30. // 当 _slowIndex >= _selectedIndex 时选中框结束慢速旋转,开启中奖奖品界面
  31. if (_slowIndex >= _selectedIndex) {
  32. [_slowTimer invalidate];
  33.  
  34. dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW,(int64_t)(1.5/*延迟执行时间*/ * NSEC_PER_SEC));
  35. dispatch_after(delayTime,dispatch_get_main_queue(),^{
  36. self.startButton.userInteractionEnabled = YES;
  37. [self showLotteryRlesultView];
  38. });
  39. }
  40. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

猜你在找的iOS相关文章