集思广益iOS 实用iOS效果原理分析

ios 转盘抽奖demo

2016-10-19  本文已影响2946人  34码的小孩子

规则要求:

想法:

数据存储

[minIndex, maxIndex} 是为中奖区间,他们的值由中奖概率和整个demo的区间长度决定。isProbabilityChage表示中奖概率是否会发生改变,是,概率数组才有存在的价值。因为概率数组probabilityArray存储的是一个model,所以需要在.m文件中,显式表示转换(使用MJExtension)。

/**
 中奖的model
 */
@interface LotteryModel : NSObject
@property (copy, nonatomic) NSString *name;     //奖品名称
@property (assign, nonatomic) NSInteger index;  //奖品的下标,唯一
@property (copy, nonatomic) NSDecimalNumber *probability;   //概率
@property (assign, nonatomic) NSInteger count;         //剩余张数 -1为无穷张
@property (assign, nonatomic) BOOL isProbabilityChage; //概率是否随张数改变。
@property (copy, nonatomic) NSMutableArray *probabilityArray;   //isProbabilityChage = YES 时有用
@property (assign, nonatomic) NSInteger minIndex;           //中奖最小值,包括
@property (assign, nonatomic) NSInteger maxIndex;           //中奖的最大值,不包括
@end

#import "LotteryModel.h"
#import "MJExtension.h"
#import "MaxProModel.h"
@implementation LotteryModel
- (instancetype)init {
self = [super init];

if (self) {
    [LotteryModel mj_setupObjectClassInArray:^NSDictionary *{
        return @{
                 @"probabilityArray" : [MaxProModel class]
                 };
    }];
}

return self;
}
@end

/**
 动态概率的model
 */
@interface MaxProModel : NSObject
@property (assign, nonatomic) NSInteger maxCount;//当张数=maxCount时,中奖概率为maxProbability
@property (copy, nonatomic) NSDecimalNumber *maxProbability;
@end

数据处理

创建数据方式如下(dict基本都是这么创建,只截取部分):

- (void)initData {
_dataArray = [NSMutableArray new];
NSDictionary *dict4 = @{
                        @"name" : @"谢谢惠顾",
                        @"index" : @3,
                        @"probability" : [NSDecimalNumber decimalNumberWithString:@"0.125"],
                        @"count" : @-1,
                        @"isProbabilityChage" : @0,
                        };
NSDictionary *dict5 = @{
                        @"name" : @"499元XX牌电冰箱购买券",
                        @"index" : @4,
                        @"probability" : [NSDecimalNumber decimalNumberWithString:@"0.05"],
                        @"count" : @51,
                        @"isProbabilityChage" : @1,
                        @"probabilityArray" : @[
                                @{
                                    @"maxCount" : @10,
                                    @"maxProbability" : [NSDecimalNumber decimalNumberWithString:@"0.02"]
                                    },
                                @{
                                    @"maxCount" : @20,
                                    @"maxProbability" : [NSDecimalNumber decimalNumberWithString:@"0.025"]
                                    },
                                @{
                                    @"maxCount" : @50,
                                    @"maxProbability" : [NSDecimalNumber decimalNumberWithString:@"0.030"]
                                    }
                                ]
                        };
NSArray *array = @[dict1, dict2, dict3, dict4, dict5, dict6, dict7, dict8];
_dataArray = [LotteryModel mj_objectArrayWithKeyValuesArray:array];

//设置整个demo的区间长度
_lotteryCount = 10000
NSInteger min = 0;

//设置每个奖品中奖区间
for (int i = 0; i < _dataArray.count; i++) {
    LotteryModel *model = _dataArray[i];
    model.minIndex = min;
    model.maxIndex = model.minIndex + [model.probability floatValue] * _lotteryCount;
    min = model.maxIndex;
}
}

用户点击抽奖之后,获得一个随机数,然后判断随机数落入那个奖品的中奖区间内。如果奖品有数量限制的,则需要将奖品数减一,并且要判断是否需要改变该奖品的中奖概率。然后使用动画,让指针指向该奖品对应的扇形区域。

