iOS 九宫格抽奖(弱鸡)
生活随笔
收集整理的這篇文章主要介紹了
iOS 九宫格抽奖(弱鸡)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
明天就是七夕了,破費的節日哈,多少要套路一下嘛。
今天刷某音看到一個用excel做的隨機選中禮物,應該是手動操作吧,哈哈~
看了以后突然想動手簡單實現一個抽獎,閑來無事那就干吧!!!
一、先設計單塊獎品視圖
一個方塊隨機背景色,上面放個獎品名稱,選中時加個邊框,加個透明度。
@interface FLYPrizeView : UIView @property (nonatomic, strong) NSString *name; @property (nonatomic, assign) BOOL isSelect; @end #import "FLYPrizeView.h"@interface FLYPrizeView () @property (nonatomic, strong) UILabel *nameLabel; @end@implementation FLYPrizeView- (instancetype)initWithFrame:(CGRect)frame {if (self = [super initWithFrame:frame]) {self.backgroundColor = [self RandomColor];[self initView];}return self; } /** 添加獎品名 */ - (void)initView {self.nameLabel = [[UILabel alloc] initWithFrame:self.bounds];self.nameLabel.textAlignment = NSTextAlignmentCenter;[self addSubview:self.nameLabel]; } /** 設置獎品名稱 */ - (void)setName:(NSString *)name {_name = name;self.nameLabel.text = _name; } /** 選擇中樣色 */ - (void)setIsSelect:(BOOL)isSelect {_isSelect = isSelect;if (_isSelect) {self.layer.borderWidth = 4;self.layer.borderColor = [UIColor colorWithRed:255.f/255.f green:255.f/255.f blue:0/255.f alpha:1].CGColor;self.alpha = 0.5f;} else {self.layer.borderWidth = 0;self.alpha = 1;} } /** 隨機色 */ - (UIColor*)RandomColor {NSInteger aRedValue = arc4random()%255;NSInteger aGreenValue = arc4random()%255;NSInteger aBlueValue = arc4random()%255;UIColor *randColor = [UIColor colorWithRed:aRedValue/255.0f green:aGreenValue/255.0f blue:aBlueValue/255.0f alpha:1.0f];return randColor; } @end二、再設計九宮格轉盤視圖
九個方格,中間方格為抽獎按鈕,其余為FLYPrizeView。
@interface FLYLuckDrawView : UIView /** 禮品名 */ @property (nonatomic, strong) NSArray *prizeNames; - (void)initLuckDrawView; @end #import "FLYLuckDrawView.h" #import "FLYPrizeView.h"#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width) #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)@interface FLYLuckDrawView () {NSTimer *startTimer;int currentTime; }/** 速度 */ @property (assign, nonatomic) CGFloat speedTime; /** 停止位置,默認第一個 */ @property (nonatomic, assign) NSInteger stopCount; /** 停止時間 */ @property (nonatomic, assign) NSInteger stopTime; /** 禮品數組 */ @property (nonatomic, strong) NSArray *prizeViews;@end@implementation FLYLuckDrawView- (instancetype)initWithFrame:(CGRect)frame {if (self = [super initWithFrame:frame]) {self.backgroundColor = [UIColor blackColor];currentTime = 0;self.stopCount = 0;self.stopTime = 30 + self.stopCount+arc4random()%10;self.speedTime = 0.1;}return self; }- (void)initLuckDrawView {CGFloat width = self.frame.size.width;CGFloat topMarge = 3; //距離頂部邊距CGFloat leftMarge = 3; //距離左邊距CGFloat space = 2; //之間的距離CGFloat prizeW = (width - leftMarge * 2 - space * 2)/3;NSMutableArray *views = [[NSMutableArray alloc] init];for (int i=0; i<9; i++) {CGFloat x = leftMarge + space * (i % 3) + (i % 3) * prizeW;CGFloat y = topMarge + space * (i / 3) + (i / 3) * prizeW;if ( i==4 ) { //中間抽獎按鈕UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, prizeW, prizeW)];btn.backgroundColor = [UIColor redColor];[btn setTitle:@"抽獎" forState:UIControlStateNormal];[btn addTarget:self action:@selector(onStartButtonClick:) forControlEvents:UIControlEventTouchUpInside];[self addSubview:btn];} else { //獎品FLYPrizeView *view = [[FLYPrizeView alloc] initWithFrame:CGRectMake(x, y, prizeW, prizeW)];view.name = self.prizeNames[i];[self addSubview:view];[views addObject:view];}}self.prizeViews = views;/**位置變換0 1 2 0 1 23 * 4 => 7 * 35 6 7 6 5 4*/[self TradePlacesWithPrizeView1:self.prizeViews[3] PrizeView2:self.prizeViews[4]];[self TradePlacesWithPrizeView1:self.prizeViews[4] PrizeView2:self.prizeViews[7]];[self TradePlacesWithPrizeView1:self.prizeViews[5] PrizeView2:self.prizeViews[6]];}- (void)TradePlacesWithPrizeView1:(FLYPrizeView *)firstView PrizeView2:(FLYPrizeView *)secondView {CGRect frame = firstView.frame;firstView.frame = secondView.frame;secondView.frame = frame; } /** 抽獎按鈕 */ - (void)onStartButtonClick:(UIButton *)btn {[btn setEnabled:NO];dispatch_async(dispatch_get_global_queue(0, 0), ^{self->startTimer = [NSTimer scheduledTimerWithTimeInterval:self.speedTime target:self selector:@selector(start:) userInfo:nil repeats:YES];[[NSRunLoop currentRunLoop] run];}); } /** 開始 */ - (void)start:(NSTimer *)timer {FLYPrizeView *oldView = [self.prizeViews objectAtIndex:currentTime % self.prizeViews.count];currentTime++;FLYPrizeView *prizeView = [self.prizeViews objectAtIndex:currentTime % self.prizeViews.count];dispatch_async(dispatch_get_main_queue(), ^{oldView.isSelect = NO;prizeView.isSelect = YES;});if (currentTime > self.stopTime) { //抽獎結果self.stopCount = [self.prizeViews indexOfObject:prizeView];NSLog(@"抽到的位置:%ld, stopTime:%ld", self.stopCount, self.stopTime);[timer invalidate];[self showAlerts:[NSString stringWithFormat:@"恭喜您獲得%@", prizeView.name]];return;}if (currentTime > self.stopTime - 10) {self.speedTime += 0.01 * (currentTime + 10 - self.stopTime); //動畫效果由快變慢[timer invalidate];dispatch_async(dispatch_get_global_queue(0, 0), ^{self->startTimer = [NSTimer scheduledTimerWithTimeInterval:self.speedTime target:self selector:@selector(start:) userInfo:nil repeats:YES];[[NSRunLoop currentRunLoop] run];});} }- (void)showAlerts:(NSString *)message {UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"中獎了" message:message preferredStyle:UIAlertControllerStyleAlert];__weak typeof(alert) weakAlert = alert;UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {[weakAlert dismissViewControllerAnimated:NO completion:nil];}];[alert addAction:alertAction];[[self viewController] presentViewController:alert animated:YES completion:nil]; }- (UIViewController *)viewController {for (UIView* next = [self superview]; next; next = next.superview) {UIResponder *nextResponder = [next nextResponder];if ([nextResponder isKindOfClass:[UIViewController class]]) {return (UIViewController *)nextResponder;}}return nil; }@end三、添加九宮格抽獎視圖
@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;FLYLuckDrawView *view = [[FLYLuckDrawView alloc] initWithFrame:CGRectMake(50, 200, screenWidth-100, screenWidth-100)];view.tag = 99;view.prizeNames = @[@"包包",@"口紅",@"神仙水",@"鉆戒",@"",@"五毛紅包",@"火鍋",@"萬元紅包",@"么么達~"];[view initLuckDrawView];[self.view addSubview:view]; }么么噠~就這么愉快的決定了。。。
總結
以上是生活随笔為你收集整理的iOS 九宫格抽奖(弱鸡)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PostgreSQL的下载和安装使用步骤
- 下一篇: 后3Q大战时代的防护选择