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.. 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) let name : uiview! override func touchesBegan(_ touches: Set, with event: UIEvent?) { let emitterLayer = CAEmitterLayer() emitterLayer.emitterPosition = CGPoint(x: view.bounds.width * 0.5, y: 100) var cells = [CAEmitterCell]() for i in 0..<10 { let cell = CAEmitterCell() cell.birthRate = 5 cell.emissionLongitude = CGFloat(M_PI/2) cell.emissionRange = CGFloat(M_PI/6) cell.velocity = 80 // 40~120 cell.velocityRange = 40 cell.lifetime = 8 // 6~14 cell.lifetimeRange = 3 cell.scale = 0.6 // 0.3 ~ 0.9 cell.scaleRange = 0.3 cell.spin = 2 cell.spinRange = 1 cell.contents = UIImage(named: "good\(i)_30x30")?.cgImage cells.append(cell) } emitterLayer.emitterCells = cells view.layer.addSublayer(emitterLayer) } 1. for i in 0.. 2.for (i, name) in count.enumerate3.一个类中多个构造方法--属性初始化(只有其中一个方法用了这样+!)
若是+? 就变成可选类型了 用的时候需要判断例如代理 var delegate: uiview?
方法1中用也进行了初始化
方法2中不用就不用进行初始化了
4.粒子动画----CAEmitterLayer()
// 1.创建发射器
// 2.设置发射器的位置
// 3.设置粒子
// 3.1.创建粒子
// 3.2.设置发射的速率
// 3.3.设置发射的方向
// 3.4.粒子的速率
// 3.5.生存时间
// 3.6.设置粒子缩放
// 3.7.设置粒子的内容
// 3.8.将cell设置到发射器中
// 4.将layer添加到父layer中
5.遍历的写法