iOS-隨機點名或抽獎遊戲的實現

來源:互聯網
上載者:User

標籤:



#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-隨機點名或抽獎遊戲的實現

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.