規(guī)則要求:
- 每個(gè)獎(jiǎng)品有中獎(jiǎng)概率和獎(jiǎng)品的個(gè)數(shù),當(dāng)獎(jiǎng)品個(gè)數(shù)達(dá)到一定的數(shù)值時(shí)扎运,中獎(jiǎng)概率會(huì)發(fā)生變化瑟曲。
- 轉(zhuǎn)盤轉(zhuǎn)動(dòng)到哪個(gè)獎(jiǎng)品的區(qū)域,即為抽中該獎(jiǎng)品豪治。
想法:
- 每個(gè)獎(jiǎng)品有個(gè)中獎(jiǎng)區(qū)間洞拨,使用簡(jiǎn)單的隨機(jī)數(shù)算法arc4random獲得一個(gè)隨機(jī)數(shù),如果該數(shù)落在了該中獎(jiǎng)區(qū)間负拟,則是中獎(jiǎng)扣甲。
- demo是用指針轉(zhuǎn)動(dòng)的方式完成,選中了獎(jiǎng)品之后齿椅,用動(dòng)畫使指針轉(zhuǎn)動(dòng)到對(duì)應(yīng)的扇形區(qū)域琉挖。(如果不想指針轉(zhuǎn),轉(zhuǎn)盤轉(zhuǎn)動(dòng)也是同樣的道理)
- 還有一些獎(jiǎng)品是有數(shù)量限制的涣脚,剩余量達(dá)到一定的數(shù)量之后示辈,就需要改變?cè)摢?jiǎng)品的中獎(jiǎng)概率了(demo中主要是用中獎(jiǎng)區(qū)間的大小來(lái)表示)。而且為了簡(jiǎn)單化遣蚀,整個(gè)demo的區(qū)間長(zhǎng)度不變矾麻,將某個(gè)“謝謝惠顧”的中獎(jiǎng)區(qū)間擴(kuò)大(后來(lái)發(fā)現(xiàn)改變整個(gè)demo的區(qū)間長(zhǎng)度,好像也是一樣的)芭梯。
數(shù)據(jù)存儲(chǔ)
[minIndex, maxIndex} 是為中獎(jiǎng)區(qū)間险耀,他們的值由中獎(jiǎng)概率和整個(gè)demo的區(qū)間長(zhǎng)度決定。isProbabilityChage表示中獎(jiǎng)概率是否會(huì)發(fā)生改變玖喘,是甩牺,概率數(shù)組才有存在的價(jià)值。因?yàn)楦怕蕯?shù)組probabilityArray存儲(chǔ)的是一個(gè)model累奈,所以需要在.m文件中贬派,顯式表示轉(zhuǎn)換(使用MJExtension)。
/**
中獎(jiǎng)的model
*/
@interface LotteryModel : NSObject
@property (copy, nonatomic) NSString *name; //獎(jiǎng)品名稱
@property (assign, nonatomic) NSInteger index; //獎(jiǎng)品的下標(biāo),唯一
@property (copy, nonatomic) NSDecimalNumber *probability; //概率
@property (assign, nonatomic) NSInteger count; //剩余張數(shù) -1為無(wú)窮張
@property (assign, nonatomic) BOOL isProbabilityChage; //概率是否隨張數(shù)改變澎媒。
@property (copy, nonatomic) NSMutableArray *probabilityArray; //isProbabilityChage = YES 時(shí)有用
@property (assign, nonatomic) NSInteger minIndex; //中獎(jiǎng)最小值搞乏,包括
@property (assign, nonatomic) NSInteger maxIndex; //中獎(jiǎng)的最大值,不包括
@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
/**
動(dòng)態(tài)概率的model
*/
@interface MaxProModel : NSObject
@property (assign, nonatomic) NSInteger maxCount;//當(dāng)張數(shù)=maxCount時(shí)戒努,中獎(jiǎng)概率為maxProbability
@property (copy, nonatomic) NSDecimalNumber *maxProbability;
@end
數(shù)據(jù)處理
創(chuàng)建數(shù)據(jù)方式如下(dict基本都是這么創(chuàng)建请敦,只截取部分):
- (void)initData {
_dataArray = [NSMutableArray new];
NSDictionary *dict4 = @{
@"name" : @"謝謝惠顧",
@"index" : @3,
@"probability" : [NSDecimalNumber decimalNumberWithString:@"0.125"],
@"count" : @-1,
@"isProbabilityChage" : @0,
};
NSDictionary *dict5 = @{
@"name" : @"499元XX牌電冰箱購(gòu)買券",
@"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];
//設(shè)置整個(gè)demo的區(qū)間長(zhǎng)度
_lotteryCount = 10000
NSInteger min = 0;
//設(shè)置每個(gè)獎(jiǎng)品中獎(jiǎng)區(qū)間
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;
}
}
用戶點(diǎn)擊抽獎(jiǎng)之后,獲得一個(gè)隨機(jī)數(shù),然后判斷隨機(jī)數(shù)落入那個(gè)獎(jiǎng)品的中獎(jiǎng)區(qū)間內(nèi)侍筛。如果獎(jiǎng)品有數(shù)量限制的萤皂,則需要將獎(jiǎng)品數(shù)減一,并且要判斷是否需要改變?cè)摢?jiǎng)品的中獎(jiǎng)概率勾笆。然后使用動(dòng)畫敌蚜,讓指針指向該獎(jiǎng)品對(duì)應(yīng)的扇形區(qū)域桥滨。
- (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(@"恭喜窝爪,您抽中了%@,快點(diǎn)去購(gòu)買商品吧", model.name);
if (model.count != -1 && model.count > 1) {
model.count -= 1;
}
//修改中獎(jiǎng)概率
[self changeProbabilityWithModel:model];
//顯示動(dòng)畫
[self showAnimationWithSelectedModel:model];
break;
}
else {
NSLog(@"%@", model.name);
//顯示動(dòng)畫
[self showAnimationWithSelectedModel:model];
break;
}
}
}
}
對(duì)于可以修改概率的獎(jiǎng)品,根據(jù)概率數(shù)組對(duì)應(yīng)的概率修改齐媒,并修改該獎(jiǎng)品的概率區(qū)間蒲每。
- (void)changeProbabilityWithModel:(LotteryModel *)model {
//獎(jiǎng)品已經(jīng)沒(méi)有了,需要將獎(jiǎng)品的中獎(jiǎng)區(qū)間改為0
if (model.count == 0) {
//修改其他的中獎(jiǎng)區(qū)間
[self changeRandomWithIndex:model.index changeCount:model.maxIndex - model.minIndex];
//中獎(jiǎng)區(qū)間為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;
//修改其他的中獎(jiǎng)區(qū)間
[self changeRandomWithIndex:model.index changeCount:changeCount];
//縮小中獎(jiǎng)區(qū)間
model.minIndex += changeCount;
model.probability = maxModel.maxProbability;
break;
}
}
}
}
/**
重新設(shè)置中獎(jiǎng)區(qū)間
@param index index前的數(shù)據(jù)才需要設(shè)置
@param changeCount index對(duì)應(yīng)的數(shù)據(jù)的區(qū)間減少量
*/
- (void)changeRandomWithIndex:(NSInteger )index changeCount:(NSInteger)changeCount {
//數(shù)據(jù)問(wèn)題喻括,第二個(gè)間隔是不中獎(jiǎng)的邀杏,所以就增大該間隔的中獎(jiǎng)區(qū)間
for (int i = 1; i < index; i++) {
//將model的區(qū)間減小,增加第一個(gè)獎(jiǎng)品的中間區(qū)間(第一個(gè)獎(jiǎng)品就是不中獎(jiǎng))
LotteryModel *model = _dataArray[i];
if (i > 1) {
model.minIndex += changeCount;
}
model.maxIndex += changeCount;
}
}
轉(zhuǎn)動(dòng)角度_angle唬血, M_PI / count是為了最后能指向中獎(jiǎng)扇形區(qū)域的中間位置望蜡。model.index * 2 * M_PI / count 是確定了中獎(jiǎng)區(qū)間的扇形的起始位置。后面再加幾個(gè)圓周是為了讓指針轉(zhuǎn)動(dòng)多幾圈拷恨。
- (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];
}
然后在指針動(dòng)畫停止的方法中脖律,延遲2秒,使指針回到最初的位置腕侄,可以再次抽獎(jiǎng)小泉。并且在中獎(jiǎng)之后彈出中獎(jiǎng)的彈框。
#pragma mark - CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
//顯示中獎(jiǎng)彈窗
[self showAlertView];
//設(shè)置指針?lè)祷爻跏嘉恢?[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后執(zhí)行
[_lotteryImageView.layer addAnimation:layer forKey:nil];
_lotteryButton.enabled = YES;
}
界面效果:
(轉(zhuǎn)盤是一張圖片冕杠,指針也是一張圖片微姊,指針圓形區(qū)域上面覆蓋著一個(gè)按鈕,點(diǎn)擊按鈕分预,指針就開(kāi)始轉(zhuǎn)動(dòng)兢交。)
demo (<--這是個(gè)跳鏈啊)上傳到百度云了,喜歡的自己去下載吧