iOS 抽奖程序 可指定版
上一篇,我们学习如何写抽奖程序,这篇将学习如何写一个可指定版。
代码下载地址
Model.h
#import <Foundation/Foundation.h>
@interface Model : NSObject
//正常的数组
@property (strong,nonatomic) NSArray* ClassArray;
//指定人的数据
@property (strong,nonatomic) NSArray* TeachName;
@end
Model.m
#import "Model.h"
@implementation Model
@synthesize ClassArray = _ClassArray;
-(id)init
{
if (self = [super init]) {
self.ClassArray = [[NSArray alloc] initWithObjects:@"aaa",@"bbb",@"ccc",nil];
self.TeachName = [NSArray arrayWithObjects:@"指定1",@"指定2",@"指定3",@"指定4", nil];
}
return self;
}
@end
ViewController.h
#import <UIKit/UIKit.h>
#import "Model.h"
@interface ViewController : UIViewController
@property (strong,nonatomic) UILabel* LuckyLabel;
@property (strong,nonatomic) Model* myModel;
@property (strong,nonatomic) NSTimer* TimeController;
@property (strong,nonatomic) UIButton* TimeButton;
@property (assign,nonatomic) BOOL TimeBool;
@property (assign,nonatomic) BOOL hahaBool;
-(void)StopTime;
-(void)addLabel;
-(void)addModel;
-(void)addTime;
-(void)addButton;
@end
ViewController.m
static int m_pInt = 0;
static int num = 0;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
TimeBool = YES;
hahaBool = NO;
num = 0;
UIImageView* backImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"002.png"]];
backImageView.frame = CGRectMake(0, 0, 768, 1024);
[self.view addSubview:backImageView];
[self addLabel];
[self addButton];
}
-(void)addLabel
{
self.LuckyLabel = [[UILabel alloc] initWithFrame:CGRectMake(300, 200, 290, 200)];
[self.LuckyLabel setFont:[UIFont fontWithName:@"Verdana" size:85]];
self.LuckyLabel.backgroundColor = [UIColor clearColor];
self.LuckyLabel.textAlignment= UITextAlignmentCenter;
[self.view addSubview:self.LuckyLabel];
UILabel* Title = [[UILabel alloc] initWithFrame:CGRectMake(200, 20, 350, 200)];
Title.backgroundColor = [UIColor clearColor];
Title.textAlignment = UITextAlignmentCenter;
[Title setFont:[UIFont fontWithName:@"Verdana" size:30]];
Title.text = @"幸运大抽奖";
[self.view addSubview:Title];
UILabel* NameLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 200, 260, 200)];
NameLabel.textAlignment = UITextAlignmentCenter;
[NameLabel setFont:[UIFont fontWithName:@"Verdana" size:50]];
NameLabel.backgroundColor = [UIColor clearColor];
NameLabel.text = @"幸运员工:";
[self.view addSubview:NameLabel];
}
-(void)addModel
{
if (m_pInt ==self.myModel.ClassArray.count)
{
m_pInt = 0;
}
self.LuckyLabel.text = [self.myModel.ClassArray objectAtIndex:m_pInt];
m_pInt++;
}
-(void)addTime
{
self.TimeController = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(addModel) userInfo:nil repeats:YES];
}
-(void)addButton
{
self.myModel = [[Model alloc] init];
self.TimeButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.TimeButton.frame = CGRectMake(266, 800, 200, 100);
[self.TimeButton setTitle:@"开始抽奖" forState:UIControlStateNormal];
self.TimeButton.titleLabel.textAlignment = UITextAlignmentCenter;
[self.TimeButton.titleLabel setFont:[UIFont fontWithName:@"Verdana" size:35]];
[self.TimeButton addTarget:self action:@selector(StopTime) forControlEvents:UIControlEventTouchUpInside];
//self.TimeButton.backgroundColor = [UIColor clearColor];
[self.view addSubview:self.TimeButton];
}
-(void)StopTime
{
if (TimeBool) {
[self addTime];
TimeBool = NO;
[self.TimeButton setTitle:@"停止" forState:UIControlStateNormal];
hahaBool = YES;
}
else{
[self.TimeController invalidate];
TimeBool = YES;
[self.TimeButton setTitle:@"开始抽奖" forState:UIControlStateNormal];
hahaBool = NO;
}
if (!hahaBool) {
switch (num) {
case 0 :
self.LuckyLabel.text = [self.myModel.TeachName objectAtIndex:num];
num++;
break;
case 1 :
self.LuckyLabel.text = [self.myModel.TeachName objectAtIndex:num];
num++;
break;
case 2 :
self.LuckyLabel.text = [self.myModel.TeachName objectAtIndex:num];
num++;
break;
case 3 :
self.LuckyLabel.text = [self.myModel.TeachName objectAtIndex:num];
num = 0;
break;
default:
break;
}
}
}