1.协议的使用
1.定义协议:
1> 定义函数
2> 定义属性
* 注意: 定义的属性必须明确的指定该属性是一个可读可写(set get)/只读属性(get)
protocol SportProtocol {
var price : Double { get }
func playFootball()
}
2. 如果让协议只能被类遵守: class
3. 如何让协议中的方法是可选的 @objc optional
4. 协议中的属性&函数, 提供默认的实现
注意:
1> 默认实现必须写在协议的extension中
2> 属性的默认实现, 只能写成计算属性(只读属性)
extension SportProtocol {
var price : Double {
return 20
}
func playFootball() {
print("踢足球")
}
}
class Person : SportProtocol {
}
let p = Person()
print(p.price)
p.playFootball()
}
2.实现全屏pop运行时 ----tab控制器加进来之后---添加自己的手势调用系统的的方法
// interactivePopGestureRecognizer --> target/action
// 1.将所有的属性拷贝出来
/*
var count : UInt32 = 0
let ivars = class_copyIvarList(UIGestureRecognizer.self, &count)
// 2.遍历数组
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的数组
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.创建自己的手势
let panGes = UIPanGestureRecognizer(target: target, action: action)
view.addGestureRecognizer(panGes)
3.一个类中多个构造方法--属性初始化(只有其中一个方法用了这样+!)
若是+? 就变成可选类型了 用的时候需要判断例如代理 var delegate: uiview?
let name : uiview!
方法1中用也进行了初始化
方法2中不用就不用进行初始化了
4.粒子动画----CAEmitterLayer()
override func touchesBegan(_ touches: Set, with event: UIEvent?) {
// 1.创建发射器
let emitterLayer = CAEmitterLayer()
// 2.设置发射器的位置
emitterLayer.emitterPosition = CGPoint(x: view.bounds.width * 0.5, y: 100)
// 3.设置粒子
var cells = [CAEmitterCell]()
for i in 0..<10 {
// 3.1.创建粒子
let cell = CAEmitterCell()
// 3.2.设置发射的速率
cell.birthRate = 5
// 3.3.设置发射的方向
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.设置粒子缩放
cell.scale = 0.6 // 0.3 ~ 0.9
cell.scaleRange = 0.3
cell.spin = 2
cell.spinRange = 1
// 3.7.设置粒子的内容
cell.contents = UIImage(named: "good\(i)_30x30")?.cgImage
cells.append(cell)
}
// 3.8.将cell设置到发射器中
emitterLayer.emitterCells = cells
// 4.将layer添加到父layer中
view.layer.addSublayer(emitterLayer)
}
5.遍历的写法
1. for i in 0..<count
2.for (i, name) in count.enumerate