- (IBAction)startLotteryButtonTouch:(UIButton *)sender {
NSInteger index = arc4random() % _lotteryCount;

for (LotteryModel *model in _dataArray) {
    if (index >= model.minIndex && index < model.maxIndex) {
        _selectedModel = model;
        
        if (![model.name isEqualToString:@"谢谢惠顾"]) {
            NSLog(@"恭喜,您抽中了%@,快点去购买商品吧", model.name);
            
            if (model.count != -1 && model.count > 1) {
                model.count -= 1;
            }
            
            //修改中奖概率
            [self changeProbabilityWithModel:model];
            //显示动画
            [self showAnimationWithSelectedModel:model];
            break;
        }
        else {
            NSLog(@"%@", model.name);
            
            //显示动画
            [self showAnimationWithSelectedModel:model];
            break;
        }
    }
}
}

对于可以修改概率的奖品,根据概率数组对应的概率修改,并修改该奖品的概率区间。

- (void)changeProbabilityWithModel:(LotteryModel *)model {
//奖品已经没有了,需要将奖品的中奖区间改为0
if (model.count == 0) {
    //修改其他的中奖区间
    [self changeRandomWithIndex:model.index changeCount:model.maxIndex - model.minIndex];
    
    //中奖区间为0
    model.minIndex = model.maxIndex;
}

//如果概率可以改变
if (model.isProbabilityChage) {
    for (MaxProModel *maxModel in model.probabilityArray) {
        if (model.count == maxModel.maxCount) {
            NSInteger changeCount = ([model.probability floatValue] - [maxModel.maxProbability floatValue]) * _lotteryCount;
            
            //修改其他的中奖区间
            [self changeRandomWithIndex:model.index changeCount:changeCount];
            
            //缩小中奖区间
            model.minIndex += changeCount;
            model.probability = maxModel.maxProbability;
            break;
        }
    }
}
}

/**
重新设置中奖区间
 @param index       index前的数据才需要设置
 @param changeCount index对应的数据的区间减少量
 */
- (void)changeRandomWithIndex:(NSInteger )index changeCount:(NSInteger)changeCount {
//数据问题,第二个间隔是不中奖的,所以就增大该间隔的中奖区间
for (int i = 1; i < index; i++) {
    //将model的区间减小,增加第一个奖品的中间区间(第一个奖品就是不中奖)
    LotteryModel *model = _dataArray[i];
    
    if (i > 1) {
        model.minIndex += changeCount;
    }
    
    model.maxIndex += changeCount;
}
}

转动角度_angle, M_PI / count是为了最后能指向中奖扇形区域的中间位置。model.index * 2 * M_PI / count 是确定了中奖区间的扇形的起始位置。后面再加几个圆周是为了让指针转动多几圈。

- (void)showAnimationWithSelectedModel:(LotteryModel *)model {
_lotteryButton.enabled = NO;

NSInteger count = _dataArray.count;
NSInteger current = 8;
_angle = M_PI / count + model.index * 2 * M_PI / count + current * 2 * M_PI;

CABasicAnimation *layer = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
layer.toValue = @(_angle);
layer.duration = _angle / (3 * M_PI);
layer.removedOnCompletion = NO;
layer.fillMode = kCAFillModeForwards;
layer.repeatCount = 0;
layer.delegate = self;

[_lotteryImageView.layer addAnimation:layer forKey:nil];
}

然后在指针动画停止的方法中,延迟2秒,使指针回到最初的位置,可以再次抽奖。并且在中奖之后弹出中奖的弹框。

#pragma mark - CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
//显示中奖弹窗
[self showAlertView];
//设置指针返回初始位置
[self backToStartPosition];
}

- (void)backToStartPosition {
CABasicAnimation *layer = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
layer.toValue = @(0);
layer.duration = 0.01;
layer.removedOnCompletion = NO;
layer.fillMode = kCAFillModeForwards;
layer.beginTime = CACurrentMediaTime() + 2; //延迟1S后执行
[_lotteryImageView.layer addAnimation:layer forKey:nil];
_lotteryButton.enabled = YES;
}

界面效果:

(转盘是一张图片,指针也是一张图片,指针圆形区域上面覆盖着一个按钮,点击按钮,指针就开始转动。)


转盘.png

demo (<--这是个跳链啊)上传到百度云了,喜欢的自己去下载吧

上一篇 下一篇

猜你喜欢

热点阅读