UI第一周
UIView及window
UIKit是所有控件的所在庫文件
在AppDelegate.swift文件中@UIApplicationMain表示為調(diào)用了oc中的UIApplicationMain中的函數(shù)拍埠。UIApplicationMain作為程序的入口, 1.他會創(chuàng)建一個UIApplication對象, 代表當(dāng)前程序, 用來檢測當(dāng)前應(yīng)用狀態(tài)的改變
2.創(chuàng)建一個遵守UIApplicationDelegate的協(xié)議的類的對象作為UIApplication的代理, 用來處理應(yīng)用程序狀態(tài)的改變
在AppDelegate中, 當(dāng)程序啟動成功后會自動調(diào)用,application這個方法。在這個方法中一般我們會去創(chuàng)建一個window, 如果沒有創(chuàng)建window系統(tǒng)將會自動去story.board中去創(chuàng)建一個window酝蜒。
window:UIView
window:窗口, 在一個系統(tǒng)中應(yīng)用想要展示在屏幕上至少需要有一個window, 對于手機(jī)來說, 一般只有一個window。應(yīng)用程序的所以節(jié)目內(nèi)容都是展示在窗口上的。
UIView, 是IOS中所有視圖(控件)的直接或者間接的父類, 所以它的屬性和方法它的子類同樣擁有瞳收。
創(chuàng)建一個UIView對象的必要的兩個方法
1.必須設(shè)置它的frame屬性, CGPoint(坐標(biāo)), CGSizw(寬和高)
2.必須將視圖添加到界面上
let view = UIView(frame: CGRectMake(0, 0, 100, 100)) //也創(chuàng)建的時候不設(shè)置frame, 后面來設(shè)置它的frame屬性。
self.view.addSubview(view)
設(shè)置顏色
擁有3種方法來設(shè)置顏色
view.backgroundColor = UIColor.redColor() //方式一, 系統(tǒng)方法設(shè)置
view.backgroundColor = UIColor.init(red: 0.3, green: 0.3, blue: 0.3, alpha: 1) //方式二, 通過三原色來確定顏色, alpha表示透明度<0~1>, 1表示不透明
view.backgroundColor = UIColor.init(White: 0.7, alpha: 1) //方式三, 創(chuàng)建灰色
Frame的相關(guān)屬性
1.frame
視圖的坐標(biāo)和大小
2.center
視圖的中心點, 可以通過改變視圖的center來改變視圖的坐標(biāo)
3.bounds
視圖的坐標(biāo)和大小, 默認(rèn)情況下bounds的坐標(biāo)為(0,0),大小和frame一致厢汹。改變bounds的坐標(biāo)在當(dāng)前視圖上不沒有任何影響, 但是會影響添加到當(dāng)前視圖上的子視圖的坐標(biāo), 一般情況下不去修改它
4.transform
a.縮放
view.transform = CGAffineTransformMakeScale(0.5, 0.5) //兩個參數(shù)為x, y上的縮放比例
b.平移
view.transform = CGAffineTransformMakeTranslation(300, 300) //兩個參數(shù)為在x, y上的平移距離, 負(fù)數(shù)為反方向
c.旋轉(zhuǎn)
view.transform = CGAffineTransformMakeRotation(M_PI) // 旋轉(zhuǎn)的弧度
d.多個形變同時發(fā)生
view.transform = CGAffineTransformTranslate(CGAffineTransformMakeScale(0.5, 0.5), 300, 300) //在縮放的情況下, 再進(jìn)行平移
e.組合2個形變
view.transform = CGAffinetransformConcat(trans1, trans2) //組合trans1, trans2, 2個形變
父子視圖關(guān)系
每當(dāng)代碼中調(diào)用了一次addSubView方法時, 系統(tǒng)就會存在父子視圖關(guān)系螟深。
1.一個視圖只能有一個父視圖
將一個視圖添加到2個視圖上時, 只有最后一次添加有效
2.獲取一個視圖的父視圖
let superView = redView.superview
3.獲取一個視圖的所有子視圖
let subviews = redView.subviews
4.使用iskindofClass方法判斷類型
if item.iskindofClass(UIView.self) {
print("類型為UIView")
}
5.tag值
每個視圖都擁有一個tag值屬性, 用來判斷視圖
redView.tag = 100
6.通過tag值拿到視圖
let redView = self.view.viewWithTag(100)
7.將視圖放到最上層
self.view.bringSubViewToFront(redView)
8.將視圖放到最下層
self.view.sendSubviewToback(redView)
9.將一個視圖插入到另一個視圖的上面
self.view.insertSubView(view2, aboveSubview: view3)
10.將一個視圖插入到另一個視圖的下面
self.view.insertSubview(view2, belowSubview: view3)
UIView動畫
共需要了解3種動畫方法
1.參數(shù)1表示動畫時間, 參數(shù)2表示一個閉包, 表示動畫結(jié)束時視圖的狀態(tài)
UIView.animateWithDuration(2) {
//在這兒來設(shè)置視圖動畫結(jié)束時的狀態(tài)
//動畫的改變視圖的坐標(biāo)
self.subView.frame.origin.y = 50
}
2.增加一個閉包, 里面需要寫上完成動畫后需要做的事情
func UIViewAnimation2(){
//參數(shù)3:整個動畫完成后會自動調(diào)用這個閉包
UIView.animateWithDuration(2, animations: {
self.subView.transform = CGAffineTransformMakeTranslation(0, -200)
}) { (_) in
//寫動畫結(jié)束后需要執(zhí)行的代碼
UIView.animateWithDuration(2, animations: {
self.subView.transform = CGAffineTransformMakeTranslation(0, 0)
})
}
}
3.方式3
func UIViewAnimation3() {
//參數(shù)1:動畫時間
//參數(shù)2:延遲時間
//參數(shù)3:選項,Repeat->動畫重復(fù)執(zhí)行,Autoreverse->自動回到動畫開始的狀態(tài)
//參數(shù)4:設(shè)置動畫結(jié)束時視圖狀態(tài)的閉包
//參數(shù)5:整個動畫過程完成后需要執(zhí)行的閉包
UIView.animateWithDuration(2, delay: 1, options: [ .Repeat, .Autoreverse], animations: {
self.subView.transform = CGAffineTransformMakeTranslation(0, -200)
}, completion: nil)
}
4.方式4
func UIViewAnimation4() {
//參數(shù)1:動畫時間
//參數(shù)2:延遲時間
//參數(shù)3:彈簧的壓力系數(shù)
//參數(shù)4:彈簧初始的加速度
//參數(shù)5:選項
//參數(shù)6:設(shè)置動畫結(jié)束時視圖的狀態(tài)
//參數(shù)7:動畫結(jié)束后要執(zhí)行的閉包
UIView.animateWithDuration(2, delay: 1, usingSpringWithDamping: 0.1, initialSpringVelocity: 0, options: [ .Repeat, .Autoreverse], animations: {
//注意:對于有圓角的視圖,改變大小而又不影響形狀烫葬,只能通過形變?nèi)タs放界弧。不能直接改變frame中的size
self.subView.transform = CGAffineTransformMakeScale(0.5, 0.5)
}, completion: nil)
}
UIImageView幀動畫
幀動畫屬于UIImageView.繼承于UIView, 繼承UIView的方法和屬性
UIImageView擁有專有屬性, image屬性凡蜻。擁有2中方式給image賦值
1.通過圖片名字創(chuàng)建, 對象在程序結(jié)束后才會銷毀, 只會創(chuàng)建一次.
imageView.image = UIImage(name:"back.jpg")
2.通過圖片路徑去創(chuàng)建, 當(dāng)當(dāng)前對象不再使用的時候, 就會銷毀
lst path = NSBundle.mainBundle().PathForResourse("back", ofType"jpg")
imageView.image = UIImage(contentOfFile: "path")
內(nèi)容模式
imageView.contentMode = .center // 最常用.ScaleToFill
播放幀動畫
1.設(shè)置幀動畫數(shù)組
2.設(shè)置動畫時間
3.設(shè)置重復(fù)次數(shù)
4.開始動畫
5.結(jié)束動畫
var imageArray = [UIImage]()
if button.tag == 1 {
for item in 1...3 {
let image = UIImage(named: "player_up_\(item)")
imageArray.append(image!)
}
firstImageView.animationImages = imageArray
firstImageView.animationDuration = 0.1
firstImageView.animationRepeatCount = 2 // 當(dāng)重復(fù)次數(shù)為0時, 幀動畫無限循環(huán)
firstImageView.startAnimating()
firstImageView.stopAnimating()
定時器
參數(shù)1:定時時間, 參數(shù)2: 調(diào)用方法的對象, 參數(shù)3: 存儲時間到了調(diào)用的方法, 可以帶參數(shù)也可以不帶參數(shù), 但是帶的參數(shù)必須為定時器類型, 有且只有一個。
參數(shù)4: 給當(dāng)前NSTimer的userInfo屬性賦的值, 參數(shù)5:是否重復(fù)
NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "move4:", userInfo: nil, repeats: true)
//暫停定時器
timer.fireDate = NSDate.distantFuture()
//繼續(xù)定時器
timer.fireDate = NSDate.distantPast()
UILabel
繼承自UIVIew, 用來顯示文字的控件
UILabel基本屬性
//1.創(chuàng)建UILabel對象
let label = UILabel.init(frame: CGRectMake(100, 100, 200, 300))
//2.添加到界面上
self.view.addSubview(label)
//3.設(shè)置背景顏色
label.backgroundColor = UIColor.yellowColor()
//========UILabel的專有屬性=========
//1.text屬性
//設(shè)置label上顯示的文字
label.text = "hello world 你好世界"
//拿到label上當(dāng)前顯示的文字
print(label.text)
//2.設(shè)置字體(字體大小默認(rèn)是:17)
//使用系統(tǒng)字體垢箕,設(shè)置字體大小
label.font = UIFont.systemFontOfSize(17)
//使用系統(tǒng)字體划栓,設(shè)置字體大小和粗細(xì)
//參數(shù)1:字體大小
//參數(shù)2:字體粗細(xì)(小于1)
label.font = UIFont.systemFontOfSize(17, weight: 0.2)
//使用系統(tǒng)黑體,設(shè)置字體大小
label.font = UIFont.boldSystemFontOfSize(17)
//使用系統(tǒng)斜體条获,設(shè)置字體大小
label.font = UIFont.italicSystemFontOfSize(17)
//獲取系統(tǒng)所有字體的字體名
//75
print(UIFont.familyNames(),UIFont.familyNames().count)
//參數(shù)1:字體名
//參數(shù)2:字體大小
label.font = UIFont.init(name: "FZJKai-Z03S", size: 17)
//總結(jié)使用自己的字體的步驟:
//1.將ttf文件拖到工程中
//2.在info.plist文件中添加鍵值對"Fonts provided by application",將字體添加到系統(tǒng)字體庫中
//3.通過提供字體名的構(gòu)造方法去創(chuàng)建字體(先要找到自己添加的字體的字體名)
//3.設(shè)置文字顏色
label.textColor = UIColor.redColor()
//4.設(shè)置陰影顏色
label.shadowColor = UIColor.blackColor()
//5.設(shè)置陰影的偏移效果
label.shadowOffset = CGSizeMake(-1, -1)
//6.設(shè)置文字的對齊模式(默認(rèn)是左對齊)
//Center -> 居中
//Right -> 右對齊
//Left -> 左對齊
label.textAlignment = .Left
label.text = "default is NSLineBreakBy TruncatingTail used for single and multiple lines of text 這樣就用代碼實現(xiàn)了label的創(chuàng)建忠荞,其中initWithFrame設(shè)置了label的位置還有大小,奧斯卡的積分換空間和"
//7.設(shè)置行數(shù)
label.numberOfLines = 5
//自動換行
label.numberOfLines = 0
//8.設(shè)置換行模式
//ByWordWrapping -> 以單詞為單位換行
//ByCharWrapping -> 以字符為單位換行
//...
label.lineBreakMode = .ByCharWrapping
根據(jù)文字設(shè)置label大小
//需要顯示在label上的文字
let str = "收到回復(fù)asdfsajfdkl刷卡費上"
//計算顯示指定文字所需要的最小空間
//1.將swift的字符串轉(zhuǎn)換成OC的字符串
let ocStr = str as NSString
//2.計算字符串的大小
//參數(shù)1:限制顯示當(dāng)前字符串的最大寬度和最大高度
//參數(shù)2:設(shè)置渲染方式(UsesLineFragmentOrigin)
//參數(shù)3:確定文字的字體(大小)
//NSFontAttributeName ->字體對應(yīng)的key值
//NSForegroundColorAttributeName -> 文字顏色對應(yīng)的key值
let strSize = ocStr.boundingRectWithSize(CGSizeMake(100, 1000000000), options: .UsesLineFragmentOrigin, attributes: [NSFontAttributeName:UIFont.systemFontOfSize(17)], context: nil).size
print(strSize)
//3.創(chuàng)建label顯示文字
let label = UILabel.init(frame: CGRectMake(100, 100, strSize.width, strSize.height))
label.backgroundColor = UIColor.yellowColor()
self.view.addSubview(label)
label.text = str
//自動換行
label.numberOfLines = 0
UIButton
基本屬性方法介紹
UIButton:UIControl: UIView
繼承自UIControl的類都具有點擊事件, target方法
//1.創(chuàng)建一個按鈕對象
let btn1 = UIButton.init(frame: CGRectMake(100, 300, 100, 50))
self.view.addSubview(btn1)
//2.設(shè)置title
btn1.setTitle("標(biāo)題", forState: .Normal)
btn1.setTitleColor(UIColor.whiteColor(), forState: .Normal)
//3.設(shè)置圖片
//btn1.setImage(UIImage.init(named: "luffy1"), forState: .Normal)
btn1.setBackgroundImage(UIImage.init(named: "luffy2"), forState: .Normal)
//4.添加事件
btn1.addTarget(self, action: "btnAction:", forControlEvents: .TouchUpInside)
//5.按鈕點擊方法
func btnAction(btn:UIButton) {
//CGFloat(arc4random()%256)/255
//設(shè)置按鈕的背景顏色是隨機(jī)色
btn.backgroundColor = UIColor.init(red: CGFloat(arc4random()%256)/255, green: CGFloat(arc4random()%256)/255, blue: CGFloat(arc4random()%256)/255, alpha: 1)
//MARK: - 圖片按鈕
func imageButton() {
//1.創(chuàng)建一個按鈕對象
let imageBtn = UIButton.init(frame: CGRectMake(100, 200, 80, 80))
//2.添加到界面上
self.view.addSubview(imageBtn)
//3.設(shè)置圖片
//參數(shù)1:圖片
//參數(shù)2:狀態(tài)(正常帅掘、高亮委煤、選中、不可用)
imageBtn.setImage(UIImage.init(named: "luffy1"), forState: .Normal)
//4.添加按鈕點擊事件
imageBtn.addTarget(self, action: "btnAction:", forControlEvents: .TouchUpInside)
//MARK: - 文字按鈕
func titleButton() {
//UIButton:UIControl:UIView
//UIButton上有一個titleLabel專門負(fù)責(zé)按鈕上文字的顯示修档;有一個imageView專門負(fù)責(zé)按鈕上圖片的顯示
//=========UIView的屬性和方法========
//1.創(chuàng)建UIButton對象
let titleBtn = UIButton.init(frame: CGRectMake(100, 100, 100, 50))
//2.添加到界面上
self.view.addSubview(titleBtn)
//3.設(shè)置背景顏色
titleBtn.backgroundColor = UIColor.redColor()
//=========UIButton專有的屬性和方法======
//1.設(shè)置按鈕上顯示的文字(給不同的狀態(tài)設(shè)置不一樣的文字)
//參數(shù)1:想要在按鈕上顯示的文字
//參數(shù)2:狀態(tài)
//Normal -> 正常狀態(tài)(按鈕正常顯示碧绞,沒有被點擊或者按下的時候)
//Highlighted ->高亮(按鈕被按下,沒有彈起來的時候的狀態(tài))
//Selected -> 選中狀態(tài)
//Disabled -> 不可用狀態(tài)(按鈕不能被點擊)
titleBtn.setTitle("正常", forState: .Normal)
titleBtn.setTitle("高亮", forState: .Highlighted)
titleBtn.setTitle("選中", forState: .Selected)
titleBtn.setTitle("不可用", forState: .Disabled)
//2.設(shè)置當(dāng)前按鈕是否選中(默認(rèn)是false->非選中)
titleBtn.selected = false
//3.設(shè)置當(dāng)前按鈕是否可用(默認(rèn)是true->可用)
titleBtn.enabled = true
//4.設(shè)置文字顏色(給不同的狀態(tài)設(shè)置不一樣的顏色)
titleBtn.setTitleColor(UIColor.yellowColor(), forState: .Normal)
titleBtn.setTitleColor(UIColor.lightGrayColor(), forState: .Disabled)
//注意:按鈕上的文字和文字顏色吱窝,必須通過對應(yīng)的set方法去根據(jù)狀態(tài)去設(shè)置讥邻。其他和文字相關(guān)的屬性可以通過拿到titleLabel去設(shè)置
//5.設(shè)置按鈕上的文字字體
titleBtn.titleLabel?.font = UIFont.systemFontOfSize(12)
//6.設(shè)置按鈕上的文字的對齊方式
titleBtn.titleLabel?.textAlignment = .Right
//!!!7.給按鈕添加事件
//參數(shù)1:調(diào)用方法的對象
//參數(shù)2:指定事件發(fā)生后參數(shù)1要去調(diào)用的方法(這個方法可以不帶參,如果帶參只能帶一個癣诱,并且參數(shù)的類型是UIButton類型)计维,實參就是當(dāng)前添加事件的按鈕本身
//參數(shù)3:事件
//TouchDown -> 按下事件
//功能:當(dāng)按鈕被按下的時候袜香,self會去調(diào)用btnAction方法
//TouchUpInside ->按下彈起事件
//功能:當(dāng)按鈕被按下彈起來的時候撕予,self會去調(diào)用btnAction方法
titleBtn.addTarget(self, action: "btnAction:", forControlEvents: .TouchUpInside)
}
自定制Button
需求: 圖片占button的4/5, 文字占button的1/5。
//圖片高度是整個按鈕高度的4/5;文字高度是整個按鈕高度的1/5
//功能:設(shè)置button上的imageView的坐標(biāo)和大小
//參數(shù)1:當(dāng)前按鈕的范圍(只需要大小)
//返回值:重新設(shè)置的圖片的坐標(biāo)和大小
override func imageRectForContentRect(contentRect: CGRect) -> CGRect {
let x:CGFloat = 0
let y:CGFloat = 0
let w:CGFloat = contentRect.size.width
let h:CGFloat = contentRect.size.height * 4 / 5
return CGRectMake(x, y, w, h)
}
//功能:設(shè)置button上的titleLabel的坐標(biāo)和大小
//參數(shù)1:當(dāng)前按鈕的范圍(只需要大小)
//返回值:重新設(shè)置的文字的坐標(biāo)和大小
override func titleRectForContentRect(contentRect: CGRect) -> CGRect {
let x:CGFloat = 0
let y:CGFloat = contentRect.size.height * 4 / 5
let w = contentRect.size.width
let h = contentRect.size.height / 5
return CGRectMake(x, y, w, h)
}
UITextFile
可以輸入的文本框, 可以通過text拿到文本上的文字, 也可以設(shè)置文本框上的文字
//UITextField:UIControl:UIView
//===========UIView的屬性和方法=======
//1.創(chuàng)建UITextField對象
let textField = UITextField.init(frame: CGRectMake(100, 100, 200, 50))
//2.添加到界面上
self.view.addSubview(textField)
//3.設(shè)置背景顏色
textField.backgroundColor = UIColor.yellowColor()
//4.再創(chuàng)建一個UITextField對象
let textField2 = UITextField.init(frame: CGRectMake(100, 200, 200, 50))
self.view.addSubview(textField2)
textField2.backgroundColor = UIColor.yellowColor()
textField2.delegate = self
//=====textField的專有屬性和方法====
//(1)文字相關(guān)
//1.text屬性
//設(shè)置文本輸入框的內(nèi)容
textField.text = "啊哈哈哈"
//拿到文本輸入框的內(nèi)容
print(textField.text)
//2.文字顏色
textField.textColor = UIColor.brownColor()
//3.設(shè)置文字字體
textField.font = UIFont.systemFontOfSize(14)
//4.設(shè)置占位文字(在輸入框的內(nèi)容為空的時候就會顯示出來)
textField.placeholder = "請輸入賬號"
//5.設(shè)置文本的對齊方式(默認(rèn)是:左對齊)
textField.textAlignment = .Left
//6.密文顯示(默認(rèn)是false)
textField.secureTextEntry = true
//(2)顯示相關(guān)
//1.設(shè)置文本框的樣式
textField.borderStyle = .RoundedRect
//2.設(shè)置清除按鈕模式(清除按鈕實質(zhì)是rightView)
//(前提是輸入框中有文字)
//Always -> 一直顯示
//Never -> 從不顯示(默認(rèn))
//WhileEditing -> 當(dāng)文本輸入框處于編輯狀態(tài)的時候才顯示
//UnlessEditing ->當(dāng)文本輸入框處于非編輯狀態(tài)的時候才顯示
//注:當(dāng)文本輸入框中有光標(biāo)的時候就是處于編輯狀態(tài)
textField.clearButtonMode = .Always
//3.左視圖
let leftImageView = UIImageView.init(frame: CGRectMake(0,0, 40,40))
leftImageView.image = UIImage.init(named: "luffy1")
//設(shè)置左視圖
textField.leftView = leftImageView
//設(shè)置左視圖的顯示模式(確定什么時候顯示蜈首,默認(rèn)是從不顯示)
textField.leftViewMode = .Always
//4.右視圖
//當(dāng)右視圖顯示的時候实抡,清除按鈕不能顯示
/*
let rightLabel = UILabel.init(frame: CGRectMake(0, 0, 40, 40))
rightLabel.text = "你好"
textField.rightView = rightLabel
textField.rightViewMode = .Always
*/
//(3)鍵盤相關(guān)
//1.設(shè)置鍵盤上返回按鈕的樣式
textField.returnKeyType = .Search
//2.鍵盤樣式
textField.keyboardType = .Default
//3.設(shè)置自定義的鍵盤
//自定義的鍵盤,只有高度有效欢策。寬度是屏幕的寬度
let myInputView = UIView.init(frame: CGRectMake(0, 0, 0, 256))
myInputView.backgroundColor = UIColor.redColor()
//textField.inputView = myInputView
//4.設(shè)置子鍵盤
let accessoryView = UIView.init(frame: CGRectMake(0, 0, 0, 50))
accessoryView.backgroundColor = UIColor.greenColor()
textField.inputAccessoryView = accessoryView
//(4)設(shè)置代理
//textField ->委托
//self -> 代理
textField.delegate = self
UITextField協(xié)議方法
extension ViewController: UITextFieldDelegate{
//6.當(dāng)按鍵盤上的返回按鈕的時候吆寨,會自動調(diào)用
func textFieldShouldReturn(textField: UITextField) -> Bool{
print("返回按鈕被點擊")
//收起鍵盤(結(jié)束編輯)
//1.放棄第一響應(yīng)者
textField.resignFirstResponder()
//2.直接讓指定的textField結(jié)束編輯
textField.endEditing(true)
//3.讓self.view上的所有的子視圖都結(jié)束編輯
self.view.endEditing(true)
return true
}
//5.當(dāng)點擊textField彈出來的鍵盤上的按鍵的時候會自動調(diào)用這個方法
//參數(shù)1:委托
//參數(shù)2:當(dāng)前輸入的字符所在的位置
//參數(shù)3:當(dāng)前輸入的字符串(在鍵盤上按的鍵的值)
//返回值:是否可改變textField的text屬性();false -> 按鍵盤上的按鍵無效
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool{
print(range)
print(string)
if string == "0" {
print("進(jìn)入秘密頁")
}
return true
}
//4.當(dāng)文本輸入框已經(jīng)結(jié)束編輯的時候會自動調(diào)用這個方法
func textFieldDidEndEditing(textField: UITextField){
print("已經(jīng)結(jié)束編輯")
}
//2.當(dāng)文本輸入框已經(jīng)開始編輯的時候會自動調(diào)用這個方法
func textFieldDidBeginEditing(textField: UITextField){
print(textField.text)
print("已經(jīng)開始編輯")
}
//3.當(dāng)文本輸入框?qū)⒁Y(jié)束編輯的時候會自動調(diào)用這個方法
//返回:設(shè)置當(dāng)前的textField是否可以結(jié)束編輯(默認(rèn)是true)
func textFieldShouldEndEditing(textField: UITextField) -> Bool{
print("將要結(jié)束編輯")
//要求文本輸入框中的文字長度要大于等于8的時候才能結(jié)束編輯
if textField.text?.characters.count >= 8 {
return true
}
return false
}
//1.在textField將要開始編輯的時候會自動調(diào)用
//參數(shù):當(dāng)前這個協(xié)議對應(yīng)的委托
//返回值:設(shè)置當(dāng)前的textField是否可以進(jìn)行編輯(默認(rèn)是true)
func textFieldShouldBeginEditing(textField: UITextField) -> Bool{
print("將要開始編輯")
return true //false ->讓textField不能進(jìn)行編輯
}
}
控件大集合
//1.開關(guān)
func creatSwitch() {
//1.創(chuàng)建開關(guān)對象
//UISwitch:UIControl:UIView
let sw = UISwitch.init(frame: CGRectMake(100, 100, 100, 50))
//2.添加到界面上
self.view.addSubview(sw)
//3.核心屬性:開關(guān)狀態(tài)(默認(rèn)是:關(guān))
//設(shè)置開關(guān)的狀態(tài)
sw.on = true //false -> 關(guān)
sw.setOn(false, animated: true)
//拿到當(dāng)前的狀態(tài)
print(sw.on)
//4.核心方法:
//參數(shù)1:調(diào)用方法的對象
//參數(shù)2:指定的事件發(fā)生后參數(shù)1要去調(diào)用的方法對應(yīng)的selector
//參數(shù)3:事件
//功能:當(dāng)開關(guān)的值(開關(guān)的狀態(tài))發(fā)生改變的時候,self會去調(diào)用switchAction方法
sw.addTarget(self, action: "switchAction:", forControlEvents: .ValueChanged)
//5.設(shè)置開關(guān)開的狀態(tài)的顏色(默認(rèn)是綠色)
sw.onTintColor = UIColor.redColor()
//6.開關(guān)關(guān)閉的時候的邊框顏色
sw.tintColor = UIColor.purpleColor()
//7.設(shè)置開關(guān)上的滑塊的顏色
sw.thumbTintColor = UIColor.yellowColor()
}
//2.滑條
func creatSlider() {
//1.創(chuàng)建滑條對象
//UISlider:UIControl:UIView
let slider = UISlider.init(frame: CGRectMake(100, 160, 200, 20))
//2.添加到界面上
self.view.addSubview(slider)
//3.核心屬性:值
//value:滑塊的位置對應(yīng)的值(默認(rèn)是0~1)
slider.value = 0.5
//最小值和最大值
slider.minimumValue = 0
slider.maximumValue = 100
//4.核心方法
slider.addTarget(self, action: "sliderAction:", forControlEvents: .ValueChanged)
//7.是否連續(xù)改變
slider.continuous = false
//5.和顏色相關(guān)的屬性
//6.和圖片相關(guān)的屬性
}
//3.步進(jìn)器
func creatStepper() {
//1.創(chuàng)建步進(jìn)器對象
let stepper = UIStepper.init(frame: CGRectMake(100, 200, 100, 50))
//2.添加到界面上
self.view.addSubview(stepper)
//3.核心屬性:值
//當(dāng)前值
stepper.value = 1
print(stepper.value)
//最小值和最大值
stepper.minimumValue = 0
stepper.maximumValue = 10
//步進(jìn)(每按一下加或者減踩寇,增加/減少的值)
stepper.stepValue = 1 //步進(jìn)值必須大于0
//4.核心方法
stepper.addTarget(self, action: "stepperAction:", forControlEvents: .ValueChanged)
//5.設(shè)置值是否連續(xù)改變(按住不放的時候)
stepper.continuous = false
//6.設(shè)置是否重復(fù) false->按住不放的時候不計數(shù)啄清;true->按住不放的時候計數(shù)(默認(rèn))
stepper.autorepeat = false
//7.設(shè)置填充顏色
stepper.tintColor = UIColor.redColor()
}
//4.進(jìn)度條
func creatProgress() {
//1.創(chuàng)建進(jìn)度條對象
//UIProgressView : UIView
let progress = UIProgressView.init(frame: CGRectMake(100, 300, 200, 20))
progress.tag = 100
//2.添加到界面上
self.view.addSubview(progress)
//3.核心屬性
//進(jìn)度:0~1
//設(shè)置當(dāng)前進(jìn)度
progress.progress = 0.5
progress.setProgress(0.6, animated: true)
//4.顏色相關(guān)
//5.圖片相關(guān)
}
//5.活動指示器
func creatActivity() {
//1.創(chuàng)建活動指示器對象
//UIActivityIndicatorView : UIView
let activity = UIActivityIndicatorView.init(frame: CGRectMake(100, 360, 50, 50))
//2.添加到界面上
self.view.addSubview(activity)
//3.想要讓活動指示器顯示,必須讓它開始動畫
activity.startAnimating()
//4.停止動畫->活動指示器就會消失
//activity.stopAnimating()
//5.設(shè)置活動指示器的樣式
activity.activityIndicatorViewStyle = .WhiteLarge
}
//6.多段選擇器
func creatSegement() {
//1.創(chuàng)建多段選擇器對象
//UISegmentedControl : UIControl
//參數(shù)1:分段選擇器上的內(nèi)容對應(yīng)的數(shù)組
let segemnet = UISegmentedControl.init(items: ["海賊王","火影忍者","死神"])
segemnet.frame = CGRectMake(100, 400, 200, 50)
//2.顯示在界面上
self.view.addSubview(segemnet)
//3.核心屬性
//a.每個分段上的內(nèi)容 ->通過創(chuàng)建分段選擇器的時候去設(shè)置
//b.當(dāng)前選中的分段的下標(biāo)(從0開始)
segemnet.selectedSegmentIndex = 0
//4.核心方法
segemnet.addTarget(self, action: "segementAction:", forControlEvents: .ValueChanged)
//5.拿到分段選擇的分段數(shù)
print(segemnet.numberOfSegments)
//6.設(shè)置填充顏色
segemnet.tintColor = UIColor.whiteColor()
}
}
//MARK: - 事件響應(yīng)
extension ViewController{
//4.分段選擇器事件
func segementAction(segement:UISegmentedControl) {
print(segement.selectedSegmentIndex)
//拿到當(dāng)前被選中的分段的title
print(segement.titleForSegmentAtIndex(segement.selectedSegmentIndex))
}
//3.步進(jìn)器
func stepperAction(stepper:UIStepper) {
print(stepper.value)
}
//2.滑條
func sliderAction(slider:UISlider) {
print(slider.value)
//拿到進(jìn)度條
let progress = self.view.viewWithTag(100) as! UIProgressView
let t = slider.value/(slider.maximumValue - slider.minimumValue)
progress.setProgress(t, animated: true)
}
//1.開關(guān)事件
func switchAction(sw:UISwitch) {
if sw.on {
print("開關(guān)打開")
}else{
print("開關(guān)關(guān)閉")
}
}
警告框俺孙、文本視圖和表單視圖
//MARK: - 生命周期
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
//1.創(chuàng)建一個表單視圖
//UIAlertController : UIViewController
//參數(shù)1:標(biāo)題
//參數(shù)2:信息
//參數(shù)3:風(fēng)格(ActionSheet->表單,Alert->警告框)
let alterController = UIAlertController.init(title: nil, message: "消息", preferredStyle: .Alert)
//2.添加到界面上
//參數(shù)1:需要顯示的視圖控制
self.presentViewController(alterController, animated: true, completion: nil)
//3.添加選項按鈕
//參數(shù)1:選項對應(yīng)的按鈕上的文字
//參數(shù)2:風(fēng)格
//參數(shù)3:當(dāng)當(dāng)前選項對應(yīng)的按鈕被點擊后會執(zhí)行的代碼對應(yīng)的閉包
let action = UIAlertAction.init(title: "相機(jī)", style: .Default) { (_) in
print("相機(jī)被點擊")
}
//Destructive風(fēng)格
let action2 = UIAlertAction.init(title: "相冊", style: .Destructive) { (_) in
print("相冊被點擊")
}
//Cancel風(fēng)格
let action3 = UIAlertAction.init(title: "取消", style: .Cancel) { (_) in
print("取消")
}
//添加相機(jī)對應(yīng)的action
alterController.addAction(action)
//添加相冊對應(yīng)的action
alterController.addAction(action2)
// alterController.addAction(action3)
}
//屬性:
var textView = UITextView()
//MARK: - textView
override func viewDidLoad() {
super.viewDidLoad()
//1.創(chuàng)建textView對象
//UITextView:UIScrollView : UIView
textView = UITextView.init(frame: CGRectMake(100, 100, 200, 70))
//2.添加到界面上
self.view.addSubview(textView)
//3.設(shè)置背景顏色
textView.backgroundColor = UIColor.yellowColor()
//4.text屬性
textView.text = " default is nil. Can be useful with the appearance proxy on custom UIView subclasses."
//5.設(shè)置是否可以選中和編輯
//默認(rèn)是true -> 可以選中和編輯
textView.selectable = true
//6.是否可以選中刪除所有
textView.clearsOnInsertion = true
//7.其他的屬性和方法參考UITextField
}
//在點擊當(dāng)前頁面的時候會自動調(diào)用這個方法
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//5.獲取選中的范圍
let range = textView.selectedRange
print(range)
}