日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

iOS 九宫格抽奖(弱鸡)

發(fā)布時(shí)間:2023/12/20 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS 九宫格抽奖(弱鸡) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

明天就是七夕了,破費(fèi)的節(jié)日哈,多少要套路一下嘛。
今天刷某音看到一個(gè)用excel做的隨機(jī)選中禮物,應(yīng)該是手動(dòng)操作吧,哈哈~

看了以后突然想動(dòng)手簡(jiǎn)單實(shí)現(xiàn)一個(gè)抽獎(jiǎng),閑來無事那就干吧!!!

一、先設(shè)計(jì)單塊獎(jiǎng)品視圖

一個(gè)方塊隨機(jī)背景色,上面放個(gè)獎(jiǎng)品名稱,選中時(shí)加個(gè)邊框,加個(gè)透明度。

@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; } /** 添加獎(jiǎng)品名 */ - (void)initView {self.nameLabel = [[UILabel alloc] initWithFrame:self.bounds];self.nameLabel.textAlignment = NSTextAlignmentCenter;[self addSubview:self.nameLabel]; } /** 設(shè)置獎(jiǎng)品名稱 */ - (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;} } /** 隨機(jī)色 */ - (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

二、再設(shè)計(jì)九宮格轉(zhuǎn)盤視圖

九個(gè)方格,中間方格為抽獎(jiǎng)按鈕,其余為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; /** 停止位置,默認(rèn)第一個(gè) */ @property (nonatomic, assign) NSInteger stopCount; /** 停止時(shí)間 */ @property (nonatomic, assign) NSInteger stopTime; /** 禮品數(shù)組 */ @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 ) { //中間抽獎(jiǎng)按鈕UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, prizeW, prizeW)];btn.backgroundColor = [UIColor redColor];[btn setTitle:@"抽獎(jiǎng)" forState:UIControlStateNormal];[btn addTarget:self action:@selector(onStartButtonClick:) forControlEvents:UIControlEventTouchUpInside];[self addSubview:btn];} else { //獎(jiǎng)品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; } /** 抽獎(jiǎng)按鈕 */ - (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) { //抽獎(jiǎng)結(jié)果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); //動(dòng)畫效果由快變慢[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:@"中獎(jiǎng)了" 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

三、添加九宮格抽獎(jiǎng)視圖

@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 = @[@"包包",@"口紅",@"神仙水",@"鉆戒",@"",@"五毛紅包",@"火鍋",@"萬元紅包",@"么么達(dá)~"];[view initLuckDrawView];[self.view addSubview:view]; }

么么噠~就這么愉快的決定了。。。

總結(jié)

以上是生活随笔為你收集整理的iOS 九宫格抽奖(弱鸡)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。