iOS抽奖程序

iOS抽奖程序



代码下载地址: http://vdisk.weibo.com/s/HKehU




我们要先写一个数据模型:Model.h
#import <Foundation/Foundation.h>

@interface Model : NSObject
@property (strong,nonatomic) NSArray* ClassArray;

@end

Model.m代码如下:
#import "Model.h"

@implementation Model
@synthesize ClassArray = _ClassArray;
-(id)init
{
    if (self = [super init]) {
        self.ClassArray = [[NSArray alloc] initWithObjects:@"ddd",@"aaa",@"vvv",@"fff",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;
-(void)StopTime;
-(void)addLabel;
-(void)addModel;
-(void)addTime;
-(void)addButton;
@end




ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    TimeBool = YES;
 
    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, 260, 200)];
    [self.LuckyLabel setFont:[UIFont fontWithName:@"Verdana" size:63]];
    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.view addSubview:self.TimeButton];
}

暂停方法
-(void)StopTime
{
    if (TimeBool) {
        [self addTime];
        TimeBool = NO;
        [self.TimeButton setTitle:@"停止" forState:UIControlStateNormal];
     

    }
    else{
        //Time停止
        [self.TimeController invalidate];
        TimeBool = YES;
        [self.TimeButton setTitle:@"开始抽奖" forState:UIControlStateNormal];
    }
}


原文地址:https://www.cnblogs.com/jiangu66/p/3162820.html