AppDelegate.swift中應(yīng)該了解的內(nèi)容
@UIApplicationMain //調(diào)用了OC中的UIApplicationMain函數(shù)泳桦;是iOS應(yīng)用程序的入口檀轨; 功能: 1.創(chuàng)建了一個(gè)UIApplication對(duì)象宵睦,代表當(dāng)前應(yīng)用程(作用是用來(lái)檢測(cè)當(dāng)前應(yīng)用程序狀態(tài)的改變)。2.創(chuàng)建一個(gè)遵守UIApplicationDelegate的協(xié)議的類的對(duì)象作為UIApplication的代理(作用處理應(yīng)用程序狀態(tài)的改變,實(shí)質(zhì)創(chuàng)建AppDelegate對(duì)象并且設(shè)置為UIApplication對(duì)象的代理)
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
//1. 當(dāng)應(yīng)用程序啟動(dòng)成功后,會(huì)自動(dòng)調(diào)用這個(gè)方法
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// 1.在這個(gè)方法中來(lái)創(chuàng)建應(yīng)用程序中的所有的界面
// 2.獲取應(yīng)用程序需要展示的數(shù)據(jù)
// 3.使用界面展示數(shù)據(jù)
// 注意:如果不在這個(gè)文件中創(chuàng)建window,那么程序會(huì)通過(guò)Main.storyboard去創(chuàng)建應(yīng)用程序的界面
print("啟動(dòng)成功")
return true
}
// 2. 當(dāng)應(yīng)用程序?qū)⒁腔钴S狀態(tài)的時(shí)候會(huì)自動(dòng)調(diào)用這個(gè)方法枝嘶。
// 活躍狀態(tài):程序在屏幕上可見
// 非活躍狀態(tài):程序沒有顯示在屏幕上(a.按home鍵進(jìn)入后臺(tái)。b.來(lái)電打斷蘸炸。c.在當(dāng)前應(yīng)用程序中打開其它的應(yīng)用程序)
func applicationWillResignActive(application: UIApplication) {
// 在這個(gè)方法中一般去暫停視頻/音頻播放;游戲需要暫停游戲尖奔;保存數(shù)據(jù)
print("將要成為非活躍狀態(tài)")
// 按home鍵:command + shift + h
// 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.
}
// 3. 應(yīng)用程序已經(jīng)進(jìn)入后臺(tái)的時(shí)候會(huì)自動(dòng)調(diào)用
// 進(jìn)入后臺(tái)按home鍵
func applicationDidEnterBackground(application: UIApplication) {
print("進(jìn)入后臺(tái)")
}
// 4.應(yīng)用程序?qū)⒁M(jìn)入前臺(tái)的時(shí)候會(huì)自動(dòng)調(diào)用的方法
// 進(jìn)入前臺(tái):
func applicationWillEnterForeground(application: UIApplication) {
print("將要進(jìn)入前臺(tái)")
}
// 5.應(yīng)用程序已經(jīng)變成活躍狀態(tài)的時(shí)候會(huì)自動(dòng)調(diào)用
// a.程序啟動(dòng)成功之后 b.程序從后臺(tái)重新進(jìn)入前臺(tái) c.來(lái)電打斷結(jié)束...
func applicationDidBecomeActive(application: UIApplication) {
print("已經(jīng)變成活躍狀態(tài)")
}
// 6.應(yīng)用程序?qū)⒁K止的時(shí)候會(huì)調(diào)用的方法
//
func applicationWillTerminate(application: UIApplication) {
print("應(yīng)用程序?qū)⒁K止")
}
如何用代碼的形式加載windows窗口
class AppDelegate: UIResponder, UIApplicationDelegate {
// UIWindow:UIView
// window:窗口搭儒,一個(gè)應(yīng)用程序想要展示在屏幕上,至少要有一個(gè)window提茁,一個(gè)手機(jī)應(yīng)用程序一般只有一個(gè)window
// 應(yīng)用程序中的所有界面全部是展示在window上的
var window: UIWindow?
// 1.程序已經(jīng)啟動(dòng)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// 1.創(chuàng)建UIWindow對(duì)象
// frame 是UIView中的屬性淹禾,確定視圖顯示在屏幕上的位置和大小
// UIScreen.mainScreen() 拿到手機(jī)屏幕
self.window = UIWindow.init(frame: UIScreen.mainScreen().bounds)
// 2.設(shè)置根視圖控制器
self.window?.rootViewController = UIViewController()
self.window?.backgroundColor = UIColor.yellowColor()
// Override point for customizatio n after application launch.
return true
}
}
UIView基本屬性和方法
// UIView:是iOS 中所有控件(視圖)直接/間接的父類;所以UIView的屬性和方法茴扁,對(duì)其他類型的視圖都有效
// 視圖:在屏幕上能看到的所有的東西都屬于視圖
// 1.創(chuàng)建UIView的對(duì)象
0let redView = UIView()
// 2. 想要將視圖展示在屏幕上的兩個(gè)條件铃岔;
// a.必須設(shè)置坐標(biāo)和大小(默認(rèn)坐標(biāo)是(0,0)大谢傧啊(0智嚷,0))
// b.將視圖添加到已經(jīng)展示在屏幕上的視圖
// 設(shè)置frame的(x,y)和(width,height)兩個(gè)部分組成
// redView.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(10, 10, 100, 100)
// 3.將視圖添加到界面上
self.view.addSubview(redView)
// 4.設(shè)置背景顏色
// 視圖的背景顏色默認(rèn)是透明色
// 顏色創(chuàng)建的方式:
// a.通過(guò)類型方法創(chuàng)建指定顏色
redView.backgroundColor = UIColor.redColor()
// b.通過(guò)三原色來(lái)創(chuàng)建顏色
// CGFloat就是UI中的浮點(diǎn)型
// 參數(shù)1,2纺且,3:紅盏道,綠,藍(lán)的值(0~1) 4:透明度(0~1)
// redView.backgroundColor = UIColor.init(red: 0.35, green: 0.45, blue: 0.75, alpha: 0.88)
// redView.backgroundColor = UIColor.init(white: 0.4, alpha: 1)
// 練習(xí):創(chuàng)建一個(gè)黃色的矩形载碌,顯示在紅色視圖的中間位置猜嘱,大小是(50,50)
// 方式1:
// let yellowView = UIView(frame: CGRectMake(35,35,50,50))
// yellowView.backgroundColor = UIColor.yellowColor()
// self.view.addSubview(yellowView)
// 方式2:
let yellowView = UIView(frame: CGRectMake(25,25,50,50))
yellowView.backgroundColor = UIColor.yellowColor()
// 添加到紅色視圖上
redView.addSubview(yellowView)
// 總結(jié):計(jì)算視圖的坐標(biāo)的時(shí)候,注意相對(duì)性嫁艇。當(dāng)前視圖被添加到哪個(gè)視圖上朗伶,那么當(dāng)前視圖的坐標(biāo)就是相對(duì)于誰(shuí)來(lái)算的
}
與frame相關(guān)的屬性
// 創(chuàng)建一個(gè)視圖
let redView = UIView()
// 添加到界面上
self.view.addSubview(redView)
// 設(shè)置背景顏色
redView.backgroundColor = UIColor.redColor()
// 1.frame(坐標(biāo)和大小)
redView.frame = CGRectMake(100, 100, 100, 100)
// 2.center(中心點(diǎn))
// a.通過(guò)frame和確定視圖的中心點(diǎn)坐標(biāo)
print(redView.center)
// b.可以通過(guò)改變center的值步咪,去改變視圖的坐標(biāo)
redView.center = CGPoint(x: 200, y: 200)
print(redView.center)
// 3.bounds(坐標(biāo)和大新劢浴)
// 默認(rèn)情況下bounds的坐標(biāo) 是(0,0)大小和視圖的frame大小一樣
print(redView.bounds)
// 了解:
// 如果改變bounds的大小歧斟,frame的大小和坐標(biāo)都改變纯丸,center不變
// 如果改變bounds的坐標(biāo),不影響當(dāng)前視圖的位置静袖。但是影響添加到當(dāng)前視圖上的子視圖的坐標(biāo)觉鼻,不建議修改bounds
// redView.bounds = CGRect(x: 0, y: 0, width: 200, height: 200)
// 4.transform(形變)
// 當(dāng)前視圖發(fā)生形變,那么添加到當(dāng)前視圖上的所有視圖都會(huì)跟著一起形變
// a.縮放
// 參數(shù)1:x方向上的縮放比例
// 參數(shù)2:y方向上的縮放比例
// redView.transform = CGAffineTransformMakeScale(0.8, 2.5)
// b.旋轉(zhuǎn)
// 參數(shù):旋轉(zhuǎn)角度(圓周率對(duì)應(yīng)的角度)
// redView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_4))
// c.平移
// 參數(shù)1:在x方向上平移的距離
// 參數(shù)2:在y方向上平移的距離
// redView.transform = CGAffineTransformMakeTranslation(100, 100)
// d.多個(gè)形變同時(shí)發(fā)生
// 在另外一個(gè)形變的前提下旋轉(zhuǎn)
// 參數(shù)1:另外一個(gè)形變
redView.transform = CGAffineTransformRotate(CGAffineTransformMakeScale(0.5, 0.5), CGFloat(M_PI_4))
// 在另外一個(gè)形變的前提下平移
redView.transform = CGAffineTransformTranslate(redView.transform, 0, 300)
// 在另外一個(gè)形變的前提下縮放
// 創(chuàng)建一個(gè)平移的形變
let transLation = CGAffineTransformMakeTranslation(100, 0)
redView.transform = CGAffineTransformScale(transLation, 0.5, 2 )
// 組合兩個(gè)變形
// 將旋轉(zhuǎn)形變和平移形變組合
let rotate = CGAffineTransformMakeRotation(0.2)
let transLation1 = CGAffineTransformMakeTranslation(100, 100)
redView.transform = CGAffineTransformConcat(transLation1, rotate)