視圖的創(chuàng)建
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// 1.視圖控制器本身不顯示,但是每個(gè)視圖控制器都有一個(gè)view的屬性,專門用來負(fù)責(zé)視圖控制器的顯示。想要顯示在視圖控制器上的內(nèi)容需要添加到它的view屬性上
// 2.在使用視圖控制器的時(shí)候,一般不直接使用UIviewController這個(gè)類,而是通過繼承UIViewController寫一個(gè)自己的視圖控制器類
// 3.在一個(gè)應(yīng)用程序中,一般一個(gè)界面對(duì)應(yīng)一個(gè)視圖控制器
// 1.創(chuàng)建window對(duì)象
self.window = UIWindow.init(frame: UIScreen.mainScreen().bounds)
// 2.設(shè)置根視圖控制器
// 創(chuàng)建視圖控制器對(duì)象:
// a.手寫創(chuàng)建視圖控制器
let root1 = FirstViewController()
// 每個(gè)視圖控制器都有一個(gè)view屬性,負(fù)責(zé)顯示
root1.view.backgroundColor = UIColor.redColor()
// b.通過xib去創(chuàng)建視圖控制器
// 創(chuàng)建一個(gè)類繼承UIUViewController,創(chuàng)建的時(shí)候?qū)ⅰ癮lse creat XIB ” 選上
// 直接用最簡(jiǎn)單的方式創(chuàng)建對(duì)應(yīng)的類的對(duì)象
let root2 = ThirdViewController()
// 直接通過xib創(chuàng)建視圖控制器的方法創(chuàng)建
// 參數(shù)1:xib文件名
let root4 = ThirdViewController(nibName: "ThirdViewController", bundle: nil)
// c.通過storyboard去創(chuàng)建視圖控制器
// 拿到storyboard文件拿到對(duì)象
// 參數(shù)1:storyboard文件名
let storyboard = UIStoryboard.init(name: "Second", bundle: nil)
// 拿到storyboard文件中箭頭指向的視圖控制器
let root3 = storyboard.instantiateInitialViewController()
// 設(shè)置window的根視圖(window默認(rèn)顯示的就是它的根視圖控制器)
self.window?.rootViewController = root3
return true
}
視圖的生命周期
/ 1. 開始創(chuàng)建視圖控制器的view的屬性的時(shí)候會(huì)自動(dòng)調(diào)用
override func loadView() {
super.loadView()
print("創(chuàng)建view")
}
// 2. view屬性已經(jīng)加載完成之后會(huì)調(diào)用(創(chuàng)一個(gè)視圖控制器只會(huì)調(diào)用一次)
// 一般在這個(gè)方法中去搭建當(dāng)前視圖控制器對(duì)應(yīng)的界面
override func viewDidLoad() {
super.viewDidLoad()
// 設(shè)置背景顏色
self.view.backgroundColor = UIColor.greenColor()
// 添加按鈕
let button = UIButton.init(frame: CGRectMake(100, 100, 100, 50))
button.setTitle("下一頁", forState: .Normal)
button.addTarget(self, action: "buttonAction", forControlEvents: .TouchDown)
self.view.addSubview(button)
print("view加載完成")
}
// 3.每次view將要出現(xiàn)的時(shí)候會(huì)調(diào)用(可能會(huì)調(diào)用多次)
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
print("view將要出現(xiàn)")
}
// 4.每次view已經(jīng)出現(xiàn)的時(shí)候會(huì)調(diào)用(可能會(huì)被調(diào)用多次)
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
print("view已經(jīng)出現(xiàn)")
}
// 5.每次view將要消失的時(shí)候會(huì)被調(diào)用(可能會(huì)被調(diào)用多次)
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
print("view將要消失")
}
// 6.view已經(jīng)消失的時(shí)候會(huì)被調(diào)用(可能會(huì)被調(diào)用多次)
override func viewDidDisappear(animated: Bool) {
super.viewDidAppear(animated)
print("view已經(jīng)消失")
}
// 接收到內(nèi)存警告的時(shí)候會(huì)調(diào)用
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
轉(zhuǎn)場(chǎng)動(dòng)畫
func btnAction() {
// 轉(zhuǎn)場(chǎng)動(dòng)畫,就是界面切換的時(shí)候的動(dòng)畫效果。
// 1.添加轉(zhuǎn)場(chǎng)動(dòng)畫
// a.創(chuàng)建轉(zhuǎn)場(chǎng)動(dòng)畫對(duì)象
let animation = CATransition.init()
// b.設(shè)置動(dòng)畫時(shí)間
animation.duration = 0.4
// c.設(shè)置動(dòng)畫類型
animation.type = "cube"
// d.設(shè)置動(dòng)畫的方向
animation.subtype = kCATransitionFromRight
// e.添加動(dòng)畫
// 可以通過任何已經(jīng)顯示在界面上的視圖去拿到當(dāng)前應(yīng)用程序的win(主窗口)
// 參數(shù)2. 區(qū)分不同的動(dòng)畫
self.view.window?.layer.addAnimation(animation, forKey: nil)
// 跳轉(zhuǎn)到下一個(gè)界面
let second = SecondViewController()
self.presentViewController(second, animated: false , completion: nil)
}
自己封裝的轉(zhuǎn)場(chǎng)動(dòng)畫
public enum TransitionType: String{
/// 交叉淡化過渡
case Fade = "fade"
/// 新視圖移到舊視圖上面
case MoveIn = "moveIn"
/// 新視圖把舊視圖推出去
case Push = "push"
/// 將舊視圖移開,顯示下面的
case Reveal = "reveal"
/// 向上翻一頁
case pageCurl
/// 向下翻一頁
case pageUnCurl
/// 滴水效果
case rippleEffect
///收縮效果,如一塊布被抽走
case suckEffect
///立方體效果
case cube
/// 上下翻轉(zhuǎn)效果
case oglFlip
}
public enum TransitionDirection: String{
case FromRight = "fromRight"
case FromLeft = "fromLeft"
case kCATransitionFromTop = "fromTop"
case kCATransitionFromBottom = "fromBottom"
}
extension UIView {
// 添加轉(zhuǎn)場(chǎng)動(dòng)畫
func addTransitionAnimation(duration: Double,type: TransitionType,direction:TransitionDirection) {
// 1.創(chuàng)建動(dòng)畫對(duì)象
let animation = CATransition.init()
// 2.設(shè)置動(dòng)畫時(shí)間
animation.duration = duration
// 3.設(shè)置動(dòng)畫類型
animation.type = type.rawValue
// 4.設(shè)置動(dòng)畫方向
animation.subtype = direction.rawValue
// 5.
self.window?.layer.addAnimation(animation, forKey: nil)
}
//調(diào)用方法
func btnAction() {
// 添加轉(zhuǎn)場(chǎng)動(dòng)畫
self.view.addTransitionAnimation(0.4, type: TransitionType.cube, direction: TransitionDirection.FromRight)
self.dismissViewControllerAnimated(false, completion: nil)
}
容器視圖控制器
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.greenColor()
// 1.創(chuàng)建視圖控制器對(duì)象
let second = SecondViewController()
// 2.將second作為當(dāng)前控制器的子視圖控制器
// 當(dāng)前視圖控制器就可以將second的view屬性當(dāng)成一般的視圖去使用
self.addChildViewController(second)
// a.將second的view作為子視圖添加到當(dāng)前界面上
self.view.addSubview(second.view)
// b.設(shè)置second的view的frame(默認(rèn)坐標(biāo)是(0,0),大小是屏幕的大小)
// second.view.frame = CGRectMake(100, 0, self.view.bounds.width, self.view.bounds.height)
}
//下一個(gè)頁面的類的代碼
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.orangeColor()
// 添加按鈕點(diǎn)擊按鈕時(shí)側(cè)滑
let btn = UIButton.init(frame: CGRectMake(20, 50, 100, 50))
btn.setTitle("頭像", forState: .Normal)
btn.addTarget(self, action: "btnAction:", forControlEvents: .TouchDown)
self.view.addSubview(btn)
}