Swift之UI-楊夏
總體感知
UI即User Interface(用戶界面)的簡稱。泛指用戶的操作界面,UI設(shè)計(jì)主要指界面的樣式,美觀程度。而使用上,對軟件的人機(jī)交互、操作邏輯、界面美觀的整體設(shè)計(jì)則是同樣重要的另一個(gè)門道。好的UI不僅是讓軟件變得有個(gè)性有品味,還要讓軟件的操作變得舒適、簡單、自由、充分體現(xiàn)軟件的定位和特點(diǎn)。
知識體系結(jié)構(gòu).png
上圖不清晰,請看下圖(自己對這周所學(xué)控件的總結(jié)):
ps:吼吼,冰山一角啊!!!個(gè)人感覺畫出來比較有感覺,但是這只是對本周學(xué)習(xí)的總結(jié)
Youth丶夏夏
第一天
首先我們從創(chuàng)建一個(gè)新的App開始:
屏幕快照 2016-08-22 下午7.26.27.png
屏幕快照 2016-08-22 下午7.26.53.png
屏幕快照 2016-08-22 下午7.28.37.png
屏幕快照 2016-08-22 下午7.28.56.png
關(guān)于AppDelegate.swift文件的解讀
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//創(chuàng)建一個(gè)視圖對象
let redView = UIView.init()
//添加到界面上
self.view.addSubview(redView)
//設(shè)置背景顏色
redView.backgroundColor = UIColor.redColor()
//1.frame(坐標(biāo)和大小)
redView.frame = CGRectMake(100, 100, 100, 100)
//2.center(中心點(diǎn)坐標(biāo))
//a.通過frame和確定視圖的中心點(diǎn)坐標(biāo)
print(redView.center)
//b.可以通過改變center的值,去改變視圖的坐標(biāo)
redView.center = CGPointMake(200, 200)
print(redView.frame)
//3.bounds(坐標(biāo)和大小)
//掌握:默認(rèn)情況下bounds的坐標(biāo)是(0,0),大小和視圖的frame大小一樣
print(redView.bounds)
//了解:
//如果改變bounds的大小,frame的大小和坐標(biāo)都改變,center不變
/*
redView.bounds = CGRectMake(0, 0, 200, 200)
print("frame\(redView.frame)")
print("center\(redView.center)")
*/
//如果改變bounds的坐標(biāo),不影響當(dāng)前視圖的位置。但是影響添加到當(dāng)前視圖上的子視圖的坐標(biāo),不建議修改bounds
redView.bounds = CGRectMake(0, 0, 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 / 2))
//c.平移
//參數(shù)1:在x方向上平移的距離,負(fù)值->向左移,正值->向右移
//參數(shù)2:在y方向上平移的距離,負(fù)值->向上移,正值->向下移
redView.transform = CGAffineTransformMakeTranslation(0, 300)
//d.多個(gè)形變同時(shí)發(fā)生
//在另外一個(gè)形變的前提下旋轉(zhuǎn)
//參數(shù)1:另外一個(gè)形變
//參數(shù)2:旋轉(zhuǎn)角度
redView.transform = CGAffineTransformRotate(CGAffineTransformMakeScale(0.5, 0.5), CGFloat(M_PI_4 / 2))
//在另外一個(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)
//將旋轉(zhuǎn)形變和平移形變組合
redView.transform = CGAffineTransformConcat(rotate, transLation1)
}
}
關(guān)于用UIWindow設(shè)計(jì)窗口(代碼實(shí)現(xiàn)App的窗口)
在AppDelegate.swift文件下
import UIKit
@UIApplicationMain
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)啟動完成
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
//1.創(chuàng)建UIWindow對象
//frame是UIView中的屬性,確定視圖顯示在屏幕上的位置和大小
//UIScreen.mainScreen() 拿到手機(jī)屏幕
self.window = UIWindow.init(frame: UIScreen.mainScreen().bounds)
//2.設(shè)置根視圖控制器
self.window?.rootViewController = UIViewController()
//3.設(shè)置背景顏色
self.window?.backgroundColor = UIColor.yellowColor()
return true
}
}
UIView基本屬性和方法
在ViewController.swift文件下
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//UIView:是iOS中所有視圖(控件)直接/間接的父類;所以UIView的屬性和方法,對于其他類型的視圖都有效
//視圖:在屏幕上能看見的所有的東西都屬于視圖
//1.創(chuàng)建UIView的對象
let redView = UIView.init()
//想要將視圖展示在屏幕上的兩個(gè)必要條件:
//a.必須設(shè)置坐標(biāo)和大小(默認(rèn)坐標(biāo)是(0,0),大小(0,0))
//b.將視圖添加到已經(jīng)展示在屏幕上的視圖上
//2.設(shè)置frame屬性(由坐標(biāo)(x,y)和大小(width,height)兩個(gè)部分組成)
redView.frame = CGRect(x: 10, y: 10, width: 100, height: 100)
//iOS中所有的結(jié)構(gòu)體都有一個(gè)對應(yīng)的Make方法用來快速的創(chuàng)建一個(gè)結(jié)構(gòu)體變量
redView.frame = CGRectMake(10, 10, 100, 100)
//3.將視圖添加到界面上
self.view.addSubview(redView)
//4.設(shè)置背景顏色
//視圖的背景顏色默認(rèn)是透明色
//顏色的創(chuàng)建方式:
//b.通過三原色來創(chuàng)建顏色
//CGFloat就是UI中的浮點(diǎn)型
//參數(shù)1,2,3: 紅、綠、藍(lán)的值(0~1) 0/255 ~ 255/255
//參數(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.7, alpha: 1)
//a.通過類型方法創(chuàng)建指定顏色
redView.backgroundColor = UIColor.redColor()
//練習(xí):創(chuàng)建一個(gè)黃色的矩形,顯示在紅色視圖的中點(diǎn)位置,大小是(50,50)
//方式1:
/*
//創(chuàng)建視圖對象并且設(shè)置frame屬性
let yellowView = UIView.init(frame: CGRectMake(35, 35, 50, 50))
//添加到界面上
self.view.addSubview(yellowView)
//設(shè)置背景顏色
yellowView.backgroundColor = UIColor.yellowColor()
*/
//方式2:
//創(chuàng)建視圖對象并且設(shè)置frame屬性
let yellowView = UIView.init(frame: CGRectMake(25, 25, 50, 50))
//添加到紅色視圖上
redView.addSubview(yellowView)
//設(shè)置背景顏色
yellowView.backgroundColor = UIColor.yellowColor()
//總結(jié):計(jì)算視圖的坐標(biāo)的時(shí)候,注意相對性。當(dāng)前視圖被添加到哪個(gè)視圖上,那么當(dāng)前視圖的坐標(biāo)就是相對于誰來算的
}
}
Frame相關(guān)屬性
在ViewController.swift文件下
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//創(chuàng)建一個(gè)視圖對象
let redView = UIView.init()
//添加到界面上
self.view.addSubview(redView)
//設(shè)置背景顏色
redView.backgroundColor = UIColor.redColor()
//1.frame(坐標(biāo)和大小)
redView.frame = CGRectMake(100, 100, 100, 100)
//2.center(中心點(diǎn)坐標(biāo))
//a.通過frame和確定視圖的中心點(diǎn)坐標(biāo)
print(redView.center)
//b.可以通過改變center的值,去改變視圖的坐標(biāo)
redView.center = CGPointMake(200, 200)
print(redView.frame)
//3.bounds(坐標(biāo)和大小)
//掌握:默認(rèn)情況下bounds的坐標(biāo)是(0,0),大小和視圖的frame大小一樣
print(redView.bounds)
//了解:
//如果改變bounds的大小,frame的大小和坐標(biāo)都改變,center不變
/*
redView.bounds = CGRectMake(0, 0, 200, 200)
print("frame\(redView.frame)")
print("center\(redView.center)")
*/
//如果改變bounds的坐標(biāo),不影響當(dāng)前視圖的位置。但是影響添加到當(dāng)前視圖上的子視圖的坐標(biāo),不建議修改bounds
redView.bounds = CGRectMake(0, 0, 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 / 2))
//c.平移
//參數(shù)1:在x方向上平移的距離,負(fù)值->向左移,正值->向右移
//參數(shù)2:在y方向上平移的距離,負(fù)值->向上移,正值->向下移
redView.transform = CGAffineTransformMakeTranslation(0, 300)
//d.多個(gè)形變同時(shí)發(fā)生
//在另外一個(gè)形變的前提下旋轉(zhuǎn)
//參數(shù)1:另外一個(gè)形變
//參數(shù)2:旋轉(zhuǎn)角度
redView.transform = CGAffineTransformRotate(CGAffineTransformMakeScale(0.5, 0.5), CGFloat(M_PI_4 / 2))
//在另外一個(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)
//將旋轉(zhuǎn)形變和平移形變組合
redView.transform = CGAffineTransformConcat(rotate, transLation1)
}
}
第二天
UIView父子視圖
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//1.創(chuàng)建一個(gè)UIView對象
let redView = UIView.init(frame: CGRectMake(100, 100, 200, 200))
//2.設(shè)置背景顏色
redView.backgroundColor = UIColor.redColor()
//3.添加到界面上
//self.view就是redView的父視圖,redView就是self.view的子視圖
self.view.addSubview(redView)
//4.設(shè)置tag值,默認(rèn)都是0.設(shè)tag值的時(shí)候值必須要大于0
//tag的作用是用來區(qū)分界面上不同的視圖
redView.tag = 100
//MARK: - 父子視圖的特點(diǎn)和方法
//再創(chuàng)建一個(gè)黃色視圖
let yellowView = UIView.init(frame: CGRectMake(0, 0, 50, 50))
yellowView.backgroundColor = UIColor.yellowColor()
yellowView.tag = 101
//1.一個(gè)視圖只有一個(gè)父視圖,可以有多個(gè)子視圖
//連續(xù)將同一個(gè)視圖添加到兩個(gè)視圖上,最后一次添加有效
redView.addSubview(yellowView)
self.view.addSubview(yellowView)
//2.獲取一個(gè)視圖的父視圖
let superView = redView.superview
superView?.backgroundColor = UIColor.greenColor()
//3.獲取一個(gè)視圖的所有的子視圖
let subViews = self.view.subviews
//拿到所有的子視圖中的紅色視圖和黃色視圖
//a.
for item in subViews {
//判斷item是否是UILabel類型的。如果是就返回true,如果不是就返回false
if item.isKindOfClass(UILabel.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 subView2 = self.view.viewWithTag(101)
subView2?.frame.origin.y = 100
}
}
視圖的層次關(guān)系
import UIKit
class ViewController: UIViewController {
//MARK: - 生命周期方法
override func viewDidLoad() {
super.viewDidLoad()
self.creatUI()
}
//MARK: - 創(chuàng)建界面
func creatUI() {
//結(jié)論:在一個(gè)視圖上,添加多個(gè)子視圖的時(shí)候,子視圖之間如果有公共的部分。那么后添加的子視圖會覆蓋先添加的
//1.一般情況下,如果想要將一個(gè)視圖顯示在最下面,就最先添加。想要顯示在最上面就最后添加
//創(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.將指定的視圖插入到另一個(gè)視圖的上面
self.view.insertSubview(view2, aboveSubview: view3)
//5.將指定的視圖插入到另外一個(gè)視圖的下面
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動畫
import UIKit
class ViewController: UIViewController {
//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)
//切圓角
self.layerAction()
//使用動畫
self.UIViewAnimation4()
}
//MARK: - Layer屬性
func layerAction() {
//layer屬性是負(fù)責(zé)視圖的形狀(顯示)
//1.切圓角
//當(dāng)圓角的值為正方形的寬的一半,就可以切一個(gè)圓
self.subView.layer.cornerRadius = 50
//2.設(shè)置邊框
self.subView.layer.borderWidth = 3
self.subView.layer.borderColor = UIColor.redColor().CGColor
}
//MARK: - 動畫方法
//UIView的動畫使用來動畫的改變視圖的frame相關(guān)屬性、背景顏色、透明度
//方法4:
func UIViewAnimation4() {
//參數(shù)1:動畫時(shí)間
//參數(shù)2:延遲時(shí)間
//參數(shù)3:彈簧的壓力系數(shù)
//參數(shù)4:彈簧初始的加速度
//參數(shù)5:選項(xiàng)
//參數(shù)6:設(shè)置動畫結(jié)束時(shí)視圖的狀態(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)
}
//方法3:
func UIViewAnimation3() {
//參數(shù)1:動畫時(shí)間
//參數(shù)2:延遲時(shí)間
//參數(shù)3:選項(xiàng),Repeat->動畫重復(fù)執(zhí)行,Autoreverse->自動回到動畫開始的狀態(tài)
//參數(shù)4:設(shè)置動畫結(jié)束時(shí)視圖狀態(tài)的閉包
//參數(shù)5:整個(gè)動畫過程完成后需要執(zhí)行的閉包
UIView.animateWithDuration(2, delay: 1, options: [ .Repeat, .Autoreverse], animations: {
self.subView.transform = CGAffineTransformMakeTranslation(0, -200)
}, completion: nil)
}
//方法2:
func UIViewAnimation2(){
//參數(shù)3:整個(gè)動畫完成后會自動調(diào)用這個(gè)閉包
UIView.animateWithDuration(2, animations: {
self.subView.transform = CGAffineTransformMakeTranslation(0, -200)
}) { (_) in
//寫動畫結(jié)束后需要執(zhí)行的代碼
UIView.animateWithDuration(2, animations: {
self.subView.transform = CGAffineTransformMakeTranslation(0, 0)
})
}
}
//方法1:
func UIViewAnimation1() {
//功能:執(zhí)行這個(gè)方法前視圖的狀態(tài),動畫的切換到閉包里面設(shè)置的最終狀態(tài)
//參數(shù)1:動畫時(shí)間(單位:秒)
//參數(shù)2:閉包,設(shè)置動畫結(jié)束時(shí)視圖的狀態(tài)
UIView.animateWithDuration(2) {
//在這兒來設(shè)置視圖動畫結(jié)束時(shí)的狀態(tài)
//a.動畫的改變視圖的坐標(biāo)
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
}
}
}
UILabel基礎(chǔ)
import UIKit
class ViewController: UIViewController {
//MARK: - 生命周期
override func viewDidLoad() {
super.viewDidLoad()
self.creatLabel()
}
//MARK: - 創(chuàng)建Label
func creatLabel() {
//UILabel:UIView -> UIView的屬性和方法,UILabel都擁有
//=======從UIView繼承下來的屬性=======
//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 這樣就用代碼實(shí)現(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的大小
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//需要顯示在label上的文字
let str = "收到回復(fù)asdfsajfdkl刷卡費(fèi)上"
//計(jì)算顯示指定文字所需要的最小空間
//1.將swift的字符串轉(zhuǎn)換成OC的字符串
let ocStr = str as NSString
//2.計(jì)算字符串的大小
//參數(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
}
}
第三天
01-UIImageView基礎(chǔ)
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//UIImageView:UIView
//==========UIView的屬性和方法==========
//1.創(chuàng)建UIImageView對象
let imageView = UIImageView.init(frame: CGRectMake(0, 100, 300, 300))
//2.添加到界面上
self.view.addSubview(imageView)
//3.設(shè)置背景顏色
imageView.backgroundColor = UIColor.yellowColor()
//=========UImageView專有屬性=========
//1.image屬性
//a.通過圖片名去創(chuàng)建一個(gè)圖片對象(注意:如果圖片的格式是png,那么圖片名的后綴可以省略。但是其他格式的圖片的圖片名的后綴不能省略)
imageView.image = UIImage.init(named: "back2.jpg")
//b.通過圖片路徑去創(chuàng)建一個(gè)圖片對象
//將文件(除了swift文件)放到工程中,實(shí)質(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)前圖片對象不再使用的時(shí)候就銷毀
//(2)使用圖片名創(chuàng)建圖片的情況:創(chuàng)建小圖標(biāo)的時(shí)候;在工程中會重復(fù)使用的圖片
//(3)使用圖片地址創(chuàng)建圖片對象的情況:不會頻繁的在多個(gè)界面出現(xiàn)的大圖
//2.內(nèi)容模式
imageView.contentMode = .ScaleToFill
}
}
02-UIImageView幀動畫
import UIKit
class ViewController: UIViewController {
//MARK: - 屬性
var imageView = UIImageView()
//MARK: - 生命周期
override func viewDidLoad() {
super.viewDidLoad()
self.creatImageView()
//創(chuàng)建一個(gè)定時(shí)器,并且自動開啟
//參數(shù)1:定時(shí)時(shí)間
//參數(shù)2:調(diào)用方法的對象
//參數(shù)3:存儲定時(shí)時(shí)間到了以后需要調(diào)用的方法(可以不帶參也可以帶參,但是如果帶參只能帶一個(gè)參,并且參數(shù)類型是NSTimer類型)
//參數(shù)4:給當(dāng)前的NSTimer的userInfo屬性賦的值(一般寫nil)
//參數(shù)5:是否重復(fù)
//功能:每隔0.1秒,self去調(diào)用一次timerAction方法
NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: "timerAction:", userInfo: "aaa", repeats: true)
}
//定時(shí)器方法
//參數(shù):當(dāng)前定時(shí)器
func timerAction(timer:NSTimer) {
print(timer.userInfo)
self.imageView.frame.origin.x += 3
//判斷小鳥是否飛到了屏幕邊緣
if self.imageView.frame.origin.x >= self.view.bounds.width - self.imageView.bounds.width {
//暫停定時(shí)器
timer.fireDate = NSDate.distantFuture()
//讓定時(shí)器繼續(xù)
//timer.fireDate = NSDate.distantPast()
}
}
//創(chuàng)建imageView
func creatImageView() {
//1.創(chuàng)建一個(gè)UIImageView對象
//通過圖片去創(chuàng)建一個(gè)imageView;UIImageView的大小是圖片的大小,坐標(biāo)是(0,0)
imageView = UIImageView.init(image: UIImage.init(named: "DOVE 1.png"))
//2.顯示在界面上
self.view.addSubview(imageView)
//3.使用UIImageView播放幀動畫
//a.設(shè)置幀動畫數(shù)組
//創(chuàng)建一個(gè)空的圖片數(shù)組
var imageArray = [UIImage]()
//通過for循環(huán)創(chuàng)建18張圖片
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è)置動畫時(shí)間,單位秒
imageView.animationDuration = 1
//c.設(shè)置動畫的重復(fù)次數(shù)(默認(rèn)是0->無限循環(huán))
imageView.animationRepeatCount = 0
//d.開始動畫
imageView.startAnimating()
}
}
UIButton基礎(chǔ)
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.titleButton()
self.imageButton()
self.imageTitleBtn()
}
//MARK: - 圖片文字按鈕
func imageTitleBtn() {
//a.同時(shí)設(shè)置title和image屬性,顯示是圖片在左,文字在右
//b.同時(shí)設(shè)置title和groundImage,顯示是圖片在下層,文字在上層
//1.創(chuàng)建一個(gè)按鈕對象
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)
}
//MARK: - 圖片按鈕
func imageButton() {
//1.創(chuàng)建一個(gè)按鈕對象
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.添加按鈕點(diǎn)擊事件
imageBtn.addTarget(self, action: "btnAction:", forControlEvents: .TouchUpInside)
}
//MARK: - 文字按鈕
func titleButton() {
//UIButton:UIControl:UIView
//UIButton上有一個(gè)titleLabel專門負(fù)責(zé)按鈕上文字的顯示;有一個(gè)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)(按鈕正常顯示,沒有被點(diǎn)擊或者按下的時(shí)候)
//Highlighted ->高亮(按鈕被按下,沒有彈起來的時(shí)候的狀態(tài))
//Selected -> 選中狀態(tài)
//Disabled -> 不可用狀態(tài)(按鈕不能被點(diǎn)擊)
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)用的方法(這個(gè)方法可以不帶參,如果帶參只能帶一個(gè),并且參數(shù)的類型是UIButton類型),實(shí)參就是當(dāng)前添加事件的按鈕本身
//參數(shù)3:事件
//TouchDown -> 按下事件
//功能:當(dāng)按鈕被按下的時(shí)候,self會去調(diào)用btnAction方法
//TouchUpInside ->按下彈起事件
//功能:當(dāng)按鈕被按下彈起來的時(shí)候,self會去調(diào)用btnAction方法
titleBtn.addTarget(self, action: "btnAction:", forControlEvents: .TouchUpInside)
}
//MARK: - 按鈕點(diǎn)擊
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)
}
}
溫馨提示:當(dāng)系統(tǒng)所設(shè)定的Button按鈕不能滿足你的需要時(shí),你可以自定義你的個(gè)性按鈕
第四天
UITextField基礎(chǔ)
//快捷鍵:command + k 彈起/收起模擬器上的鍵盤
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//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)建一個(gè)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)容為空的時(shí)候就會顯示出來)
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è)置清除按鈕模式(清除按鈕實(shí)質(zhì)是rightView)
//(前提是輸入框中有文字)
//Always -> 一直顯示
//Never -> 從不顯示(默認(rèn))
//WhileEditing -> 當(dāng)文本輸入框處于編輯狀態(tài)的時(shí)候才顯示
//UnlessEditing ->當(dāng)文本輸入框處于非編輯狀態(tài)的時(shí)候才顯示
//注:當(dāng)文本輸入框中有光標(biāo)的時(shí)候就是處于編輯狀態(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è)置左視圖的顯示模式(確定什么時(shí)候顯示,默認(rèn)是從不顯示)
textField.leftViewMode = .Always
//4.右視圖
//當(dāng)右視圖顯示的時(shí)候,清除按鈕不能顯示
/*
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
}
}
//MARK: - UITextField Delegate
extension ViewController: UITextFieldDelegate{
//6.當(dāng)按鍵盤上的返回按鈕的時(shí)候,會自動調(diào)用
func textFieldShouldReturn(textField: UITextField) -> Bool{
print("返回按鈕被點(diǎn)擊")
//收起鍵盤(結(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)點(diǎn)擊textField彈出來的鍵盤上的按鍵的時(shí)候會自動調(diào)用這個(gè)方法
//參數(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é)束編輯的時(shí)候會自動調(diào)用這個(gè)方法
func textFieldDidEndEditing(textField: UITextField){
print("已經(jīng)結(jié)束編輯")
}
//2.當(dāng)文本輸入框已經(jīng)開始編輯的時(shí)候會自動調(diào)用這個(gè)方法
func textFieldDidBeginEditing(textField: UITextField){
print(textField.text)
print("已經(jīng)開始編輯")
}
//3.當(dāng)文本輸入框?qū)⒁Y(jié)束編輯的時(shí)候會自動調(diào)用這個(gè)方法
//返回:設(shè)置當(dāng)前的textField是否可以結(jié)束編輯(默認(rèn)是true)
func textFieldShouldEndEditing(textField: UITextField) -> Bool{
print("將要結(jié)束編輯")
//要求文本輸入框中的文字長度要大于等于8的時(shí)候才能結(jié)束編輯
if textField.text?.characters.count >= 8 {
return true
}
return false
}
//1.在textField將要開始編輯的時(shí)候會自動調(diào)用
//參數(shù):當(dāng)前這個(gè)協(xié)議對應(yīng)的委托
//返回值:設(shè)置當(dāng)前的textField是否可以進(jìn)行編輯(默認(rèn)是true)
func textFieldShouldBeginEditing(textField: UITextField) -> Bool{
print("將要開始編輯")
return true //false ->讓textField不能進(jìn)行編輯
}
}
溫馨提示:和前面Button一樣,當(dāng)你不想使用系統(tǒng)自帶的鍵盤時(shí),你完全可以自己定制一個(gè)屬于你的個(gè)性鍵盤。
第五天
今天所講的控件圖
QFImage_2016-08-26_9.17.30.png
控件大集合
import UIKit
//MARK: - 生命周期和屬性
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
//設(shè)置背景顏色
self.view.backgroundColor = UIColor.greenColor()
self.creatSwitch()
self.creatSlider()
self.creatStepper()
self.creatProgress()
self.creatActivity()
self.creatSegement()
}
}
//MARK: - 創(chuàng)建控件
extension ViewController{
//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ā)生改變的時(shí)候,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)閉的時(shí)候的邊框顏色
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ù)改變(按住不放的時(shí)候)
stepper.continuous = false
//6.設(shè)置是否重復(fù) false->按住不放的時(shí)候不計(jì)數(shù);true->按住不放的時(shí)候計(jì)數(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.每個(gè)分段上的內(nèi)容 ->通過創(chuàng)建分段選擇器的時(shí)候去設(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)閉")
}
}
}
效果圖展示:
屏幕快照 2016-08-27 下午4.03.46.png
警告框和文本視圖
import UIKit
class ViewController: UIViewController {
//MARK: - 生命周期
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
//1.創(chuàng)建一個(gè)表單視圖
//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.添加選項(xiàng)按鈕
//參數(shù)1:選項(xiàng)對應(yīng)的按鈕上的文字
//參數(shù)2:風(fēng)格
//參數(shù)3:當(dāng)當(dāng)前選項(xiàng)對應(yīng)的按鈕被點(diǎn)擊后會執(zhí)行的代碼對應(yīng)的閉包
let action = UIAlertAction.init(title: "相機(jī)", style: .Default) { (_) in
print("相機(jī)被點(diǎn)擊")
}
//Destructive風(fēng)格
let action2 = UIAlertAction.init(title: "相冊", style: .Destructive) { (_) in
print("相冊被點(diǎn)擊")
}
//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
}
//在點(diǎn)擊當(dāng)前頁面的時(shí)候會自動調(diào)用這個(gè)方法
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//5.獲取選中的范圍
let range = textView.selectedRange
print(range)
}
}
-更多精彩內(nèi)容請關(guān)注:Youth丶夏夏-