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

iOS实现新年抽奖转盘效果的思路,第1张

概述iOS实现新年抽奖转盘效果思路 临近春节,相信不少app都会加一个新的需求--新年抽奖 不多废话,先上GIF效果图 DEMO链接 1. 跑马灯效果 2. 抽奖效果 实现步骤: 一.跑马灯效果 其实很简单,就是通过以下两张图片,用NSTimer无限替换,达到跑马灯的效果 实现代码: _rotaryTable = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth-366*XT)/2, 218*XT, 366*XT, 318*XT)]; _rotaryTable.

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

不多废话,先上GIF效果图

DEMO链接

1. 跑马灯效果


2. 抽奖效果


实现步骤:

一、跑马灯效果

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



实现代码:

_rotarytable = [[UIImageVIEw alloc] initWithFrame:CGRectMake((kScreenWIDth-366*XT)/2,218*XT,366*XT,318*XT)];_rotarytable.tag = 100;[_rotarytable setimage:[UIImage imagenamed:@"bg_lamp_1"]];[scrollVIEw addSubvIEw:_rotarytable];_itemBordeTImer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(itemBordeTImerEvent) userInfo:nil repeats:YES];[[NSRunLoop currentRunLoop] addTimer:_itemBordeTImer forMode:NSRunLoopCommonModes];
- (voID)itemBordeTImerEvent{ if (_rotarytable.tag == 100) { _rotarytable.tag = 101; [_rotarytable setimage:[UIImage imagenamed:@"bg_lamp_2"]]; }else if (_rotarytable.tag == 101){ _rotarytable.tag = 100; [_rotarytable setimage:[UIImage imagenamed:@"bg_lamp_1"]]; }}

二、抽奖效果

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

_itemTitleArray = @[@"3跳币",@"嘉年华门票",@"8跳币",@"10朵花",@"128朵花",@"2018跳币",@"528跳币",@"128跳币",@"28朵花",@"88跳币"];for (int i = 0 ; i < 4; i ++) { UIImageVIEw *img = [[UIImageVIEw alloc] initWithFrame:CGRectMake(i*82*XT,78*XT,80*XT)]; [img setimage:[UIImage imagenamed:itemimgArray[i]]]; [itemVIEw addSubvIEw:img]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,63*XT,13*XT)]; label.textAlignment = NSTextAlignmentCenter; label.textcolor = [UIcolor whitecolor]; label.Font = [UIFont systemFontOfSize:13*XT]; label.text = _itemTitleArray[I]; [img addSubvIEw:label]; } for (int i = 0 ; i < 2; i ++) { UIImageVIEw *img = [[UIImageVIEw alloc] initWithFrame:CGRectMake(i*(78*XT+169*XT),84*XT,80*XT)]; [img setimage:[UIImage imagenamed:itemimgArray[i+4]]]; [itemVIEw addSubvIEw:img]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,13*XT)]; label.textAlignment = NSTextAlignmentCenter; label.textcolor = [UIcolor whitecolor]; label.Font = [UIFont systemFontOfSize:13*XT]; label.text = _itemTitleArray[i+4]; [img addSubvIEw:label]; } for (int i = 0 ; i < 4; i ++) { UIImageVIEw *img = [[UIImageVIEw alloc] initWithFrame:CGRectMake(i*82*XT,168*XT,80*XT)]; [img setimage:[UIImage imagenamed:itemimgArray[i+6]]]; [itemVIEw addSubvIEw:img]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,13*XT)]; label.textAlignment = NSTextAlignmentCenter; label.textcolor = [UIcolor whitecolor]; label.Font = [UIFont systemFontOfSize:13*XT]; label.text = _itemTitleArray[i+6]; [img addSubvIEw:label]; }

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


