码迷,mamicode.com
首页 > 移动开发 > 详细

iOS-随机点名或抽奖游戏的实现

时间:2015-08-02 13:44:20      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:



#import "ViewController.h"


@interface ViewController ()

{

    NSMutableArray *nameList;

    UILabel *label;

    NSTimer *timer;

}

@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    [self loadData];

    

    

    self.view.backgroundColor = [UIColor grayColor];

    label = [[UILabel alloc]initWithFrame:CGRectMake(50, 200, 275, 50)];

    label.text = @"不会是我吧";

    

    label.textAlignment = NSTextAlignmentCenter;

    label.font = [UIFont systemFontOfSize:30];

    label.textColor = [UIColor orangeColor];

    label.backgroundColor = [UIColor clearColor];

    [self.view addSubview:label];

    

    

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    button.frame = CGRectMake(150, 500, 100, 100);

    [self.view addSubview:button];

    [button setTitle:@"点我啊" forState:UIControlStateNormal];

    button.backgroundColor = [UIColor redColor];

    button.layer.cornerRadius = 50;

    button.layer.masksToBounds = YES;

    button.showsTouchWhenHighlighted = YES;

    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    [button addTarget:self action:@selector(push:) forControlEvents:UIControlEventTouchUpInside];

    

    timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(loadData) userInfo:nil repeats:YES];

    [[NSRunLoop currentRunLoop ]addTimer:timer forMode:NSDefaultRunLoopMode];

    timer.fireDate = [NSDate distantFuture];

}




- (void)loadData

{

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

    nameList = [NSMutableArray arrayWithObjects:@"a",@"b",@"c",@"d",@"e",@"f",@"g俊",@"f",@"gd",@"fv",@"sd",@"f",@"sdf",@"4t",@"i红",@"k",@"u",@"i海",@"ijk",@"k",@"i林",@"杨小平",@"i均",@"ui飞",@"i宽富",@"i旺",@"e浩",@"i诚",@"i文兴",@"i水",@"i波",@"tu金奎",@"i",@"王e",@"5j来",@"l康",@"i8黔",@"k.意",@";龙",@u",@"ol忠6华",@"y涛", nil];

    [userDefaults setObject:nameList forKey:@"nameList"];

    [userDefaults synchronize];

    [userDefaults objectForKey:@"nameList"];

    int arcNumber = arc4random()%nameList.count;

    label.text = nameList[arcNumber];

    

    

}


- (void)push:(UIButton *)sender

{

    if (sender.selected !=YES ) {

        label.textColor = [UIColor clearColor];

        [sender setTitle:@"暂停" forState:UIControlStateNormal];

//        [nameList removeObject:label.text];

        timer.fireDate = [NSDate distantPast];

        sender.selected = YES;

    }else{

        timer.fireDate = [NSDate distantFuture];

        label.textColor = [UIColor blackColor];

        

        [sender setTitle:@"点我啊" forState:UIControlStateNormal];

        sender.selected = NO;

    }

    

}


- (void)showAlertMessage:(NSString *)message

{

    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"真的是你" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

    [alert show];


}



@end


版权声明:本文为博主原创文章,未经博主允许不得转载。

iOS-随机点名或抽奖游戏的实现

标签:

原文地址:http://blog.csdn.net/jzq_sir/article/details/47205987

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!