iOS 随机点名App

<span style="font-family:SimSun;font-size:18px;">
#import <UIKit/UIKit.h>

@interface Rootview : UIView
@property(nonatomic,retain)UIButton *starButton;
@property(nonatomic,retain)UITextField *textField;

@end
</span>

<span style="font-family:SimSun;font-size:18px;">#import "Rootview.h"



@implementation Rootview


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
//        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"pa.jpg"]];
        UIImageView *viw = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pa.jpg"]];
        viw.frame = CGRectMake(0, 0, 320, 480);
        viw.tag = 300;
        UIPinchGestureRecognizer *pin = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(g:)];
        [viw addGestureRecognizer:pin];
        
        [self addSubview:viw];
        [self allAllViews];
    }
    return self;
}
-(void)g:(UIPinchGestureRecognizer *)sender
{
    
    sender.view.transform = CGAffineTransformMakeScale(sender.scale, sender.scale);
    
}

-(void)allAllViews
{
    int count = 0;
    for(int i = 0;i < 5;i++)
    {
        for(int j = 0;j < 4;j++)
        {
            UIButton *button = [[UIButton alloc] initWithFrame:[UIScreen mainScreen].bounds];
            button.frame = CGRectMake(10+80*j, 20+70*i, 60, 50);
            button.backgroundColor = [UIColor orangeColor];
            button.layer.cornerRadius = 20;
            //从侧边栏添加姓名到button上面
            NSString *nameStr = [NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"name" ofType:@"txt"] encoding:NSUTF8StringEncoding error:nil];
            NSArray *nameArray = [nameStr componentsSeparatedByString:@"\n"];
            [button setTitle:nameArray[count] forState:UIControlStateNormal];
            button.tag = 100+count++;
            [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:button];
            
        }
    }

    self.starButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.starButton.frame = CGRectMake(30, 380, 70, 50);
    self.starButton.backgroundColor = [UIColor grayColor];
    self.starButton.layer.cornerRadius = 10;

    [self.starButton setTitle:@"开始" forState:UIControlStateNormal];
    [self.starButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

    
    self.starButton.titleLabel.font = [UIFont systemFontOfSize:20];
    [self addSubview:self.starButton];
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(140, 380, 160, 50)];
    self.textField.backgroundColor = [UIColor grayColor];
    self.textField.enabled = NO;
    self.textField.borderStyle = UITextBorderStyleRoundedRect;
    self.textField.font = [UIFont systemFontOfSize:30];
    self.textField.textAlignment = NSTextAlignmentCenter;
    self.textField.backgroundColor = [UIColor whiteColor];
    self.textField.textColor = [UIColor redColor];
    [self addSubview:self.textField];
    
</span><pre name="code" class="objc"><span style="font-family:SimSun;font-size:18px;">#import "RootViewController.h"
#import "Rootview.h"
@interface RootViewController ()
@property(nonatomic,retain)Rootview *myView;
@end

@implementation RootViewController
{
    NSInteger _count;       //循环计数
    NSTimer *_timer;        //时间计时器
    UIButton *_lastButton;  //选中的Button变色
    int m;

}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        self.view.backgroundColor = [UIColor orangeColor];
        self.myView = [[Rootview alloc] initWithFrame:[UIScreen mainScreen].bounds];
    }
    return self;
}
-(void)loadView
{
    self.view = self.myView;
}
- (void)viewDidLoad
{
        m = arc4random()%4;
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.myView.starButton addTarget:self action:@selector(buttonRun:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)buttonRun:(UIButton*)sender
{
        sender.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:arc4random()%256/255.0];


    //当点击开始按钮的时候
    if([[sender titleForState:UIControlStateNormal]isEqualToString:@"开始"])
    {
        [sender setTitle:@"停止" forState:UIControlStateNormal];
    }
    else
    {
        [sender setTitle:@"开始" forState:UIControlStateNormal];
    }
    //判断定时器是否开始
    if(_timer.isValid)
    {
        //如果开始就让他停下来
        [_timer invalidate];
    }
    else
    {
        //每隔多长时间就会执行你给定的方法
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(chose) userInfo:nil repeats:YES];
        _count = 0;
        
    }

}
-(void)chose
{

    int t = 50;//规定执行次数
    if(_count < t)
    {
        //随机数,随机选中
        int num = arc4random() % 18;
        while(num == 118 || num == 119)
        {
            num = arc4random()%18;
        }
        //变回原来的颜色
        _lastButton.backgroundColor = [UIColor orangeColor];
        //选人
        _lastButton = (UIButton*)[self.myView viewWithTag:100+num];
        //变颜色
        _lastButton.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:arc4random()%256/255.0];
        self.myView.textField.text = [_lastButton titleForState:UIControlStateNormal];
    }
    else
    {
        [_timer invalidate];
        [self.myView.starButton setTitle:@"开始" forState:UIControlStateNormal];
    }
    if(_count == t * 0.5)
    {
        //先关闭原先的计时器
        [_timer invalidate];
        //开一个新的定时器
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(chose) userInfo:nil repeats:YES];
    }
    if(_count == t * 0.8)
    {
        //先关闭原先的计时器
        [_timer invalidate];
        //开一个新的定时器
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(chose) userInfo:nil repeats:YES];
    }
    
    
    if(_count == t - m)
    {
        //先关闭原先的计时器
        [_timer invalidate];
        //开一个新的定时器
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(chose) userInfo:nil repeats:YES];
    }
    
    _count ++;
    
    
}

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

{
    
    //释放第一响应者
    
    NSLog(@"asdasd");
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
</span>

}-(void)buttonAction:(UIButton*)sender{ NSLog(@"%d",sender.tag);}@end

 



}






@end



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值