#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
// 应用程序的启动函数
// 作用:
// 1. 创建一个应用程序启动的对象
// UIApplication
// 2. 给应用程序指定一个代理,用来回应应用程序的各种状态(启动,点击,退出...),系统默认就是AppDelegate
// 3. 创建一个事件循环,随时响应外部事件
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[_window release];
MainViewController *mainVC = [[MainViewController alloc] init];
self.window.rootViewController = mainVC;
[mainVC release];
NSLog(@"应用加载完毕");
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(@"将要取消激活状态");
// 使用这个方法暂停正在进行的任务,停止计时器,减缓帧率,游戏要使用这个方法暂停游戏
// 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 {
NSLog(@"已经进入后台");
//使用这个方法释放资源,保存用户数据,废弃计时器,保存足够的应用程序数据,防止在后台被退出或者在重新进入前台加载数据快速恢复状态
// 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 {
NSLog(@"将要进入前台");
// 撤销一些进入后台前的改变
// 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 {
NSLog(@"已经转为激活状态");
// 重新启动被暂停的任务,如果应用程序之前在后台,可以选择刷新用户界面
// 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 {
NSLog(@"将要退出/结束");
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
#import "MainViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 335, 40)];
label.backgroundColor = [UIColor redColor];
[self.view addSubview:label];
[label release];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(20, 120, 335, 40);
[button setTitle:@"click" forState:UIControlStateNormal];
button.backgroundColor = [UIColor yellowColor];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// 将一个视图排除在响应者之外,即,让一个视图不响应用户交互
// iOS系统中有两个视图默认是关闭的,UILabel和UIImageView
button.userInteractionEnabled = NO;
}
- (void)buttonAction:(UIButton *)btn
{
NSLog(@"点击");
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"触摸开始");
// self.view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:arc4random()%256/255.0];
//
// self.view.frame = CGRectMake(arc4random()%(375-100), arc4random()%(667-40), 100, 40);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"触摸移动");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"触摸结束");
}
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"摇一摇开始");
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
NSLog(@"摇一摇结束");
}