iOS 抽奖程序 可指定版

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;
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杜甲同学

感谢打赏,我会继续努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值