關(guān)于AppDelegate.swift
UIapplicationMain
UIapplicationMain是IOS應(yīng)用程序的入口助泽。
1.創(chuàng)建一個(gè)UIapplicationMain對(duì)象代表當(dāng)前應(yīng)用程序,可以用來(lái)檢測(cè)當(dāng)親應(yīng)用程序的改變
2.創(chuàng)建一個(gè)UIapplicationMainDelegate的協(xié)議的類的對(duì)象最為UIapplication的代理擦耀,處理應(yīng)用程序的改變(創(chuàng)建AppDelegate對(duì)象并且設(shè)置為UIApplication對(duì)象的代理)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool{}
當(dāng)應(yīng)用程序已經(jīng)啟動(dòng)成功后顿痪,會(huì)自動(dòng)調(diào)用該方法依溯。
1.在這個(gè)方法中創(chuàng)建應(yīng)用程序中的所有界面
2.獲取應(yīng)用程序需要的展示數(shù)據(jù)
3.使用界面展示數(shù)據(jù)
注意:如果不在該方法中去創(chuàng)建window胚泌,那么程序會(huì)自動(dòng)通過(guò)Main.storyboard去創(chuàng)建應(yīng)用程序界面
func applicationWillResignActive(application: UIApplication) {}
當(dāng)應(yīng)用程序?qū)⒁蔀榉腔钴S狀態(tài)的時(shí)候回自動(dòng)調(diào)用這個(gè)方法。(活躍狀態(tài):屏幕上看見(jiàn)纸兔;非活躍狀態(tài):程序沒(méi)有顯示在屏幕上惰瓜,如:按HOME鍵進(jìn)入后臺(tái)、來(lái)電打斷汉矿、當(dāng)前應(yīng)用程序打開(kāi)其他的應(yīng)用程序)
在這個(gè)方法中一般要暫停視頻/音頻播放崎坊,游戲需要暫停游戲,保存數(shù)據(jù)洲拇。
func applicationDidEnterBackground(application: UIApplication) {}
進(jìn)入后臺(tái)妇押,自動(dòng)調(diào)用
func applicationWillEnterForeground(application: UIApplication) {}
進(jìn)入前臺(tái)自動(dòng)調(diào)用
func applicationDidBecomeActive(application: UIApplication) {}
進(jìn)入活躍狀態(tài)自動(dòng)調(diào)用
1.程序啟動(dòng)成功后
2.程序從后臺(tái)重新進(jìn)入
3.來(lái)電打斷結(jié)束
func applicationDidBecomeActive(application: UIApplication) {}
應(yīng)用程序終止的時(shí)候調(diào)用
窗口
UIAwindow繼承UIView
window
窗口求橄,一個(gè)應(yīng)用程序想要展示在屏幕上放前,至少要有一個(gè)window快耿,一個(gè)手機(jī)應(yīng)用程序一般只有一個(gè)window
應(yīng)用程序的所有界面全部展示在window上的
創(chuàng)建UIKwindow對(duì)象
//程序已經(jīng)啟動(dòng)完成
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//1.創(chuàng)建UIKwindow對(duì)象
//frame是UIView中屬性確定視圖顯示屏幕上的位置和大小
//UIScreen.mainScreen()拿到手機(jī)屏幕
self.window = UIWindow.init(frame:UIScreen.mainScreen().bounds)
//2.設(shè)置根視圖控制器(不寫(xiě)會(huì)奔潰)
self.window?.rootViewController = ViewController()
//3.設(shè)置背景色
self.window?.backgroundColor = UIColor.yellowColor()
return true
}
UIView
UIView是IOS中所有視圖(控件)直接/間接的父類;所以UIView的屬性和方法纽乱,對(duì)于其他的類型的視圖也有效
視圖:在屏幕上能看見(jiàn)的所有的東西都屬于視圖
創(chuàng)建UIView的對(duì)象
let redView = UIView.init()
let blueView = UIView.init()
//想要將視圖展示在屏幕上的兩個(gè)必要條件“
//a.必須設(shè)置坐標(biāo)和大卸暌铩(默認(rèn)坐標(biāo)(0.0),大醒涣小(0租冠,0))
//b.設(shè)置frame屬性(有坐標(biāo)(x,y)和大小(width,height)由兩個(gè)部分組成)
//2.將視圖添加到已經(jīng)展示在屏幕上的視圖上
blueView.frame = CGRect(x: 10, y: 10, width: 100, height: 100)
//ios中所有的結(jié)構(gòu)體都有一個(gè)對(duì)應(yīng)的Make方法用來(lái)創(chuàng)建一個(gè)結(jié)構(gòu)體變量
redView.frame = CGRectMake(150, 250, 100, 100)
//3.將視圖添加到界面上
self.view.addSubview(redView)
self.view.addSubview(blueView)
//4.設(shè)置背景色
//視圖的背景色默認(rèn)是透明色
//顏色的創(chuàng)建方式:
//a.通過(guò)類型方法創(chuàng)建指定顏色
redView.backgroundColor = UIColor.redColor()
//b.通過(guò)三原色創(chuàng)建顏色
//CGFloat就是ui中的浮點(diǎn)型
//參數(shù)1,2薯嗤,3:紅顽爹,綠,藍(lán)的值(0-1)
//參數(shù)4:透明度(0-1)
blueView.backgroundColor = UIColor.init(red: 30/255.0, green: 133/255.0, blue: 26/255.0, alpha: 0.5)
//c.創(chuàng)建灰色
redView.backgroundColor = UIColor.init(white: 0.8, alpha: 1)
//聯(lián)系:創(chuàng)建一個(gè)黃色的矩形骆姐,顯示在紅色視圖的中點(diǎn)位置镜粤,大小50,50
// let yellowView = UIView.init()
// yellowView.frame = CGRect(x: 175, y: 275, width: 50, height: 50)
// self.view.addSubview(yellowView)
// yellowView.backgroundColor = UIColor.yellowColor()
//方式2
let yellowView = UIView.init(frame:CGRectMake(25, 25, 50, 50))
redView.addSubview(yellowView)
yellowView.backgroundColor = UIColor.yellowColor()
總結(jié)
計(jì)算視圖的坐標(biāo)的時(shí)候玻褪,注意相對(duì)性肉渴,當(dāng)前視圖被添加到哪個(gè)視圖上,那么當(dāng)前視圖的坐標(biāo)就是相對(duì)于誰(shuí)來(lái)算的
frame
override func viewDidLoad() {
super.viewDidLoad()
//創(chuàng)建一個(gè)視圖對(duì)象
let redView = UIView.init()
//添加到界面上
self.view.addSubview(redView)
//設(shè)置背景色
redView.backgroundColor = UIColor.redColor()
//1.frame(坐標(biāo)和大小)
redView.frame = CGRectMake(150, 250, 100, 100)
//2.center(中心點(diǎn)坐標(biāo))
//a.確定frame和確定視圖的中心點(diǎn)坐標(biāo)
print(redView.center)
//b.可以通過(guò)改變center的值带射,去改變視圖的坐標(biāo)
redView.center = CGPointMake(190, 325)
print(redView.frame)
//3.bounds(坐標(biāo)和大谢剖)
//默認(rèn)情況下bounds的坐標(biāo)是(0,0)庸诱,大小和視圖的frame大小一樣
print(redView.bounds)
//了解:
//如果改變了bounds的大小捻浦,frame的大小和坐標(biāo)都改變,center不變
redView.bounds = CGRectMake(0, 0, 200, 200)
print(redView.frame)
//只改bounds的坐標(biāo)不影響當(dāng)前視圖的位置桥爽。但影響添加到當(dāng)前視圖的子視圖的坐標(biāo)朱灿,不建議修改bounds
// redView.bounds = CGRectMake(20, 20, 100, 100)
// let yellowView = UIView.init(frame:CGRectMake(165, 300, 50, 50))
// redView.addSubview(yellowView)
// yellowView.backgroundColor = UIColor.yellowColor()
// print(redView.frame)
// print(yellowView.frame)
//4.transform(形變)但只會(huì)生效一個(gè)
//當(dāng)前視圖發(fā)生形變。那么添加到當(dāng)前視圖上的所有的視圖會(huì)跟著一起形變
//a.縮放
//參數(shù)1钠四,x方向的縮放比例
//參數(shù)2,y方向的縮放比例
redView.transform = CGAffineTransformMakeScale(0.5, 0.5)
//b.旋轉(zhuǎn)
//參數(shù):旋轉(zhuǎn)角度(圓周率)
redView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_4))
//c.平移
//參數(shù)1在x方向平移的距離
//參數(shù)2在y方向平移的距離
redView.transform = CGAffineTransformMakeTranslation(0, 100)
//d.多個(gè)平移同時(shí)發(fā)生
//在另外一個(gè)形變的前提下旋轉(zhuǎn)
//參數(shù)1:另外一個(gè)形變
//參數(shù)2:旋轉(zhuǎn)角度
redView.transform = CGAffineTransformRotate(CGAffineTransformMakeScale(0.5, 0.5), CGFloat(M_PI_4))
//在另一個(gè)形變的前提下盗扒,平移
redView.transform = CGAffineTransformTranslate(redView.transform, 0, 100)
//在另一個(gè)形變的前提下,縮放
// redView.transform = CGAffineTransformScale(CGAffineTransformRotate(CGAffineTransformTranslate(<#T##t: CGAffineTransform##CGAffineTransform#>, <#T##tx: CGFloat##CGFloat#>, <#T##ty: CGFloat##CGFloat#>), <#T##angle: CGFloat##CGFloat#>), <#T##sx: CGFloat##CGFloat#>, <#T##sy: CGFloat##CGFloat#>)
// //組合兩個(gè)形變
// redView.transform = CGAffineTransformConcat(<#T##t1: CGAffineTransform##CGAffineTransform#>, <#T##t2: CGAffineTransform##CGAffineTransform#>)
}