iOS 九宫格抽奖(弱鸡)

2024-06-01 15:32
文章标签 九宫格 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 九宫格抽奖(弱鸡)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1021394

相关文章

iOS HTTPS证书不受信任解决办法

之前开发App的时候服务端使用的是自签名的证书,导致iOS开发过程中调用HTTPS接口时,证书不被信任 - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAu

IOS 数组去重的几种方式

本来只知道NSSet和KeyValues的。今天又新学了几种方式 还有就是和同事学的一种方式 外层循环从0开始遍历,内层从最后一个元素开始遍历 for(int i=0;i<index;i++){  for(int j=index-1;j>i;j-- ){ } }

iOS Assertion failure in -[UITableView _classicHeightForRowAtIndexPath:]

iOS Assertion failure in -[UITableView _classicHeightForRowAtIndexPath:]  2015-04-24 11:40  956人阅读  评论(0)  收藏  举报   分类:   iOS 基础篇(208)  版权声明:本文为博主原创文章,未经博主允许不得转载。 Assertion

iOS:编译时出现no such file or directory:xxx以及use twice...filenames are used to distinguish private dec

简    注册  登录   添加关注 作者  婉卿容若 2016.04.29 11:22 写了21870字,被16人关注,获得了14个喜欢 iOS:编译时出现"no such file or directory:xxx"以及"use twice...filenames are used to distinguish private

iOS 到处 ipa包的时候 会有四个选项分别代表什么

如图 在 iOS 到处 ipa包的时候 会有四个选项  1.Save for iOS App Store Deployment 保存到本地 准备上传App Store 或者在越狱的iOS设备上使用 2.Save for Ad Hoc Deployment 保存到本地 准备在账号添加的可使用设备上使用(具体为在开发者账户下添加可用设备的udid),该app包是发布证书编

iOS 7适配上存在的各种问题

谈谈项目中遇到的各种iOS7适配问题 由于我的项目要适配到iOS7.1, 而现在已经是9时代了,在实际工作中我也是遇到了各种奇葩的坑,所以我想尽快把遇到的iOS7适配问题和解决方案分享出来,以后这些东西可能就用处不大了。   1.字体问题 iOS7中的字体适配恐怕是最麻烦的坑了,原因是iOS7以上的许多字体在7都是不存在的,甚至包括一些system-字体。比如system-

潜艇伟伟迷杂交版植物大战僵尸2024最新免费安卓+ios苹果+iPad分享

嗨,亲爱的游戏迷们!今天我要给你们种草一个超有趣的游戏——植物大战僵尸杂交版。这款游戏不仅继承了原有经典游戏的核心玩法,还加入了许多创新元素,让玩家能够体验到前所未有的乐趣。快来跟随我一起探索这个神奇的世界吧! 植物大战僵尸杂交版最新绿色版下载链接: https://pan.quark.cn/s/d60ed6e4791c 🔥 创新与经典的完美结合 植物大战僵尸杂交版在保持了原游戏经典玩

浏览器在iOS或Android中的一些方法

判断当前应用 var deviceType="H5"if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {deviceType='ios'} else if (/(Android)/i.test(navigator.userAgent)) {// alert("Android");deviceType='android'} else

基于51单片机抽奖系统

基于51单片机抽奖系统 (仿真+程序) 功能介绍 具体功能: 1.利用5片74HC495对单片机的IO进行串并转换,进而控制5个1位数码管; 2.采用一个独立按键用于抽奖系统的启停控制; 3.8位拨码开关是用于设定随机数发生器的“种子值”(初始值); ​演示视频: 基于51单片机抽奖系统  添加图片注释,不超过 140 字(可选) 程序 #inclu

iOS 视图之间的各种传值方式

属性传值 将A页面所拥有的信息通过属性传递到B页面使用 B页面定义了一个naviTitle属性,在A页面中直接通过属性赋值将A页面中的值传到B页面。 A页面DetailViewController.h文件 #import <UIKit/UIKit.h> #import "DetailViewController.h" @interface RootViewCon