UI打地鼠

//

//  AppDelegate.m

//  UI_Lesson打地鼠

//

//  Created by 李洪鹏 on 15/7/1.

//  Copyright (c) 2015年 李洪鹏. All rights reserved.

//

#import "AppDelegate.h"

@interface AppDelegate ()

{

    NSInteger _index;

    NSInteger _score;   //得分

}

@property (nonatomic, strong) UILabel *label;  //计分板

@property (nonatomic, strong) NSTimer *timer;  //计时器

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    

    self.window = [[[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    

    //设置初始值

    _index = 100;

    _score = 0;

    int count = 0;

    

    for (int i = 0; i < 4; i++) {

        for ( int j = 0; j < 3; j++) {

            UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

            button.frame = CGRectMake(20 + (100 + 20) *j, 50 + (100 + 20) *i, 100, 100);

            [button setBackgroundImage:[UIImage imageNamed:@"王琪.jpg"] forState:UIControlStateNormal];

            

            button.tag = 100 + count++;

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

            [self.window addSubview:button];

            //[button release];

            

            

            

        }

    }

    

    //创建计分板

    self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 50)];

    _label.center = CGPointMake(375 / 2, 667 - 50);

    _label.backgroundColor = [UIColor redColor];

    _label.text = [NSString stringWithFormat:@"%ld", _score];

    [self.window addSubview:_label];

    

    //设置计时器

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

    

    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];

    button3.frame = CGRectMake(30, 550, 100, 100);

//    button3.backgroundColor = [UIColor orangeColor];

    [button3 setBackgroundImage:[UIImage imageNamed:@"于华健.JPG"] forState:UIControlStateNormal];

    [self.window addSubview:button3];

    

    

    

    

    return YES;

}

- (void)buttonAction:(UIButton *)sender

{

    if (sender.tag == _index) {

        //显示打开的图片

        [sender setBackgroundImage:[UIImage imageNamed:@"耿强.jpg"] forState:

         UIControlStateNormal];

      //分数 + 1

        _score ++;

        //讲分数显示在计分板上

        self.label.text = [NSString stringWithFormat:@"%ld", _score];

        

        

    }

}

- (void)timerAction:(NSTimer *)sender

{

    //将之前的地鼠位置设置为地鼠的图片

    UIButton *button = (UIButton *)[self.window viewWithTag:_index];

    [button setBackgroundImage:[UIImage imageNamed:@"冯宇天.jpg"] forState:UIControlStateNormal];

    

    //随机一个位置

    _index = arc4random() %12 + 100;

    

    //通过 _index 将对应的位置显示地鼠的图片

    UIButton *button1 = (UIButton *)[self.window viewWithTag:_index];

    [button1 setBackgroundImage:[UIImage imageNamed:@"郝智盛.jpg"] forState:UIControlStateNormal];

    

    

}

- (void)applicationWillResignActive:(UIApplication *)application {

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

- (void)applicationDidEnterBackground:(UIApplication *)application {

    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

- (void)applicationWillEnterForeground:(UIApplication *)application {

    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

- (void)applicationDidBecomeActive:(UIApplication *)application {

    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

- (void)applicationWillTerminate:(UIApplication *)application {

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

@end

原文地址:https://www.cnblogs.com/lhp-1992/p/4615168.html