第一天
視圖控制器
1.視圖控制器的創(chuàng)建
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//!!!1.視圖控制器本身不顯示舀奶,但是每個(gè)視圖控制器都有一個(gè)view的屬性啊楚,專門(mén)用來(lái)負(fù)責(zé)視圖控制器的顯示。想要顯示在視圖控制器上的內(nèi)容,需要添加到它是view屬性上
//2.在使用視圖控制器的時(shí)候,一般不直接使用UIViewController這個(gè)類,而是通過(guò)繼承UIViewController寫(xiě)一個(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)建手寫(xiě)的視圖控制器
let root1 = FirstViewController()
//每個(gè)視圖控制器都有一個(gè)view屬性刊驴,負(fù)責(zé)顯示
root1.view.backgroundColor = UIColor.redColor()
//b.通過(guò)xib去創(chuàng)建視圖控制器
//創(chuàng)建一個(gè)類繼承自UIViewController,創(chuàng)建的時(shí)候?qū)?also creat XIB"選上
//直接用最簡(jiǎn)單的方法創(chuàng)建對(duì)應(yīng)的類的對(duì)象
let root2 = ThirdViewController()
//通過(guò)直接通過(guò)xib創(chuàng)建視圖控制器的方法去創(chuàng)建
//參數(shù)1:xib文件名
let root4 = ThirdViewController(nibName: "ThirdViewController", bundle: nil)
//c.通過(guò)storyboard去創(chuàng)建視圖控制器
//拿到storyboard文件對(duì)應(yīng)的對(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 = root4
return true
}
}
2.視圖控制器的生命周期
import UIKit
//視圖控制器的生命周期:指的是視圖控制器里面的view屬性從創(chuàng)建到消失的過(guò)程
class FirstViewController: UIViewController {
//MARK: - 生命周期
//注意:重寫(xiě)生命周期相關(guān)的方法的時(shí)候必須通過(guò)super去調(diào)用父類的相關(guān)方法
//1.開(kāi)始創(chuàng)建視圖控制器的view的屬性的時(shí)候會(huì)自動(dòng)調(diào)用(創(chuàng)建一個(gè)視圖控制器只會(huì)調(diào)用一次)
override func loadView() {
super.loadView()
print("創(chuàng)建view")
}
//2.view屬性已經(jīng)加載完成之后會(huì)自動(dòng)調(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("下一頁(yè)", 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.viewDidDisappear(animated)
print("view已經(jīng)消失")
}
//接收到內(nèi)存警告的時(shí)候自動(dòng)調(diào)用
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
//MARK: - 按鈕點(diǎn)擊
extension FirstViewController{
func buttonAction() {
//點(diǎn)擊按鈕進(jìn)入下一頁(yè):
//1.創(chuàng)建下一頁(yè)對(duì)應(yīng)的視圖控制器對(duì)象
let second = SecondViewController()
//2.將視圖控制器展示出來(lái)(模態(tài)化彈出下一個(gè)界面)
//參數(shù)1:想要顯示的視圖控制器對(duì)象
//參數(shù)2:是否有動(dòng)畫(huà)效果
//參數(shù)3:界面切換完成后姿搜,想要執(zhí)行的代碼對(duì)應(yīng)的閉包
self.presentViewController(second, animated: true, completion: nil)
}
}
//頁(yè)面切換說(shuō)明:
//回到上一頁(yè)(注意:只有通過(guò)present的形式添加的視圖控制器,才能通過(guò)dismiss方法讓其消失捆憎,并且銷毀)
//參數(shù)1:是否動(dòng)畫(huà)
//參數(shù)2:當(dāng)前界面消失后想要執(zhí)行的代碼對(duì)應(yīng)的閉包
//self.dismissViewControllerAnimated(true, completion: nil)
3.轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
//轉(zhuǎn)場(chǎng)動(dòng)畫(huà)舅柜,就是界面切換的時(shí)候的動(dòng)畫(huà)效果。
//1.添加轉(zhuǎn)場(chǎng)動(dòng)畫(huà)
//a.創(chuàng)建轉(zhuǎn)場(chǎng)動(dòng)畫(huà)對(duì)象
let animation = CATransition.init()
//b.設(shè)置動(dòng)畫(huà)時(shí)間
animation.duration = 0.4
//c.設(shè)置動(dòng)畫(huà)類型
//"rippleEffect"
animation.type = "oglFlip"
//d.設(shè)置動(dòng)畫(huà)方向
animation.subtype = kCATransitionFromRight
//e.添加動(dòng)畫(huà)
//可以通過(guò)任何已經(jīng)顯示在界面上的視圖去拿到當(dāng)前應(yīng)用程序的window(主窗口)
self.view.superview?.layer.addAnimation(animation, forKey: nil)
小技巧:在平時(shí)用的時(shí)候顯然這種方法非常繁瑣躲惰,我們可以通過(guò)繼承UIView寫(xiě)一個(gè)關(guān)于轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的封裝函數(shù)致份,在用的時(shí)候直接調(diào)用其方法,不僅大大減少了工作量础拨,而且使動(dòng)畫(huà)得到了更好地?cái)U(kuò)展氮块。
//自定義視圖切換動(dòng)畫(huà)
//宏
kCATransitionFade 交叉淡化過(guò)渡
kCATransitionMoveIn 新視圖移到舊視圖上面
kCATransitionPush 新視圖把舊視圖推出去
kCATransitionReveal 將舊視圖移開(kāi),顯示下面的新視圖
//字符串
pageCurl 向上翻一頁(yè)
pageUnCurl 向下翻一頁(yè)
rippleEffect 滴水效果
suckEffect 收縮效果绍载,如一塊布被抽走
cube 立方體效果
oglFlip 上下翻轉(zhuǎn)效果
cameraIrisHollowOpen
cameraIrisHollowClose
kCATransitionFromRight;
kCATransitionFromLeft(默認(rèn)值)
kCATransitionFromTop滔蝉;
kCATransitionFromBottom
4.容器視圖控制器
** 用法指南:**使用容器視圖控制器可以實(shí)現(xiàn)視圖的側(cè)滑
import UIKit
class FirstViewController: UIViewController {
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)
}
}
第二天
傳值
1.正向傳值
正向傳值:將上一個(gè)界面的值傳到下一個(gè)界面
方法:
1.在下一個(gè)界面中聲明一個(gè)要傳的值的類型的屬性击儡。例:將上一個(gè)界面(First)中的文本輸入框中的文字傳到下一個(gè)界面(Second)。只需要在下一個(gè)界面(Second)中聲明一個(gè)String類型的屬性
2.上一個(gè)界面彈出下一個(gè)界面的時(shí)候蝠引,通過(guò)屬性去傳值
3.在下一個(gè)界面中去使用傳過(guò)去的值(在生命周期的ViewDidLoad及ViewDidLoad以后調(diào)用的方法中都可以使用)
2.反向傳值
2.1反向傳值(閉包)
用閉包做反向傳值阳谍,就是利用閉包的聲明、實(shí)現(xiàn)和調(diào)用:
1.在下一個(gè)界面中聲明閉包(將要傳的值通過(guò)閉包的參數(shù)來(lái)傳)
2.在上一個(gè)界面中界面跳轉(zhuǎn)到下一個(gè)界面的時(shí)候去實(shí)現(xiàn)閉包
3.在下一個(gè)界面消失的時(shí)候去調(diào)用閉包
//1.***(閉包)屬性***頁(yè)面2
var sendValue: ((String)->Void)? = nil
//2.***實(shí)現(xiàn)閉包***頁(yè)面1
let second = SecondViewController()
second.sendValue = { (value)->Void in
//使用從上一個(gè)界面?zhèn)鱽?lái)的值
self.textFiled.text = value
}
//3.***調(diào)用閉包***頁(yè)面2
self.sendValue!(self.textFiled.text!)
2.2反向傳值(消息中心)
消息中心:相當(dāng)于生活中的廣播站螃概。1.用來(lái)發(fā)送消息矫夯;2.一個(gè)消息中心可以發(fā)送多條消息,每條消息以不同的消息名來(lái)區(qū)分
觀察者:相當(dāng)于收音機(jī)吊洼。1.用來(lái)接收消息训貌;2.能接收到消息的前提:a.消息中心發(fā)送消息,并且是實(shí)時(shí)的 b.觀察者觀察的消息名要和消息中心發(fā)送的消息的消息名保持一致 3.同一個(gè)觀察者可以接收不同的消息
消息:消息中心發(fā)出的內(nèi)容/觀察者接收的內(nèi)容
消息中心做反向傳值:在下一個(gè)界面中使用消息中心發(fā)送消息(消息的內(nèi)容就是要傳的值)冒窍;在上一個(gè)界面注冊(cè)觀察者來(lái)接收消息
//1.使用消息中心發(fā)送消息(消息的內(nèi)容就是要傳的值)
//a.拿到消息中心(單例對(duì)象)NSNotificationCenter.defaultCenter()
//b.發(fā)送消息
//參數(shù)1:消息名(相當(dāng)于頻段)
//參數(shù)2:要發(fā)送的消息的內(nèi)容
NSNotificationCenter.defaultCenter().postNotificationName("nof1", object: self.textField.text)
//2.注冊(cè)成為觀察者
//參數(shù)1:觀察者對(duì)象
//參數(shù)2:消息中心發(fā)送消息的時(shí)候觀察者會(huì)自動(dòng)調(diào)用的方法對(duì)應(yīng)的selector(觀察者接收到消息后會(huì)調(diào)用的方法)-->必須帶一個(gè)參數(shù)旺订,并且參數(shù)的類型是NSNotification
//參數(shù)3:觀察者要觀察的消息的名字
NSNotificationCenter.defaultCenter().addObserver(self, selector: "notificationAction:", name: "nof1", object: nil)
func notificationAction(nof:NSNotification) {
self.textField.text = nof.object as? String
}
//移除觀察者
/*
NSNotificationCenter.defaultCenter().removeObserver(self)
NSNotificationCenter.defaultCenter().removeObserver(self, name: "nof2", object: nil)
*/
2.3反向傳值(單例)
class ValueManager: NSObject {
//1.保證當(dāng)前這個(gè)類只能創(chuàng)建出一個(gè)對(duì)象,而且這個(gè)對(duì)象必須defalutManager去拿到
//拿到當(dāng)前這個(gè)類唯一的對(duì)象
static let defalutManager = ValueManager()
//構(gòu)造方法私有化
private override init() {
}
//2.聲明一個(gè)屬性的類型是需要傳的值的類型
var sendValue = ""
}
//3.拿到單例對(duì)象超燃,并且給屬性賦值
ValueManager.defalutManager.sendValue = self.textField.text!
//4.通過(guò)單例對(duì)象拿到值
self.textField.text = ValueManager.defalutManager.sendValue
第三天
導(dǎo)航控制器
1.導(dǎo)航控制器:
UINavigationController : UIViewController ->UIViewController的屬性和方法UINavigationController都擁有
a.默認(rèn)會(huì)顯示一個(gè)導(dǎo)航條区拳,還有一個(gè)隱藏的工具條;
b.是系統(tǒng)自帶的一個(gè)封裝好的容器視圖控制器意乓,專門(mén)用來(lái)管理其他的視圖控制器樱调。通過(guò)一個(gè)棧結(jié)構(gòu)的容器來(lái)管理。
c.導(dǎo)航控制器上永遠(yuǎn)顯示的是當(dāng)前棧結(jié)構(gòu)中的棧頂元素(最上層的視圖控制器對(duì)象)届良。
d.可以通過(guò)將視圖控制器設(shè)置為導(dǎo)航控制器的根視圖控制器和通過(guò)導(dǎo)航控制器調(diào)用push方法將視圖控制器添加到導(dǎo)航控制器的棧結(jié)構(gòu)中笆凌。
e.通過(guò)導(dǎo)航控制器調(diào)用pop方法將棧結(jié)構(gòu)中的視圖控制器移除
self.window = UIWindow.init(frame: UIScreen.mainScreen().bounds)
self.window?.backgroundColor = UIColor.whiteColor()
//1.創(chuàng)建導(dǎo)航控制器對(duì)象(導(dǎo)航控制器必須有一個(gè)根視圖控制器)
let navC = UINavigationController.init(rootViewController: FirstViewController())
//2.將指定的控制器添加到導(dǎo)航控制器的棧結(jié)構(gòu)中
//push -> 入棧
let second = SecondViewController()
navC.pushViewController(second, animated: true)
//3.1將導(dǎo)航控制器棧結(jié)構(gòu)中的棧頂元素移除
//pop -> 出棧
navC.popViewControllerAnimated(true)
//3.2將當(dāng)前導(dǎo)航控制器對(duì)應(yīng)的棧結(jié)構(gòu)中,除了根視圖以外其他的視圖控制器對(duì)象全部移除
//navC?.popToRootViewControllerAnimated(true)
//3.3將導(dǎo)航控制器對(duì)應(yīng)的棧結(jié)構(gòu)中士葫,指定的視圖控制器對(duì)象前面的所有的視圖控制器移除
//參數(shù)1:指定的視圖控制器
//a.拿到當(dāng)前導(dǎo)航控制器的所有的子視圖控制器(拿到棧結(jié)構(gòu)中的所有的對(duì)象)
let childArray = navC?.childViewControllers
navC?.popToViewController(childArray![1], animated: true)
//4.將導(dǎo)航控制器作為window的根視圖控制器
self.window?.rootViewController = navC
** 2.導(dǎo)航控制器的定制:**
a.navigationBar和navigationItem
navigationBar:寬度是屏幕的寬度乞而,高度是64的導(dǎo)航條(20(狀態(tài)欄的高度)+44) -> 屬于導(dǎo)航控制器
navigationItem:指的是導(dǎo)航條上的內(nèi)容(文字、按鈕慢显、分段選擇器等) -> 屬于讓導(dǎo)航控制器管理的視圖控制器
b.toolBar和toolBarItem
toolBar:默認(rèn)隱藏的爪模,工具條 -> 屬于導(dǎo)航控制器
toolBarItem:指的是工具條上的內(nèi)容 -> 屬于讓導(dǎo)航控制器管理的視圖控制器
navigationBar:整體定義
//定制導(dǎo)航條
func navigationItemSetting() {
//1.定制rightItem
//a.通過(guò)其他視圖來(lái)創(chuàng)建一個(gè)UIBarButtonItem對(duì)象
let btn = UIButton.init(frame: CGRectMake(0, 0, 50, 30))
btn.setTitle("注冊(cè)", forState: .Normal)
btn.setBackgroundImage(UIImage.init(named: "buttonbar_action.png"), forState: .Normal)
btn.setTitleColor(UIColor.greenColor(), forState: .Normal)
btn.addTarget(self, action: "btnAction", forControlEvents: .TouchDown)
let item1 = UIBarButtonItem.init(customView: btn)
//b.通過(guò)一張圖片創(chuàng)建一個(gè)UIBarButtonItem對(duì)象
//參數(shù)1:圖片
//參數(shù)2:item的風(fēng)格
//參數(shù)3:調(diào)用方法的對(duì)象
//參數(shù)4:當(dāng)當(dāng)前item對(duì)應(yīng)的按鈕被點(diǎn)擊后會(huì)調(diào)用的方法
let item2 = UIBarButtonItem.init(image: UIImage.init(named: "itemImage")?.imageWithRenderingMode(.AlwaysOriginal), style: .Plain, target: self, action: "itemAction:")
//c.通過(guò)文字去創(chuàng)建一個(gè)UIBarButtonItem對(duì)象
let item3 = UIBarButtonItem.init(title: "注冊(cè)", style: .Plain, target: self, action: "itemAction:")
//d.通過(guò)系統(tǒng)樣式去創(chuàng)建UIBarButtonItem對(duì)象
//參數(shù)1:指定的系統(tǒng)樣式
let item4 = UIBarButtonItem.init(barButtonSystemItem: .Add, target: self, action: "itemAction:")
//2.設(shè)置navigationBar右邊的顯示
//a.在右邊顯示一個(gè)視圖
self.navigationItem.rightBarButtonItem = item4
//b.在右邊顯示顯示多個(gè)視圖
//self.navigationItem.rightBarButtonItems = [item4,item3]
//3.設(shè)置navigationBar左邊的顯示
self.navigationItem.leftBarButtonItem = item2
//self.navigationItem.leftBarButtonItems = [item1, item2]
//4.設(shè)置navigationBar中間的顯示
//a.顯示純文字
self.title = "登錄"
//b.通過(guò)一個(gè)label顯示文字
let label = UILabel.init(frame: CGRectMake(0, 0, 100, 30))
label.text = "登錄"
label.textColor = UIColor.yellowColor()
label.textAlignment = .Center
//c.創(chuàng)建中間顯示的分段選中器
let segement = UISegmentedControl.init(items: ["海賊","火影"])
segement.frame = CGRectMake(0, 0, 100, 40)
segement.selectedSegmentIndex = 0
//設(shè)置中間視圖
self.navigationItem.titleView = segement
}
//1.替換系統(tǒng)自帶的返回按鈕 -> 設(shè)置leftBarButtonItem屬性
let item2 = UIBarButtonItem.init(barButtonSystemItem: .Done, target: self, action: "itemAction")
//self.navigationItem.leftBarButtonItem = item2
//2.隱藏系統(tǒng)自帶的返回按鈕
self.navigationItem.hidesBackButton = true
toolBar:整體定義
//1.定制toolBar(高度是44)
//toolBar屬于導(dǎo)航控制器,默認(rèn)是隱藏的
//a.讓toolBar顯示出來(lái)
self.toolbarHidden = false
//b.設(shè)置是否有透明度(默認(rèn)true->有透明度)
self.toolbar.translucent = false
//c.設(shè)置背景顏色
self.toolbar.barTintColor = UIColor.yellowColor()
//d.設(shè)置填充顏色
self.toolbar.tintColor = UIColor.redColor()
//2.定制導(dǎo)航條
//a.設(shè)置背景顏色
self.navigationBar.barTintColor = UIColor.blackColor()
//b.設(shè)置填充顏色
self.navigationBar.tintColor = UIColor.whiteColor()
toolBarItem
func toolBarItemSetting() {
//創(chuàng)建toolBar上顯示的按鈕對(duì)應(yīng)的item
let item1 = UIBarButtonItem.init(barButtonSystemItem: .Camera, target: self, action: "toolBarAction")
let item2 = UIBarButtonItem.init(barButtonSystemItem: .Add, target: self, action: "toolBarAction")
let item3 = UIBarButtonItem.init(barButtonSystemItem: .Edit, target: self, action: "toolBarAction")
let item4 = UIBarButtonItem.init(barButtonSystemItem: .Refresh, target: self, action: "toolBarAction")
//a.FlexibleSpace(相當(dāng)于空格荚藻,用來(lái)設(shè)置每個(gè)item之間的間距屋灌,間距是自動(dòng)計(jì)算的)
let space1 = UIBarButtonItem.init(barButtonSystemItem: .FlexibleSpace, target: nil, action: "")
//b.FixedSpace(相當(dāng)于空格,用來(lái)設(shè)置每個(gè)item之間的間距,間距需要手動(dòng)設(shè)置)
let space2 = UIBarButtonItem.init(barButtonSystemItem: .FixedSpace, target: nil, action: "")
//設(shè)置間距
space2.width = 20
//將item添加到toolBar上
self.toolbarItems = [space2,item1,space2,item2,space2,item3,space2,item4,space2]
}
心靈雞湯:將狀態(tài)欄變成白色(默認(rèn)是黑色)
//1.在info.plist文件中去添加一個(gè)鍵值對(duì) -> View controller-based status bar appearance:NO
//2.通過(guò)應(yīng)用程序?qū)ο蟾淖儬顟B(tài)欄的樣式
//a.拿到當(dāng)前應(yīng)用程序?qū)ο?let app = UIApplication.sharedApplication()
//b.設(shè)置狀態(tài)欄的樣式
//LightContent -> 白色
app.statusBarStyle = .LightContent
第四天
標(biāo)簽欄視圖控制器(UITabBarController)
1.UITabBarController基礎(chǔ)
UITabBarController:UIViewController
UITabBarController是一個(gè)容器視圖控制器应狱,專門(mén)用來(lái)管理其他的視圖控制器共郭。如果將視圖控制器交給UITabBarController管理的話,UITabBarController會(huì)自動(dòng)在它的tabBar上創(chuàng)建一個(gè)對(duì)應(yīng)標(biāo)簽,然后每次選中這個(gè)標(biāo)簽的時(shí)候除嘹,界面就會(huì)自動(dòng)切換到這個(gè)視圖控制器
//將視圖控制器交給標(biāo)簽欄控制器管理的方法:
//1.創(chuàng)建window
self.window = UIWindow.init(frame: UIScreen.mainScreen().bounds)
self.window?.backgroundColor = UIColor.whiteColor()
//2.創(chuàng)建標(biāo)簽欄控制器
//a.創(chuàng)建對(duì)象
let tabBarC = YTTabBarController()
//b.將需要交給標(biāo)簽欄控制器管理的視圖控制器對(duì)象創(chuàng)建出來(lái)
let one = OneViewController()
let two = TwoViewController()
let there = ThereViewController()
let four = FourViewController()
//c.將視圖控制器交給標(biāo)簽欄控制器管理
//標(biāo)簽控制器會(huì)自動(dòng)創(chuàng)建每個(gè)視圖控制器對(duì)應(yīng)標(biāo)簽
//注意:標(biāo)簽欄控制器的標(biāo)簽欄上最多能顯示5個(gè)標(biāo)簽写半。如果有超過(guò)5個(gè)子視圖控制器,那么第5個(gè)和超出的視圖控制器的標(biāo)簽會(huì)被"more"標(biāo)簽代替
tabBarC.viewControllers = [one,two,there,four]
//d.設(shè)置默認(rèn)選中的標(biāo)簽
tabBarC.selectedIndex = 2
//3.將標(biāo)簽欄控制器作為window的根視圖控制器
self.window?.rootViewController = tabBarC
2.UITabBarController的定制
UITabBarController的定制分兩個(gè)部分(整體定義即可):
1.tabBar -> 屬于標(biāo)簽欄控制器
2.tabBarItem -> 屬于標(biāo)簽欄控制器管理的視圖控制器
//MARK: - 定制tabBar(高度:49)
func tabBarSetting() {
//1.設(shè)置是否透明(默認(rèn)true->有透明度)
self.tabBar.translucent = true
//2.設(shè)置背景顏色
self.tabBar.barTintColor = UIColor.whiteColor()
//3.設(shè)置背景圖片
//self.tabBar.backgroundImage = UIImage.init(named: "bg")
//4.設(shè)置填充顏色
self.tabBar.tintColor = UIColor.orangeColor()
}
//MARK: - 定制tabBarItem
func tabBarItemSetting() {
//拿到tabBar上所有的tabBarItem對(duì)象
let items = self.tabBar.items
//創(chuàng)建所有的title和圖片名對(duì)應(yīng)的數(shù)組
let titles = ["條漫","繪本","專題","我的"]
let imageNames = ["tiaoman","huiben","zhuanti","wode"]
//設(shè)置item
for (i,item) in items!.enumerate() {
//設(shè)置標(biāo)題
item.title = titles[i]
//設(shè)置正常狀態(tài)的圖片
item.image = UIImage.init(named: imageNames[i]+"_u")?.imageWithRenderingMode(.AlwaysOriginal)
//設(shè)置選中狀態(tài)的圖片
item.selectedImage = UIImage.init(named: imageNames[i]+"_d")?.imageWithRenderingMode(.AlwaysOriginal)
//設(shè)置文字屬性
item.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFontOfSize(13),NSForegroundColorAttributeName:UIColor.lightGrayColor()], forState: .Normal)
item.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFontOfSize(13),NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
}
}
應(yīng)用程序界面框架搭建
說(shuō)明:結(jié)合以上所學(xué)知識(shí)我們就可以進(jìn)行簡(jiǎn)單的應(yīng)用程序界面搭建尉咕,圖示如下:
第五天
UITouch
所有繼承自UIResponder的類(UIView和UIViewController)都可以去重寫(xiě)UITouch的相關(guān)方法污朽,去檢測(cè)視圖的開(kāi)始觸摸、結(jié)束觸摸以及移動(dòng)等觸摸相關(guān)事件
觸摸只有在視圖可以接收用戶交互的時(shí)候才會(huì)有效龙考。UILabel和UIImageView默認(rèn)都是不能進(jìn)行用戶交互
//1 每次開(kāi)始觸摸的時(shí)候會(huì)自動(dòng)調(diào)用
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("開(kāi)始觸摸:\(touches)")
//參數(shù)1:touches->在多個(gè)手指同時(shí)觸摸屏幕的時(shí)候只能獲取到一個(gè)觸摸對(duì)象
//拿到當(dāng)前的觸摸對(duì)象
let touch = touches.first
//拿到當(dāng)前觸摸點(diǎn)的位置
//參數(shù):計(jì)算坐標(biāo)的相對(duì)視圖
let location = touch?.locationInView(self.view)
print(location)
//參數(shù)2:event -> 可以拿到多個(gè)觸摸對(duì)象
let allTouches = event?.allTouches()
print("ALL:\(allTouches?.count)")
//遍歷拿到每個(gè)觸摸對(duì)象
for item in allTouches! {
print(item.locationInView(self.view))
}
//設(shè)置球的坐標(biāo)蟆肆,讓其出現(xiàn)在開(kāi)始觸摸的位置
self.ball.center = location!
}
//2 每次觸摸結(jié)束的時(shí)候會(huì)自動(dòng)調(diào)用
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("觸摸結(jié)束")
}
//3 手指在屏幕上移動(dòng)的時(shí)候會(huì)實(shí)時(shí)調(diào)用
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
print("移動(dòng)")
}
//打開(kāi)label的用戶交互
label.userInteractionEnabled = true
手勢(shì)集合
點(diǎn)擊手勢(shì) - UITapGestureRecognizer
長(zhǎng)按手勢(shì) - UILongPressGestureRecognizer
滑動(dòng)手勢(shì) - UISwipeGestureRecognizer
拖動(dòng)手勢(shì) - UIPanGestureRecognizer
縮放手勢(shì) - UIPinchGestureRecognizer
旋轉(zhuǎn)手勢(shì) - UIRotationGestureRecognizer