<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