iOS 抽獎(jiǎng)輪盤效果實(shí)現(xiàn)思路

臨近春節(jié)酪刀,相信不少app都會(huì)加一個(gè)新的需求——新年抽獎(jiǎng)

不多廢話梦谜,先上GIF效果圖

DEMO鏈接:https://gitee.com/xudongxiang/LuckyDraw.git

1. 跑馬燈效果
QQ20190117-153231.gif
2. 抽獎(jiǎng)效果
QQ20190117-153345.gif

實(shí)現(xiàn)步驟:

一、跑馬燈效果

其實(shí)很簡(jiǎn)單窄做,就是通過以下兩張圖片毁兆,用NSTimer無(wú)限替換河泳,達(dá)到跑馬燈的效果


bg_lamp_1@2x.png

bg_lamp_2@2x.png

實(shí)現(xiàn)代碼:

_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"]];
    }
}
二、抽獎(jiǎng)效果
  1. 初始化獎(jiǎng)品數(shù)組断序,以及按照 從上到下流纹,從左到右 的順序布局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, 0, 78*XT, 80*XT)];
        [img setImage:[UIImage imageNamed:itemImgArray[i]]];
        [itemView addSubview:img];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 63*XT, 78*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, 78*XT, 80*XT)];
        [img setImage:[UIImage imageNamed:itemImgArray[i+4]]];
        [itemView addSubview:img];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 63*XT, 78*XT, 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, 78*XT, 80*XT)];
        [img setImage:[UIImage imageNamed:itemImgArray[i+6]]];
        [itemView addSubview:img];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 63*XT, 78*XT, 13*XT)];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:13*XT];
        label.text = _itemTitleArray[i+6];
        [img addSubview:label];
    }
  1. 點(diǎn)擊之后開始抽獎(jiǎng)按鈕后,先快速地將選中框 正時(shí)針 轉(zhuǎn)三圈违诗,再慢速地在 一圈之內(nèi) 旋轉(zhuǎn)至中獎(jiǎng)位置漱凝,請(qǐng) 注意 是按照 正時(shí)針 的順序旋轉(zhuǎn),和UI布局的順序不一致诸迟,如圖所示:
bg_lamp_2@2x.png
- (void)getLotteryInfo
{
    // 快速旋轉(zhuǎn)計(jì)數(shù)茸炒,在NSTimer的方法下自增到29時(shí)結(jié)束,代表選中框快速旋轉(zhuǎn)了三圈阵苇,結(jié)束快速旋轉(zhuǎn)
    _fastIndex = 0;

    // 慢速旋轉(zhuǎn)計(jì)數(shù)壁公,在NSTimer的方法下自增到下面 _selectedIndex 的數(shù)字時(shí),選中框到達(dá)中獎(jiǎng)位置绅项,結(jié)束慢速旋轉(zhuǎn)
    _slowIndex = -1;

    // 中獎(jiǎng)位置計(jì)數(shù)紊册,按照順時(shí)針的順序,如上圖所示快耿,若 _selectedIndex = 9 則獲得 9 所在位置的獎(jiǎng)品
    _selectedIndex = arc4random()%10;
  
    // 根據(jù)獎(jiǎng)品數(shù)組囊陡,獲取中獎(jiǎng)信息
    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;

    // 開啟快速旋轉(zhuǎn),時(shí)間間隔為0.1秒
    _fastTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(fastTimerEvent) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_fastTimer forMode:NSRunLoopCommonModes];
}
  1. NSTimer 快速旋轉(zhuǎn)事件
- (void)fastTimerEvent
{
    // _fastIndex 自增
    _fastIndex = _fastIndex + 1;

    // 順時(shí)針移動(dòng)選中框的位置
    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, -1*XT, 80*XT, 82*XT)];
    }else if (_fastIndex % 10 == 2){
        [_itemBorderView setFrame:CGRectMake(2*82*XT-1*XT, -1*XT, 80*XT, 82*XT)];
    }else if (_fastIndex % 10 == 3){
        [_itemBorderView setFrame:CGRectMake(3*82*XT-1*XT, -1*XT, 80*XT, 82*XT)];
    }else if (_fastIndex % 10 == 4){
        [_itemBorderView setFrame:CGRectMake(3*82*XT-1*XT, 84*XT-1*XT, 80*XT, 82*XT)];
    }else if (_fastIndex % 10 == 5){
        [_itemBorderView setFrame:CGRectMake(3*82*XT-1*XT, 2*84*XT-1*XT, 80*XT, 82*XT)];
    }else if (_fastIndex % 10 == 6){
        [_itemBorderView setFrame:CGRectMake(2*82*XT-1*XT, 2*84*XT-1*XT, 80*XT, 82*XT)];
    }else if (_fastIndex % 10 == 7){
        [_itemBorderView setFrame:CGRectMake(82*XT-1*XT, 2*84*XT-1*XT, 80*XT, 82*XT)];
    }else if (_fastIndex % 10 == 8){
        [_itemBorderView setFrame:CGRectMake(-1*XT, 2*84*XT-1*XT, 80*XT, 82*XT)];
    }else if (_fastIndex % 10 == 9){
        [_itemBorderView setFrame:CGRectMake(-1*XT, 84*XT-1*XT, 80*XT, 82*XT)];
    }

    // _fastIndex = 29 時(shí)選中框結(jié)束快速旋轉(zhuǎn)润努,開啟慢速旋轉(zhuǎn)关斜,時(shí)間間隔為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];
    }
}
  1. NSTimer 慢速旋轉(zhuǎn)事件
