iOS怎樣實現(xiàn)新年抽獎轉(zhuǎn)盤效果

這篇文章給大家分享的是有關(guān)iOS怎樣實現(xiàn)新年抽獎轉(zhuǎn)盤效果的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

站在用戶的角度思考問題,與客戶深入溝通,找到柳南網(wǎng)站設(shè)計與柳南網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站制作、成都網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、申請域名、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋柳南地區(qū)。

實現(xiàn)步驟:

一、跑馬燈效果

其實很簡單,就是通過兩張圖片,用NSTimer無限替換,達到跑馬燈的效果

實現(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"]]; }}

二、抽獎效果

1.初始化獎品數(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]; }

2.點擊之后開始抽獎按鈕后,先快速地將選中框 正時針轉(zhuǎn)三圈,再慢速地在 一圈之內(nèi)旋轉(zhuǎn)至中獎位置,請 注意是按照 正時針的順序旋轉(zhuǎn),和UI布局的順序不一致

- (void)getLotteryInfo{ // 快速旋轉(zhuǎn)計數(shù),在NSTimer的方法下自增到29時結(jié)束,代表選中框快速旋轉(zhuǎn)了三圈,結(jié)束快速旋轉(zhuǎn) _fastIndex = 0; // 慢速旋轉(zhuǎn)計數(shù),在NSTimer的方法下自增到下面 _selectedIndex 的數(shù)字時,選中框到達中獎位置,結(jié)束慢速旋轉(zhuǎn) _slowIndex = -1; // 中獎位置計數(shù),按照順時針的順序,如上圖所示,若 _selectedIndex = 9 則獲得 9 所在位置的獎品 _selectedIndex = arc4random()%10; // 根據(jù)獎品數(shù)組,獲取中獎信息 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),時間間隔為0.1秒 _fastTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(fastTimerEvent) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:_fastTimer forMode:NSRunLoopCommonModes];}

3.NSTimer 快速旋轉(zhuǎn)事件

- (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, -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 時選中框結(jié)束快速旋轉(zhuǎn),開啟慢速旋轉(zhuǎn),時間間隔為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 慢速旋轉(zhuǎn)事件

// 慢速移動動畫- (void)slowTimerEvent{ // _slowIndex 自增 _slowIndex = _slowIndex + 1; // 順時針移動轉(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)]; } // 當 _slowIndex >= _selectedIndex 時選中框結(jié)束慢速旋轉(zhuǎn),開啟中獎獎品界面 if (_slowIndex >= _selectedIndex) { [_slowTimer invalidate]; dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5/*延遲執(zhí)行時間*/ * NSEC_PER_SEC)); dispatch_after(delayTime, dispatch_get_main_queue(), ^{  self.startButton.userInteractionEnabled = YES;  [self showLotteryRlesultView]; }); }}

感謝各位的閱讀!關(guān)于“iOS怎樣實現(xiàn)新年抽獎轉(zhuǎn)盤效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

本文名稱:iOS怎樣實現(xiàn)新年抽獎轉(zhuǎn)盤效果
網(wǎng)站地址:http://www.muchs.cn/article24/ghjgje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、Google搜索引擎優(yōu)化、網(wǎng)站策劃用戶體驗、網(wǎng)站設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站