Window :
1. UIWindow的父類為UIView.
2. window 窗口郎楼,一個應(yīng)用想要展示在屏幕上死嗦,
至少要有一個window务甥,
一個手機應(yīng)用程序一般只有一個window牡辽。
3.應(yīng)用程序中的所有的界面全部是展示在window上的
一、UIView的基本屬性和方法
1. UIView:是ios中所有視圖(控件)直接/間接的父類敞临;
所以UIView的屬性和方法态辛,對于其它類型的視圖都有效。
2. 視圖:在屏幕上能看到的所有的東西都屬于視圖哟绊。
1.創(chuàng)建UIView的對象
let redView = UIView.init()
說明:
想要將視圖展示在屏幕上的兩個必要條件:
a. 必須設(shè)置坐標和大幸蛎睢(默認坐標是(0,0))票髓,大小(0,0))攀涵;
b. 將視圖添加到已經(jīng)展示在屏幕上的視圖上。
2. 設(shè)置frame屬性
由坐標(x,y)和大小(width,height)兩個部分組成
redView.frame = CGRectMake(10, 10, 100, 100)
** 說明:**
IOS中所有的結(jié)構(gòu)體都有一個對應(yīng)的Make方法來快速的創(chuàng)建一個結(jié)構(gòu)體變量洽沟。
3. 將視圖添加到界面上
self.view.addSubview(redView)
4.設(shè)置背景顏色
說明:視圖的背景顏色默認是透明色
顏色的創(chuàng)建方式:
a. 通過類型方法創(chuàng)建指定顏色
redView.backgroundColor = UIColor.redColor()
**b. **通過三原色來創(chuàng)建顏色
參數(shù)1以故,2,3: 紅裆操、 綠怒详、 藍的值 (0~1)
參數(shù)4: 透明度(0~1)
redView.backgroundColor = UIColor.init(red: 30/255.0, green: 133/255.0, blue: 26/255.0, alpha: 1)
c. 創(chuàng)建灰色
redView.backgroundColor = UIColor.init(white: 0.4, alpha: 1)
練習(xí):創(chuàng)建一個黃色的矩形,顯示在紅色視圖的中點位置踪区,大小是(50昆烁,50)
方式一:
// 創(chuàng)建視圖對象并且設(shè)置frame屬性
let yellowView = UIView.init(frame: CGRectMake(35, 35, 50, 50))
// 添加到界面上
self.view.addSubview(yellowView)
// 設(shè)置
yellowView.backgroundColor = UIColor.yellowColor()
方式二 :
let yellowView = UIView.init(frame: CGRectMake(25, 25, 50, 50))
redView.addSubview(yellowView)
yellowView.backgroundColor = UIColor.yellowColor()
總結(jié):
計算視圖的坐標的時候,注意相對性缎岗。
當(dāng)前視圖被添加到哪個視圖上静尼,
那么當(dāng)視圖的坐標就是相對于誰來算的
二、Frame相關(guān)屬性
我們先來創(chuàng)建一個視圖對象:
//創(chuàng)建一個視圖對象
let redView = UIView.init()
// 添加到界面上
self.view.addSubview(redView)
// 設(shè)置背景顏色
redView.backgroundColor = UIColor.redColor()
1. frame(坐標和大小)
redView.frame = CGRectMake(100, 100, 100, 100)
2. center(中心點坐標)
a. 通過frame和確定視圖的中心點坐標
print(redView.center)
b.可以通過改變center的值传泊,去改變視圖的坐標
redView.center = CGPointMake(200, 200)
print(redView.frame)
3. bounds(坐標和大小)
掌握:
默認情況下bounds的坐標是(0,0),
大小和視圖的frame大小一樣
print(redView.bounds)
說明:
如果改變bounds的坐標鼠渺,不影響當(dāng)前視圖的位置,
但是影響添加到當(dāng)前視圖上的子視圖的坐標眷细,
不影響修改bounds.
redView.bounds = CGRectMake(20, 20, 100, 100)
let yellowView = UIView.init(frame: CGRectMake(10, 10, 40, 40))
yellowView.backgroundColor = UIColor.yellowColor()
redView.addSubview(yellowView)
4. transform(形變)
當(dāng)前視圖發(fā)生形變拦盹,那么添加到當(dāng)前視圖上的所有的視圖會跟著一起形變.
** a. 縮放**
參數(shù)1: x方向上的縮放比例
參數(shù)2: y方向上的縮放比例
redView.transform = CGAffineTransformMakeScale(0.8, 2.5)
b. 旋轉(zhuǎn)
參數(shù): 旋轉(zhuǎn)角度(圓周率對應(yīng)的角度值).
redView.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_4))
c. 平移
參數(shù)1: 在x方向上平移的距離, 負值-> 向左移溪椎,正值->向右移
參數(shù)2: 在y方向上平移的距離普舆, 負值-> 向上移恬口,正值->向下移
redView.transform = CGAffineTransformMakeTranslation(0, 300)
d. 多個形變同時發(fā)生
(1).在另外一個形變的前提下旋轉(zhuǎn)
參數(shù)1: 另外一個形變
redView.transform = CGAffineTransformRotate(CGAffineTransformMakeScale(0.5, 0.5), CGFloat(M_PI_4 / 2))
(2).在另外一個形變的前提下平移
redView.transform = CGAffineTransformTranslate(redView.transform, 0, 300)
(3).在另外一個形變的前提下縮放
//創(chuàng)建一個平移的形變
let transLation = CGAffineTransformMakeTranslation(100, 0)
redView.transform = CGAffineTransformScale(transLation, 0.5, 2)
組合兩個形變
// 旋轉(zhuǎn)形變
let rotate = CGAffineTransformMakeRotation(0.2)
// 平移形變
let transLation1 = CGAffineTransformMakeTranslation(100, 100)
//將旋轉(zhuǎn)形變和平移形變組合
redView.transform = CGAffineTransformConcat(rotate, transLation1)
三、UIView父子視圖
//創(chuàng)建一個UIView對象
let redView = UIView.init(frame: CGRectMake(100, 100, 100, 100))
// 設(shè)置背景顏色
redView.backgroundColor = UIColor.redColor()
// 添加到界面上
self.view.addSubview(redView)
說明:
self.view就是redView的父視圖沼侣,redView就是self.view的子視圖
1.設(shè)置tag值,默認都是0.設(shè)tag值的時候值必須要大于0
解釋:tag的作用是用來區(qū)分界面上不同的視圖
redView.tag = 100
父子視圖的特點和方法
再創(chuàng)建一個黃色視圖
let yellowView = UIView.init(frame: CGRectMake(0, 0, 50, 50))
yellowView.backgroundColor = UIColor.yellowColor()
yellowView.tag = 101
1.說明:
一個視圖只有一個父視圖楷兽,可以有多個子視圖;
連續(xù)將同一個視圖添加到兩個視圖上,最后一次添加有效.
redView.addSubview(yellowView)
self.view.addSubview(yellowView)
2. 獲取一個視圖的父視圖
let superView = redView.superview
superView?.backgroundColor = UIColor.greenColor()
3.獲取一個視圖的所有的子視圖
let subViews = self.view.subviews
print(subViews)
拿到所有的子視圖中的紅色視圖和黃色視圖
// a.
for item in subViews {
// 判斷item是否是UIView類型的华临。如果是就返回true芯杀,如果不是就返回false
if item.isKindOfClass(UIView.self) {
print(item)
}
}
// b.
for item in subViews {
if item.tag == 100 {
print("紅色視圖")
//將紅色視圖的背景顏色變成橙色
item.backgroundColor = UIColor.orangeColor()
}
if item.tag == 101 {
print("黃色視圖\(item)")
}
}
4. 通過tag值拿到指定的子視圖
let subViews2 = self.view.viewWithTag(101)
subViews2?.frame.origin.y = 100
四、 視圖的層次關(guān)系
class ViewController: UIViewController {
// MARK:生命周期方法
override func viewDidLoad() {
super.viewDidLoad()
self.creatUI()
}
// MARK: - 創(chuàng)建界面
func creatUI() {
//再一個視圖上雅潭,添加多個視圖的時候揭厚,子視圖之間如果有公共的部分。那么后添加的子視圖會覆蓋先添加的
// 一般情況下扶供,如果想要將一個視圖顯示在最下面筛圆,最先添加。想要顯示在醉上面就最后添加
// 創(chuàng)建視圖
let view1 = self.creatView(CGRectMake(50, 100, 100, 100), backColor: UIColor.yellowColor() )
let view2 = self.creatView(CGRectMake(100, 150, 100, 100), backColor: UIColor.redColor())
let view3 = self.creatView(CGRectMake(150, 200, 100, 100), backColor: UIColor.greenColor())
//let view4 = self.creatView(CGRectMake(180, 130, 100, 100), backColor: UIColor.purpleColor())
// 2.將指定的視圖放到最上層
self.view.bringSubviewToFront(view2)
// 3.將指定的視圖放到最下層
self.view.sendSubviewToBack(view2)
// 4. 將指定的視圖插入到另一個視圖的上面
self.view.insertSubview(view2, aboveSubview: view3)
// 5. 將指定的視圖插入到另外一個視圖的下面
self.view.insertSubview(view2, belowSubview: view1)
}
// 創(chuàng)建視圖
func creatView(frame:CGRect,backColor:UIColor) -> UIView {
// 創(chuàng)建視圖對象
let subView = UIView.init(frame: frame)
// 設(shè)置背景顏色
subView.backgroundColor = backColor
// 添加到界面上
self.view.addSubview(subView)
// 將創(chuàng)建的視圖對象返回
return subView
}
}
五椿浓、UIView動畫
1.創(chuàng)建視圖
// MARK: - 屬性
let subView = UIView()
let subView2:UIView? = nil
// MARK: - 生命周期
override func viewDidLoad() {
super.viewDidLoad()
// 創(chuàng)建視圖
subView.frame = CGRectMake(0, 0, 100, 100)
subView.center = self.view.center
subView.backgroundColor = UIColor.greenColor()
self.view.addSubview(subView)
// 切圓角(方法的調(diào)用)
self.layerAction()
// 使用動畫(方法的調(diào)用)
self.UIViewAniMation2()
// self.UIViewAniMation2()
// self.UIViewAniMation3()
// self.UIViewAniMation4()
}
2.將矩形切成圓(layer屬性)
layer屬性: 是負責(zé)視圖的形狀(顯示)
定義一個函數(shù)layerAction()太援,用來實現(xiàn)切圓角和設(shè)置邊框。
func layerAction() {
// 1. 切圓角
// 當(dāng)圓角的值為正方形的寬的一半扳碍,就可以切一個圓
self.subView.layer.cornerRadius = 50
// 2. 設(shè)置邊框
self.subView.layer.borderWidth = 3
self.subView.layer.borderColor = UIColor.yellowColor().CGColor
}
3.動畫方法
UIView的動畫使用來動畫的改變frame相關(guān)的屬性提岔、背景顏色、透明度笋敞。
方法1:UIViewAniMation1()
知識點:
UIView.animateWithDuration(NSTimeInterval) { () -> Void in
<#code#>
}
說明:
功能: 執(zhí)行這個方法前視圖的狀態(tài)碱蒙,動畫的切換到閉包里面設(shè)置的最終狀態(tài)
參數(shù)1: 動畫時間(單位:秒)
參數(shù)2: 閉包,設(shè)置動畫結(jié)束時視圖的狀態(tài)
具體代碼實現(xiàn)如下:
func UIViewAniMation1() {
UIView.animateWithDuration(10) {
// 在這兒來設(shè)置視圖動畫結(jié)束時的狀態(tài)
// a. 動畫的改變視圖的坐標
self.subView.frame.origin.y = 50
// b. 動畫的改變視圖的大小
self.subView.frame.size = CGSizeMake(50, 50)
self.subView.transform = CGAffineTransformMakeScale(0.5, 0.5)
// c. 動畫的改變視圖的背景顏色
self.subView.backgroundColor = UIColor.redColor()
// d. 動畫的改變視圖的透明度(0~1)
self.subView.alpha = 0.3
}
}
方法2:UIViewAniMation2()
代碼如下:
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:UIViewAniMation3()
知識點:
UIView.animateWithDuration(<#T##duration: NSTimeInterval##NSTimeInterval#>, delay: <#T##NSTimeInterval#>, options: <#T##UIViewAnimationOptions#>, animations: { () -> Void in
<#code#>
}, completion: <#T##((Bool) -> Void)?##((Bool) -> Void)?##(Bool) -> Void#>)
說明:
參數(shù)1: 動畫時間
參數(shù)2: 延遲時間
參數(shù)3: 選項 - .Repeat -> 動畫重復(fù)執(zhí)行 ;
.Autoreverse ->自動回到動畫開始的狀態(tài).
參數(shù)4: 設(shè)置動畫結(jié)束時視圖狀態(tài)的閉包
參數(shù)5: 整個動畫過程完成后需要執(zhí)行的閉包
方法4:UIViewAniMation4()
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(1, delay: 1, usingSpringWithDamping: 0.1, initialSpringVelocity: 20, options: [ .Repeat, .Autoreverse], animations: {
// 注意:對于有圓角的視圖夯巷。改變大小而又不影響形狀赛惩,只能通過形變?nèi)タs放。不能直接改變frame中的size
self.subView.transform = CGAffineTransformMakeScale(0.5, 0.5)
// self.subView.transform = CGAffineTransformMakeTranslation(0, -200)
}, completion: nil)
}
六趁餐、UILabel基礎(chǔ)
1. 創(chuàng)建Label
1.先要創(chuàng)建一個方法creatLabel()喷兼,要在這個方法里寫代碼。
2.UILabel: UIview -> UIView的屬性和方法后雷,UILabel對象
(1).創(chuàng)建UILabel對象
let label = UILabel.init(frame: CGRectMake(100, 100, 250, 550))
(2).添加到界面上
self.view.addSubview(label)
(3).設(shè)置背景顏色
label.backgroundColor = UIColor.yellowColor()
說明:以上的部分是從UIView繼承下來的屬性
2.UILable專有屬性
1. text屬性 : 設(shè)置label 上顯示的文字.
label.text = "hello,word! 你好季惯,世界!"
2.設(shè)置字體(字體大小默認是17):
使用系統(tǒng)字體,設(shè)置字體大小
label.font = UIFont.systemFontOfSize(17)
使用系統(tǒng)字體喷面,設(shè)置字體大小和粗細
參數(shù)1: 字體大小
參數(shù)2: 字體粗細(小于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)所有字體的字體名
print(UIFont.familyNames(),UIFont.familyNames().count)
使用自己的字體的方法:
- 將ttf文件拖到工程中
- 在into.plist文件中添加鍵值對
“Fonts provided by application”惧辈,
將字體添加到系統(tǒng)字體庫中 - 通過提供字體名的構(gòu)造方法去創(chuàng)建字體
(先要找到自己添加的字體的字體名)
// 參數(shù)1: 字體名
// 參數(shù)2: 字體大小
label.font = UIFont.init(name: "FZJKai-Z03S", size: 17)
設(shè)置文字的顏色
label.textColor = UIColor.blueColor()
設(shè)置陰影顏色
label.shadowColor = UIColor.blackColor()
設(shè)置陰影的偏移效果
label.shadowOffset = CGSizeMake(-1, -1)
設(shè)置文字的對齊模式:
Center -> 居中
Right -> 右對齊
label.textAlignment = .Center
設(shè)置行數(shù)
label.numberOfLines = 5
自動換行
label.numberOfLines = 0
設(shè)置換行模式
ByWordWrapping -> 以單詞為單位換行
ByCharWrapping -> 以字符為單位換行
....
label.lineBreakMode = .ByCharWrapping
根據(jù)文字設(shè)置label 的大小
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// 需要顯示在label上的文字
let str = "短發(fā)是你們心中的女神"
// 計算顯示制定文字所需要的最小空間
// 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(200, 10000000), 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
}
}
七、UIImageView基礎(chǔ)
UIImageView是UIView的子類磕瓷。
從UIView繼承的屬性和方法
// 1. 創(chuàng)建UIImageView對象
let imageView = UIImageView.init(frame: CGRectMake(30, 50, 300, 500))
// 2. 添加到界面上
self.view.addSubview(imageView)
// 3. 設(shè)置背景顏色
imageView.backgroundColor = UIColor.yellowColor()
UIImageView的專有屬性
1. image屬性
a. 通過圖片名去創(chuàng)建一個圖片對象
說明:如果圖片的格式是png盒齿,那么圖片名的后綴可以省略念逞。
但是其他格式的圖片的圖片名的后綴不能省略。
imageView.image = UIImage.init(named: "back2.jpg")
b. 通過圖片路徑去創(chuàng)建一個圖片對象
說明:將文件(除了swift文件)放到工程中边翁,
實質(zhì)是放到了當(dāng)前應(yīng)用程序的包文件中;
想要拿到工程中的圖片路徑先要獲取包文件.
// 拿到包中的指定的文件的路徑
let imagePath = NSBundle.mainBundle().pathForResource("back2", ofType: "jpg")
imageView.image = UIImage.init(contentsOfFile: imagePath!)
c. 比較通過圖片名和通過圖片地址創(chuàng)建圖片對象的兩種方法:
(1). 通過圖片名創(chuàng)建的圖片對象在程序結(jié)束后才會被銷毀翎承,只會創(chuàng)建一次;通過圖片地址創(chuàng)建圖片對象是當(dāng)前對象不再使用的時候就銷毀符匾。
(2). 使用圖片名創(chuàng)建圖片的情況:創(chuàng)建小圖標叨咖;在工程中會重復(fù)使用的圖片
(3). 使用圖片地址創(chuàng)建圖片的情況:不會頻繁的在多個界面出現(xiàn)的大圖
2. 內(nèi)容模式
imageView.contentMode = .ScaleToFill
八、UIImageView 幀動畫
class ViewController: UIViewController {
//MARK: - 屬性
var imageView = UIImageView()
// MARK: - 生命周期
override func viewDidLoad() {
super.viewDidLoad()
self.creatImageView()
// 創(chuàng)建一個定時器啊胶,并且自動開啟
// 參數(shù)1: 定時時間
// 參數(shù)2: 調(diào)用方法的對象
// 參數(shù)3: 存儲定時時間到了以后需要調(diào)用的方法(可以不帶參也可以帶參甸各,但是如果帶參只能帶一個參,并且參數(shù)是NSTimer類型)
// 參數(shù)4: 給當(dāng)前的NSTimer的userInfo屬性賦的值(一般寫nil)
// 參數(shù)5: 是否重復(fù)
// 功能:
NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: "timerAction:", userInfo: "aaa", repeats: true)
}
//定時器方法
// 參數(shù): 當(dāng)前定時器
func timerAction(timer:NSTimer) {
// print("定時器到時")
self.imageView.frame.origin.x += 5
// 判斷小鳥是否飛到了屏幕邊緣
if self.imageView.frame.origin.x >= self.view.bounds.width - self.imageView.bounds.width {
self.imageView.frame.origin.x = 0 - self.imageView.bounds.width
self.imageView.frame.origin.x += 50
// 暫停定時器
// timer.fireDate = NSDate.distantFuture()
// 讓定時器繼續(xù)
// timer.fireDate = NSDate.distantPast()
}
}
func creatImageView() {
// 1. 創(chuàng)建一個UIImageView的對象
// 通過圖片去創(chuàng)建一個imageView
imageView = UIImageView.init(image: UIImage.init(named: "DOVE 1.png"))
// 2. 顯示在界面上
self.view.addSubview(imageView)
// 3. 使用UIImageView播放幀動畫
// a. 設(shè)置幀動畫
// 創(chuàng)建一個空的圖片數(shù)組
var imageArray = [UIImage]()
for item in 1...18 {
// 拼接圖片名
let imageName = "DOVE \(item).png"
// 創(chuàng)建對應(yīng)的圖片
let image = UIImage.init(named: imageName)
// 將圖片存到數(shù)組中
imageArray.append(image!)
}
imageView.animationImages = imageArray
// b. 設(shè)置動畫時間焰坪,單位:秒
imageView.animationDuration = 1
// c. 設(shè)置動畫的重復(fù)次數(shù)(默認是0 ->無限循環(huán))
imageView.animationRepeatCount = 0
// d. 開始動畫
imageView.startAnimating()
}
}
九趣倾、UIButton基礎(chǔ)
1. 圖片按鈕
// 1. 創(chuàng)建一個按鈕對象
func imageButton() {
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)
}
2. 文字按鈕
func titleButton() {
// UIButton: UIControl:UIView
//UIButton上有一個titleLabel專門負責(zé)按鈕上文字的顯示; 有一個imageView專門負責(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è)置按鈕上顯示的文字
// 參數(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)前按鈕是否選中(默認false -> 非選中 )
titleBtn.selected = false
// 3. 設(shè)置當(dāng)前按鈕是否可用(默認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(17)
// 6. 設(shè)置按鈕上的文字的對齊方式
titleBtn.titleLabel?.textAlignment = .Center
// !!! 7. 給按鈕添加事件
// 參數(shù)1: 調(diào)用方法的對象
// 參數(shù)2: 指定事件發(fā)生后參數(shù)1要去調(diào)用的方法(這個方法可以不帶參箱锐,如果帶參只能帶一個)
// 參數(shù)3: 事件
// TouchDown -> 按下事件
// 功能:當(dāng)按鈕被按下的時候,self會去調(diào)用btnAction方法
// TouchUpInside -> 按下彈起事件
// 功能: 當(dāng)按鈕被按下彈起來的時候劳较,self會去調(diào)用btnAction方法
titleBtn.addTarget(self, action: "btnAction:", forControlEvents: .TouchUpInside)
}
// MARK: - 按鈕點擊
func btnAction(btn:UIButton) {
// CGFloat(arc4random()%256) / 255
//設(shè)置按鈕的背景顏色是隨機色
btn.backgroundColor = UIColor.init(red: CGFloat(arc4random()%256) / 255, green: CGFloat(arc4random()%256) / 255, blue: CGFloat(arc4random()%256) / 255, alpha: 1)
}
3.圖片文字按鈕
func imageTitleBtn() {
// a. 同時設(shè)置title和image屬性驹止,顯示是在圖片在左,文字在右
// b. 同時設(shè)置title和groundImage观蜗,顯示是在圖片在下層臊恋,文字在上層
// 1. 創(chuàng)建一個按鈕對象
let btn1 = UIButton.init(frame: CGRectMake(100, 300, 200, 100))
self.view.addSubview(btn1)
//2. 設(shè)置title
btn1.setTitle("標題", forState: .Normal)
btn1.setTitleColor(UIColor.redColor(), forState: .Normal)
// 3. 設(shè)置圖片
//btn1.setImage(UIImage.init(named: "luffy1"), forState: .Normal)
btn1.setBackgroundImage(UIImage.init(named: "luffy4"), forState: .Normal)
// 4. 添加事件
btn1.addTarget(self, action: "btnAction:", forControlEvents: .TouchUpInside)
}
實現(xiàn)方法的調(diào)用
override func viewDidLoad() {
super.viewDidLoad()
self.titleButton()
self.imageButton()
self.imageTitleBtn()
}
十、UITextField基礎(chǔ)
創(chuàng)建UITextField
繼承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è)置文本的對齊方式(默認是:左對齊)
textField.textAlignment = .Left
//6.密文顯示(默認是false)
textField.secureTextEntry = true
2.顯示相關(guān)
//1.設(shè)置文本框的樣式
textField.borderStyle = .RoundedRect
//2.設(shè)置清除按鈕模式(清除按鈕實質(zhì)是rightView)
//(前提是輸入框中有文字)
//Always -> 一直顯示
//Never -> 從不顯示(默認)
//WhileEditing -> 當(dāng)文本輸入框處于編輯狀態(tài)的時候才顯示
//UnlessEditing ->當(dāng)文本輸入框處于非編輯狀態(tài)的時候才顯示
//注:當(dāng)文本輸入框中有光標的時候就是處于編輯狀態(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è)置左視圖的顯示模式(確定什么時候顯示墓捻,默認是從不顯示)
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
4.擴展
//MARK: - UITextField Delegate
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("進入秘密頁")
}
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é)束編輯(默認是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是否可以進行編輯(默認是true)
func textFieldShouldBeginEditing(textField: UITextField) -> Bool{
print("將要開始編輯")
return true //false ->讓textField不能進行編輯
}
}
十一、控件大集合
1. 創(chuàng)建控件
我們利用擴展來對各種控件定義和操作梧兼。
extension ViewController{
}
說明:下面的控件都要寫在擴展內(nèi)放吩。
1. 開關(guān)
func creatSwitch() {
// 1. 創(chuàng)建開關(guān)對象
// UISwitch:UIConter:UIView
let sw = UISwitch.init(frame: CGRectMake(100, 100, 100, 50))
// 2. 添加到界面上
self.view.addSubview(sw)
// 3.核心屬性:開關(guān)狀態(tài)
// 設(shè)置開關(guān)的狀態(tài)
sw.on = true // flase -> 關(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)的顏色(默認是綠色)
sw.onTintColor = UIColor.redColor()
// 6. 開關(guān)關(guān)閉的時候的邊框顏色
sw.tintColor = UIColor.purpleColor()
// 7. 設(shè)置開關(guān)上的滑塊的顏色
sw.thumbTintColor = UIColor.yellowColor()
}
事件響應(yīng):
對于事件響應(yīng)羽杰,可以再建一個擴展渡紫,用來專門存放定義事件到推,這樣代碼可以容易閱讀,方便管理惕澎。
func switchAction(sw:UISwitch) {
if sw.on {
self.view.backgroundColor = UIColor.brownColor()
print("開關(guān)打開")
}else {
self.view.backgroundColor = UIColor.blackColor()
print("開關(guān)關(guān)閉")
}
}
效果截圖:
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. 核心屬性: 值(滑塊的位置對應(yīng)的值)
// value: 滑塊的位置對應(yīng)的值 (默認是0~1)
slider.value = 0.5
// 最小值和最大值
slider.minimumValue = 0
slider.maximumValue = 100
// 4. 核心方法
slider.addTarget(self, action: "sliderAction:", forControlEvents: .ValueChanged)
// 5. 和顏色相關(guān)的屬性
slider.minimumTrackTintColor = UIColor.blueColor()
slider.maximumTrackTintColor = UIColor.redColor()
// 6. 和圖片相關(guān)的屬性
slider.setThumbImage(UIImage.init(named: "player_down_2.png"), forState: .Normal)
slider.maximumValueImage = UIImage.init(named: "player_down_2.png")
slider.minimumValueImage = UIImage.init(named: "player_down_2.png")
// 7. 是否連續(xù)改變
slider.continuous = false
}
事件響應(yīng):
func sliderAction(slider:UISlider) {
print(slider.value)
// 拿到進度條
let progress = self.view.viewWithTag(100) as! UIProgressView
let t = slider.value/(slider.maximumValue - slider.minimumValue)
progress.setProgress(t, animated: true)
}
效果截圖
3. 步進器
func creatStepper() {
// 1. 創(chuàng)建步進器對象
let stepper = UIStepper.init(frame: CGRectMake(150, 250, 100, 50))
// 2. 添加到界面上
self.view.addSubview(stepper)
// 3. 核心屬性:值
// 當(dāng)前值
stepper.value = 1
print(stepper.value)
// 最小值和最大值
stepper.minimumValue = 0
stepper.maximumValue = 10
// 步進(每按一下加或者減莉测,增加/減少的值)
stepper.stepValue = 1 // 步進值必須大于0
// 4. 核心方法
stepper.addTarget(self, action: "stepperAction:", forControlEvents: .ValueChanged)
// 5. 設(shè)置值是否連續(xù)改變(按住不放的時候)
stepper.continuous = false
// 6. 設(shè)置是否重復(fù) false -> 按住不放的時候不計數(shù);true -> 按住不放的時候計數(shù)(默認的)
stepper.autorepeat = false
// 7. 設(shè)置填充顏色
stepper.tintColor = UIColor.redColor()
}
事件響應(yīng):
func stepperAction(stepper:UIStepper) {
print(stepper.value)
}
效果截圖:
4. 進度條
func creatProgress() {
// 1. 創(chuàng)建進度條對象
//UIProgressView:UIView
let progress = UIProgressView.init(frame: CGRectMake(50, 350, 300, 20))
progress.tag = 100
// 2. 添加到界面上
self.view.addSubview(progress)
// 3. 核心屬性
// 進度:0~1
// 設(shè)置當(dāng)前進度
progress.progress = 0.1
progress.setProgress(0.6, animated: true)
// 4. 顏色相關(guān)
// 5. 圖片相關(guān)
}
效果截圖:
5. 活動指示器
func creatActivity() {
// 1. 創(chuàng)建活動指示器對象
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)建多段選擇器對象
// let segement = UISegmentedControl.init(frame: CGRectMake(100, 400, 200, 50))
let segement = UISegmentedControl.init(items: ["海賊王", "火影忍者", "死神2"])
segement.frame = CGRectMake(100, 400, 200, 50)
// 2. 顯示在界面上
self.view.addSubview(segement)
// 3. 核心屬性
// a. 每個分段上的內(nèi)容 -> 通過創(chuàng)建分段選擇器的時候去設(shè)置
// b. 當(dāng)前選中的分段的下標(從0 開始)
segement.selectedSegmentIndex = 0
// 4. 核心方法
segement.addTarget(self, action: "segementAction:", forControlEvents: .ValueChanged)
// 5. 拿到分數(shù)段選擇器的分段數(shù)
print(segement.numberOfSegments)
// 6. 設(shè)置填充顏色
segement.tintColor = UIColor.yellowColor()
}
事件響應(yīng):
func segementAction(segement:UISegmentedControl) {
print(segement.selectedSegmentIndex)
// 拿到當(dāng)前被選中的分段的title
print(segement.titleForSegmentAtIndex(segement.selectedSegmentIndex))
}
效果截圖:
7. 警告框
class ViewController: UIViewController {
//MARK: 生命周期
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
// 1. 創(chuàng)建一個表單視圖
// 參數(shù)1: 標題
// 參數(shù)2: 信息
// 參數(shù)3: 風(fēng)格(表單捣卤、警告框)
// .Alert : 表單顯示在中間 ActionSheet -> 表單顯示在下方
let alterController = UIAlertController.init(title: "標題", message: "消息", preferredStyle: .ActionSheet)
// 2. 添加到界面上
// 參數(shù)1: 需要顯示的視圖控制
self.presentViewController(alterController, animated: true, completion: nil)
// 3. 添加選項按鈕
// 參數(shù)1: 選項對應(yīng)的按鈕上的文字
// 參數(shù)2: 風(fēng)格
// 參數(shù)3: 當(dāng)前選項對應(yīng)的按鈕被點擊后會執(zhí)行的代碼對應(yīng)的閉包
let action = UIAlertAction.init(title: "相機", style: .Default) { (_) in
print("相機被點擊")
}
//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īng)取消")
}
//添加相機對應(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對象
let textView = UITextView.init(frame: CGRectMake(100, 100, 200, 70))
// 2. 添加到界面上
self.view.addSubview(textView)
// 3. 設(shè)置背景顏色
textView.backgroundColor = UIColor.greenColor()
// 4. text屬性
textView.text = "sadkjfhalfsd adbfdasbfffffffffffffff"
// 5. 設(shè)置是否可以選中
// 默認是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)
}
}
效果截圖: