#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,strong) NSTimer *mouseTimer;
@property (nonatomic,strong) NSMutableArray *arr;
@property (nonatomic,strong) NSMutableArray *hit;
@property (nonatomic,strong) UIButton *startButton;
@property (nonatomic,strong) UIImageView *mouse;
@property (nonatomic,strong) UILabel *scoreLabel;
@property (nonatomic) NSInteger tag;
@property (nonatomic) NSInteger score;
@property (nonatomic) BOOL isStart;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_score = 0;
UIImageView *backgroundImage;
UIImage *image = [UIImage imageNamed:@"back.png"];
backgroundImage = [[UIImageView alloc] initWithImage:image];
backgroundImage.frame = self.view.frame;
[self.view addSubview:backgroundImage];
_scoreLabel = [[UILabel alloc] init];
_scoreLabel.text = @"0";
_scoreLabel.frame = CGRectMake(245, 130, 100, 40);
[self.view addSubview:_scoreLabel];
_startButton = [UIButton buttonWithType:UIButtonTypeSystem];
_startButton.frame = CGRectMake(60, 130, 60, 50);
_startButton.backgroundColor = [UIColor lightGrayColor];
[_startButton setTitle:@"开始" forState:UIControlStateNormal];
[_startButton addTarget:self action:@selector(addMouseTimer) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_startButton];
UIImage *hole = [UIImage imageNamed:@"emptyhole.png"];
_arr = [[NSMutableArray alloc] initWithCapacity:6];
_hit = [[NSMutableArray alloc] initWithCapacity:3];
for(int i = 0;i<6;i++){
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"t%d.png",i+1]];
[_arr addObject:image];
}
for(int i = 12;i>6;i--){
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"t%d.png",i-6]];
[_arr addObject:image];
}
for(int i = 0;i<3;i++){
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"hit.png"]];
[_hit addObject:image];
}
for(int i=0;i<7;i++){
for(int j=0;j<5;j++){
UIButton *mouse = [UIButton buttonWithType:UIButtonTypeSystem];
[mouse setBackgroundImage:hole forState:UIControlStateNormal];
mouse.tag = 100+i*5+j;
mouse.frame = CGRectMake(20+75*j, 300+50*i, 50, 50);
[mouse addTarget:self action:@selector(hit:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:mouse];
}
}
// Do any additional setup after loading the view, typically from a nib.
}
- (void)hit:(UIButton *)sender{
if(sender.tag ==_tag){
[_mouse stopAnimating];
UIButton *button = (UIButton *)[self.view viewWithTag:_tag];
_mouse = [[UIImageView alloc] init];
_mouse.frame = button.frame;
_mouse.animationImages = _hit;
_mouse.animationRepeatCount = 1;
_mouse.animationDuration = 0.3;
[self.view addSubview:_mouse];
[_mouse startAnimating];
_tag = arc4random()%35+1000;
_score += 10;
NSLog(@"%li",(long) _score);
_scoreLabel.text = [NSString stringWithFormat:@"%d",(int)_score];
}
}
- (void)addMouseTimer{
if (_isStart==NO) {
_score = 0;
_scoreLabel.text = [NSString stringWithFormat:@"%d",(int)_score];
[_startButton setTitle:@"停止" forState:UIControlStateNormal];
_timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(appear) userInfo:nil repeats:YES];
_isStart = !_isStart;
}else{
[_startButton setTitle:@"重新开始" forState:UIControlStateNormal];
[_timer invalidate];
_isStart = !_isStart;
}
}
- (void)appear{
_tag = arc4random()%35+100;
UIButton *button = (UIButton *)[self.view viewWithTag:_tag];
[_mouse stopAnimating];
_mouse = [[UIImageView alloc] init];
_mouse.frame = button.frame;
_mouse.animationImages = _arr;
_mouse.animationRepeatCount = 1;
_mouse.animationDuration = 1.2;
[self.view addSubview:_mouse];
[_mouse startAnimating];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong) UIImageView *holeImage;
@property (nonatomic,strong) UIButton *button;
@property (nonatomic,strong) NSTimer *timer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back.png"]];
backgroundImage.frame = self.view.frame;
[self.view addSubview:backgroundImage];
backgroundImage.userInteractionEnabled = YES;
for (int i=0; i<6; i++) {
for(int j = 0; j<6; j++){
_holeImage = [[UIImageView alloc] init];
_holeImage.frame = CGRectMake(40+j*50, 290+i*50, 50, 50);
_holeImage.image = [UIImage imageNamed:@"emptyhole.png"];
_holeImage.userInteractionEnabled = YES;
_holeImage.tag = 101+6*i+j;
[self.view addSubview:_holeImage];
_button = [UIButton buttonWithType:UIButtonTypeSystem];
_button.frame = CGRectMake(0, 0, 50, 50);
_button.tag = 137+6*i+j;
[_button addTarget:self action:@selector(hit:) forControlEvents:UIControlEventTouchUpInside];
[_holeImage addSubview:_button];
}
}
[self addTimer];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)hit:(UIButton *)sender{
NSInteger num = sender.tag;
_holeImage = (UIImageView *)[self.view viewWithTag:num-36];
if(_holeImage.isAnimating == YES){
[_holeImage stopAnimating];
NSMutableArray *arr = [[NSMutableArray alloc] init];
UIImage *image = [UIImage imageNamed:@"hit.png"];
UIImage *image1 = [UIImage imageNamed:@"hit.png"];
[arr addObject:image];
[arr addObject:image1];
UIImageView *imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(0, 0, 50, 50);
imageView.animationImages = arr;
imageView.animationDuration = 0.2;
imageView.animationRepeatCount = 1;
[imageView startAnimating];
[_holeImage addSubview:imageView];
}
}
- (void)addTimer{
self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(playImage) userInfo:nil repeats:YES];
}
- (void)playImage{
NSMutableArray *animationTemp = [[NSMutableArray alloc] initWithCapacity:10];
for (int i = 0; i<6; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"t%d.png",i+1]];
[animationTemp addObject:image];
}
UIImageView *selectedHole = (UIImageView *)[self.view viewWithTag:arc4random()%36+101];
selectedHole.animationDuration = 1;
selectedHole.animationImages = animationTemp;
selectedHole.animationRepeatCount = 1;
[selectedHole startAnimating];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
[A Gull's training note][objective-c][the third day] [homework] hit the gopher X2