iOS 抽獎輪盤效果實現思路

邏輯教育-楚陽發表於2019-02-15

臨近活動,相信不少app都會加一個新的需求——抽獎
不多廢話,先上GIF效果圖

作為一個開發者,有一個學習的氛圍跟一個交流圈子特別重要這是一個我的iOS交流群:937194184,不管你是小白還是大牛歡迎入駐 ,分享面試經驗,討論技術, 大家一起交流學習成長!

  1. 跑馬燈效果
跑馬燈效果
跑馬燈效果
  1. 抽獎效果
15591283-cb0a8bc1c83c1e9d.gif
15591283-cb0a8bc1c83c1e9d.gif

實現步驟:

一、跑馬燈效果


image.png
image.png
image.png
image.png

其實很簡單,就是通過以下兩張圖片,用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"]];
    }
}
複製程式碼

二、抽獎效果

初始化獎品陣列,以及按照 從上到下,從左到右 的順序佈局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.點選之後開始抽獎按鈕後,先快速地將選中框 正時針 轉三圈,再慢速地在 一圈之內 旋轉至中獎位置,請 注意 是按照 正時針 的順序旋轉,和UI佈局的順序不一致,如圖所示:
[圖片上傳中...(image.png-7c3fa7-1550233962701-0)]

- (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, -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 時選中框結束快速旋轉,開啟慢速旋轉,時間間隔為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, -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 時選中框結束慢速旋轉,開啟中獎獎品介面
    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交流群:937194184,不管你是小白還是大牛歡迎入駐 ,分享面試經驗,討論技術, 大家一起交流學習成長!


相關文章