swift--03

1.協(xié)議的使用

1.定義協(xié)議:

1> 定義函數(shù)

2> 定義屬性

* 注意: 定義的屬性必須明確的指定該屬性是一個可讀可寫(set get)/只讀屬性(get)

protocol SportProtocol {

var price : Double { get }

func playFootball()

}

2. 如果讓協(xié)議只能被類遵守: class

3. 如何讓協(xié)議中的方法是可選的 @objc optional

4. 協(xié)議中的屬性&函數(shù), 提供默認(rèn)的實現(xiàn)

注意:

1> 默認(rèn)實現(xiàn)必須寫在協(xié)議的extension中

2> 屬性的默認(rèn)實現(xiàn), 只能寫成計算屬性(只讀屬性)

extension SportProtocol {

var price : Double {

return 20

}

func playFootball() {

print("踢足球")

}

}

class Person : SportProtocol {

}

let p = Person()

print(p.price)

p.playFootball()

}

2.實現(xiàn)全屏pop運行時 ----tab控制器加進(jìn)來之后---添加自己的手勢調(diào)用系統(tǒng)的的方法

// interactivePopGestureRecognizer? --> target/action

// 1.將所有的屬性拷貝出來

/*

var count : UInt32 = 0

let ivars = class_copyIvarList(UIGestureRecognizer.self, &count)

// 2.遍歷數(shù)組

for i in 0..<count{

guard let ivar = ivars![Int(i)] else {

continue

}

let nameP = ivar_getName(ivar)

let name = String(cString: nameP!)

print(name)

}

// 1.獲取targetsValue的數(shù)組

guard let targetsValue = interactivePopGestureRecognizer?.value(forKeyPath: "_targets") as? [NSObject] else {

return

}

// 2.取出的對象

guard let targetObjc = targetsValue.first else {

return

}

// 4.從對象中取出target/action

let target = targetObjc.value(forKeyPath: "target")

let action = Selector(("handleNavigationTransition:"))

// 5.創(chuàng)建自己的手勢

let panGes = UIPanGestureRecognizer(target: target, action: action)

view.addGestureRecognizer(panGes)

3.一個類中多個構(gòu)造方法--屬性初始化(只有其中一個方法用了這樣+!)

若是+? 就變成可選類型了 ?用的時候需要判斷例如代理 var delegate: uiview?

let name : uiview!

方法1中用也進(jìn)行了初始化

方法2中不用就不用進(jìn)行初始化了

4.粒子動畫----CAEmitterLayer()

override func touchesBegan(_ touches: Set, with event: UIEvent?) {

// 1.創(chuàng)建發(fā)射器

let emitterLayer = CAEmitterLayer()

// 2.設(shè)置發(fā)射器的位置

emitterLayer.emitterPosition = CGPoint(x: view.bounds.width * 0.5, y: 100)

// 3.設(shè)置粒子

var cells = [CAEmitterCell]()

for i in 0..<10 {

// 3.1.創(chuàng)建粒子

let cell = CAEmitterCell()

// 3.2.設(shè)置發(fā)射的速率

cell.birthRate = 5

// 3.3.設(shè)置發(fā)射的方向

cell.emissionLongitude = CGFloat(M_PI/2)

cell.emissionRange = CGFloat(M_PI/6)

// 3.4.粒子的速率

cell.velocity = 80 // 40~120

cell.velocityRange = 40

// 3.5.生存時間

cell.lifetime = 8 // 6~14

cell.lifetimeRange = 3

// 3.6.設(shè)置粒子縮放

cell.scale = 0.6 // 0.3 ~ 0.9

cell.scaleRange = 0.3

cell.spin = 2

cell.spinRange = 1

// 3.7.設(shè)置粒子的內(nèi)容

cell.contents = UIImage(named: "good\(i)_30x30")?.cgImage

cells.append(cell)

}

// 3.8.將cell設(shè)置到發(fā)射器中

emitterLayer.emitterCells = cells

// 4.將layer添加到父layer中

view.layer.addSublayer(emitterLayer)

}

5.遍歷的寫法

1. for ?i ?in 0..<count

2.for (i, name) in count.enumerate

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容