#import "AppDelegate.h"app
@interface AppDelegate ()dom
@end ide
@implementation AppDelegate字體
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {ui
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];this
self.window.rootViewController = [[UIViewController alloc] init];spa
self.window.rootViewController.view.userInteractionEnabled = NO;.net
//=================UIButton===============rest
//UIButton:UIControl(都有事件響應機制):UIViewcode
//1.建立button對象
UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 100, 50)];
//2.設置背景顏色
button.backgroundColor = [UIColor greenColor];
//設置tag值
button.tag = 100;
//3.顯示在界面上
[self.window addSubview:button];
//4.給按鈕添加事件(核心方法)
//當指定的事件發生的時候,響應消息的對象會去調用指定的消息
//參數1:響應消息的對象
//參數2:消息 (能夠不帶參,可是若是帶參只能有一個參數,而且這個參數的實參就是按鈕自己) --必須實現
//參數3:事件
//UIControlEventTouchUpInside 按下鬆開
//UIControlEventTouchDown 按下
//UIControlEventTouchUpInside:當手指按下按鈕而且鬆開的一瞬間,self去調用onclicked:方法
//UIControlEventTouchDown:當手指按下按鈕的瞬間,self去調用onclicked:方法
[button addTarget:self action:@selector(onclicked:) forControlEvents:UIControlEventTouchDown];
//====================系統按鈕============================
// UIButtonTypeCustom 自定製類型至關於[[UIButton alloc] init]建立的按鈕
// UIButtonTypeSystem 至關於[[UIButton alloc] init],不能去設置圖片和文字(屬於淘汰的枚舉值)
//
// UIButtonTypeDetailDisclosure, 系統詳情按鈕
// UIButtonTypeInfoLight, 系統詳情按鈕
// UIButtonTypeInfoDark, 系統詳情按鈕
// UIButtonTypeContactAdd, 系統添加按鈕
// UIButtonTypeRoundedRect = UIButtonTypeSystem,
//1.建立按鈕
UIButton * button2 = [UIButton buttonWithType:UIButtonTypeContactAdd];
// //設置背景顏色
button2.backgroundColor = [UIColor lightGrayColor];
// //設置frame
button2.frame = CGRectMake(100, 100, 100, 100);
//2.顯示在界面上
[_window addSubview:button2];
//3.添加事件
[button2 addTarget:self action:@selector(onclicked:) forControlEvents:UIControlEventTouchUpInside];
//================文字按鈕====================
//1.建立按鈕對象
UIButton * button3 = [[UIButton alloc] initWithFrame:CGRectMake(100, 200, 100, 50)];
//2.顯示在界面上
[_window addSubview:button3];
//設置背景顏色
button3.backgroundColor = [UIColor redColor];
//3.給不一樣狀態設置標題(讓按鈕上顯示文字,只能經過這個方法來設置)
//參數1:標題(按鈕上顯示的文字)
//參數2:
// UIControlStateNormal 正常狀態 ,若是隻設置了正常狀態下的文字,那麼其餘的狀態下文字和正常狀態下文字同樣
// UIControlStateHighlighted 高亮狀態
// UIControlStateDisabled 不可點擊狀態
// UIControlStateSelected 選中狀態
[button3 setTitle:@"來點我啊" forState:UIControlStateNormal];
[button3 setTitle:@"還真點啊" forState:UIControlStateHighlighted];
//讓按鈕不能點擊(默認是能夠點擊的)
// button3.enabled = NO;
[button3 setTitle:@"點不了" forState:UIControlStateDisabled];
//按鈕成爲選中狀態(默認是NO)
// button3.selected = YES;
[button3 setTitle:@"已經被點了" forState:UIControlStateSelected];
//4.給不一樣狀態設置不同的文字顏色
[button3 setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[button3 setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
//5.文字字體
//titleLabel就是按鈕上專門用來顯示文字的部分,可是不能經過拿到這個label來設置按鈕上的文字和文字顏色,只能設置字體
button3.titleLabel.font = [UIFont systemFontOfSize:25];
//=================圖片按鈕======================
//1.建立一個自定製按鈕
UIButton * button4 = [UIButton buttonWithType:UIButtonTypeCustom];
//2.設置frame
button4.frame = CGRectMake(100, 280, 80, 80);
//3.顯示在界面上
[_window addSubview:button4];
//設置按鈕背景顏色
button4.backgroundColor = [UIColor lightGrayColor];
//4.給不一樣狀態設置不一樣圖片
//參數1:圖片
//參數2:按鈕狀態
//設置正常狀態下的圖片
[button4 setImage:[UIImage imageNamed:@"map.png"] forState:UIControlStateNormal];
//設置高亮狀態下的圖片
[button4 setImage:[UIImage imageNamed:@"button_up.png"] forState:UIControlStateHighlighted];
//5.設置圖片到按鈕邊界的邊距
//上、左、下、右
[button4 setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
//=================圖片和文字同時存在================
//1.建立按鈕對象
UIButton * button5 = [[UIButton alloc] initWithFrame:CGRectMake(100, 400, 200, 100)];
//2.添加到界面上
[_window addSubview:button5];
//4.設置文字(文字顏色默認是白色)
[button5 setTitle:@"hello" forState:UIControlStateNormal];
//設置文字顏色
[button5 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
//3.設置圖片
[button5 setImage:[UIImage imageNamed:@"player_down_1"] forState:UIControlStateNormal];
//5.添加按鈕點擊事件
[button5 addTarget:self action:@selector(onclicked:) forControlEvents:UIControlEventTouchUpInside];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - 按鈕
- (void)onclicked:(UIButton *)btn{
if (btn.tag == 100) {
btn.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0f green:arc4random()%256/255.0f blue:arc4random()%256/255.0f alpha:1];
}
NSLog(@"按鈕");
}
- (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