// 慢速移動(dòng)動(dòng)畫
- (void)slowTimerEvent
{
    // _slowIndex 自增
    _slowIndex = _slowIndex + 1;

    // 順時(shí)針移動(dòng)轉(zhuǎn)中框的位置
    if (_slowIndex % 10 == 0) {
        [_itemBorderView setFrame:CGRectMake(-1*XT, -1*XT, 80*XT, 82*XT)];
    }else if (_slowIndex % 10 == 1){
        [_itemBorderView setFrame:CGRectMake(82*XT-1*XT, -1*XT, 80*XT, 82*XT)];
    }else if (_slowIndex % 10 == 2){
        [_itemBorderView setFrame:CGRectMake(2*82*XT-1*XT, -1*XT, 80*XT, 82*XT)];
    }else if (_slowIndex % 10 == 3){
        [_itemBorderView setFrame:CGRectMake(3*82*XT-1*XT, -1*XT, 80*XT, 82*XT)];
    }else if (_slowIndex % 10 == 4){
        [_itemBorderView setFrame:CGRectMake(3*82*XT-1*XT, 84*XT-1*XT, 80*XT, 82*XT)];
    }else if (_slowIndex % 10 == 5){
        [_itemBorderView setFrame:CGRectMake(3*82*XT-1*XT, 2*84*XT-1*XT, 80*XT, 82*XT)];
    }else if (_slowIndex % 10 == 6){
        [_itemBorderView setFrame:CGRectMake(2*82*XT-1*XT, 2*84*XT-1*XT, 80*XT, 82*XT)];
    }else if (_slowIndex % 10 == 7){
        [_itemBorderView setFrame:CGRectMake(82*XT-1*XT, 2*84*XT-1*XT, 80*XT, 82*XT)];
    }else if (_slowIndex % 10 == 8){
        [_itemBorderView setFrame:CGRectMake(-1*XT, 2*84*XT-1*XT, 80*XT, 82*XT)];
    }else if (_slowIndex % 10 == 9){
        [_itemBorderView setFrame:CGRectMake(-1*XT, 84*XT-1*XT, 80*XT, 82*XT)];
    }

    // 當(dāng)  _slowIndex >= _selectedIndex 時(shí)選中框結(jié)束慢速旋轉(zhuǎn),開啟中獎(jiǎng)獎(jiǎng)品界面
    if (_slowIndex >= _selectedIndex) {
        [_slowTimer invalidate];

        dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5/*延遲執(zhí)行時(shí)間*/ * NSEC_PER_SEC));
        dispatch_after(delayTime, dispatch_get_main_queue(), ^{
            self.startButton.userInteractionEnabled = YES;
            [self showLotteryRlesultView];
        });
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末铺浇,一起剝皮案震驚了整個(gè)濱河市痢畜,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖丁稀,帶你破解...
    沈念sama閱讀 218,858評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件吼拥,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡线衫,警方通過查閱死者的電腦和手機(jī)凿可,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來授账,“玉大人枯跑,你說我怎么就攤上這事“兹龋” “怎么了敛助?”我有些...
    開封第一講書人閱讀 165,282評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)屋确。 經(jīng)常有香客問我纳击,道長(zhǎng),這世上最難降的妖魔是什么攻臀? 我笑而不...
    開封第一講書人閱讀 58,842評(píng)論 1 295
  • 正文 為了忘掉前任焕数,我火速辦了婚禮,結(jié)果婚禮上刨啸,老公的妹妹穿的比我還像新娘堡赔。我一直安慰自己,他們只是感情好呜投,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評(píng)論 6 392
  • 文/花漫 我一把揭開白布加匈。 她就那樣靜靜地躺著,像睡著了一般仑荐。 火紅的嫁衣襯著肌膚如雪雕拼。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,679評(píng)論 1 305
  • 那天粘招,我揣著相機(jī)與錄音啥寇,去河邊找鬼。 笑死洒扎,一個(gè)胖子當(dāng)著我的面吹牛辑甜,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播袍冷,決...
    沈念sama閱讀 40,406評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼磷醋,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了胡诗?” 一聲冷哼從身側(cè)響起邓线,我...
    開封第一講書人閱讀 39,311評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤淌友,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后骇陈,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體震庭,經(jīng)...
    沈念sama閱讀 45,767評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評(píng)論 3 336
  • 正文 我和宋清朗相戀三年你雌,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了器联。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,090評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡婿崭,死狀恐怖拨拓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情氓栈,我是刑警寧澤千元,帶...
    沈念sama閱讀 35,785評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站颤绕,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏祟身。R本人自食惡果不足惜奥务,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望袜硫。 院中可真熱鬧氯葬,春花似錦、人聲如沸婉陷。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)秽澳。三九已至闯睹,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間担神,已是汗流浹背楼吃。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評(píng)論 1 271
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留妄讯,地道東北人孩锡。 一個(gè)月前我還...
    沈念sama閱讀 48,298評(píng)論 3 372
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像亥贸,于是被迫代替她去往敵國(guó)和親躬窜。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評(píng)論 2 355