- (voID)getLotteryInfo{ // 快速旋转计数,在NSTimer的方法下自增到29时结束,代表选中框快速旋转了三圈,结束快速旋转 _fastIndex = 0; // 慢速旋转计数,在NSTimer的方法下自增到下面 _selectedindex 的数字时,选中框到达中奖位置,结束慢速旋转 _slowIndex = -1; // 中奖位置计数,按照顺时针的顺序,如上图所示,若 _selectedindex = 9 则获得 9 所在位置的奖品 _selectedindex = arc4random()%10; // 根据奖品数组,获取中奖信息 if (_selectedindex<4) { _result = _itemTitleArray[_selectedindex]; }else if (_selectedindex == 4){ _result = @"2018跳币"; }else if (_selectedindex == 5){ _result = @"88跳币"; }else if (_selectedindex == 6){ _result = @"28朵花"; }else if (_selectedindex == 7){ _result = @"128跳币"; }else if (_selectedindex == 8){ _result = @"528跳币"; }else if (_selectedindex == 9){ _result = @"128朵花"; } _itemborderVIEw.hIDden = NO; // 开启快速旋转,时间间隔为0.1秒 _fastTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(fastTimerEvent) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:_fastTimer forMode:NSRunLoopCommonModes];}

3.NSTimer 快速旋转事件

- (voID)fastTimerEvent{ // _fastIndex 自增 _fastIndex = _fastIndex + 1; // 顺时针移动选中框的位置 if (_fastIndex % 10 == 0) { [_itemborderVIEw setFrame:CGRectMake(-1*XT,-1*XT,80*XT,82*XT)]; }else if (_fastIndex % 10 == 1){ [_itemborderVIEw setFrame:CGRectMake(82*XT-1*XT,82*XT)]; }else if (_fastIndex % 10 == 2){ [_itemborderVIEw setFrame:CGRectMake(2*82*XT-1*XT,82*XT)]; }else if (_fastIndex % 10 == 3){ [_itemborderVIEw setFrame:CGRectMake(3*82*XT-1*XT,82*XT)]; }else if (_fastIndex % 10 == 4){ [_itemborderVIEw setFrame:CGRectMake(3*82*XT-1*XT,84*XT-1*XT,82*XT)]; }else if (_fastIndex % 10 == 5){ [_itemborderVIEw setFrame:CGRectMake(3*82*XT-1*XT,2*84*XT-1*XT,82*XT)]; }else if (_fastIndex % 10 == 6){ [_itemborderVIEw setFrame:CGRectMake(2*82*XT-1*XT,82*XT)]; }else if (_fastIndex % 10 == 7){ [_itemborderVIEw setFrame:CGRectMake(82*XT-1*XT,82*XT)]; }else if (_fastIndex % 10 == 8){ [_itemborderVIEw setFrame:CGRectMake(-1*XT,82*XT)]; }else if (_fastIndex % 10 == 9){ [_itemborderVIEw setFrame:CGRectMake(-1*XT,82*XT)]; } // _fastIndex = 29 时选中框结束快速旋转,开启慢速旋转,时间间隔为0.45秒 if (_fastIndex >= 29) { [_fastTimer invalIDate]; _slowTimer = [NSTimer scheduledTimerWithTimeInterval:0.45 target:self selector:@selector(slowTimerEvent) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:_slowTimer forMode:NSRunLoopCommonModes]; }}

4.NSTimer 慢速旋转事件

// 慢速移动动画- (voID)slowTimerEvent{ // _slowIndex 自增 _slowIndex = _slowIndex + 1; // 顺时针移动转中框的位置 if (_slowIndex % 10 == 0) { [_itemborderVIEw setFrame:CGRectMake(-1*XT,82*XT)]; }else if (_slowIndex % 10 == 1){ [_itemborderVIEw setFrame:CGRectMake(82*XT-1*XT,82*XT)]; }else if (_slowIndex % 10 == 2){ [_itemborderVIEw setFrame:CGRectMake(2*82*XT-1*XT,82*XT)]; }else if (_slowIndex % 10 == 3){ [_itemborderVIEw setFrame:CGRectMake(3*82*XT-1*XT,82*XT)]; }else if (_slowIndex % 10 == 4){ [_itemborderVIEw setFrame:CGRectMake(3*82*XT-1*XT,82*XT)]; }else if (_slowIndex % 10 == 5){ [_itemborderVIEw setFrame:CGRectMake(3*82*XT-1*XT,82*XT)]; }else if (_slowIndex % 10 == 6){ [_itemborderVIEw setFrame:CGRectMake(2*82*XT-1*XT,82*XT)]; }else if (_slowIndex % 10 == 7){ [_itemborderVIEw setFrame:CGRectMake(82*XT-1*XT,82*XT)]; }else if (_slowIndex % 10 == 8){ [_itemborderVIEw setFrame:CGRectMake(-1*XT,82*XT)]; }else if (_slowIndex % 10 == 9){ [_itemborderVIEw setFrame:CGRectMake(-1*XT,82*XT)]; } // 当 _slowIndex >= _selectedindex 时选中框结束慢速旋转,开启中奖奖品界面 if (_slowIndex >= _selectedindex) { [_slowTimer invalIDate]; dispatch_time_t delayTime = dispatch_time(disPATCH_TIME_Now,(int64_t)(1.5/*延迟执行时间*/ * NSEC_PER_SEC)); dispatch_after(delayTime,dispatch_get_main_queue(),^{  self.startbutton.userInteractionEnabled = YES;  [self showLotteryRlesultVIEw]; }); }}

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

总结

以上是内存溢出为你收集整理的iOS实现新年抽奖转盘效果的思路全部内容,希望文章能够帮你解决iOS实现新年抽奖转盘效果的思路所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1109950.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存