swift 学习
1 static和class的区别
/*
class 不能修饰存储属性
class 修饰的计算属性可以被重写,static 修饰的不能被重写
static 可以修饰存储属性,static修饰的存储属性称为静态变量(常量)
static 修饰的静态方法不能被重写,class 修饰的类方法可以被重写
class 修饰的类方法被重写时,可以使用static 让方法变为静态方法
class 修饰的计算属性被重写时,可以使用static 让其变为静态属性,但它的子类就不能被重写了
class 只能在类中使用,但是static 可以在类,结构体,或者枚举中使用
*/
2 学习手势的使用
let gesture = UITapGestureRecognizer.init(target: self, action: #selector(ViewController.myLocationButtonDidTouch(ges:))) //创建手势
myImageView.addGestureRecognizer(gesture) //添加手势
//手势响应方法
@objc func myLocationButtonDidTouch(ges : UITapGestureRecognizer) -> Void {
let imgView = ges.view
print("imgview.frame = \(NSStringFromCGRect((imgView?.frame)!))")
}
3 swift 中kvo的使用(和oc中使用类似)
//属性的声明 kvo观察的属性要添加dynamic关键字
@objc dynamic var color :UIColor?
// 在init初始化方法中添加观察者
override init() {
super .init()
self.addObserver(self, forKeyPath: "color", options:[.old,.new], context: nil);
}
//在析构方法中移除观察者
deinit {
self.removeObserver(self, forKeyPath: "color")
}
//监听属性的改变
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "color"{
let newColor :UIColor = change![NSKeyValueChangeKey.newKey] as! UIColor
let oldColor :UIColor = change![NSKeyValueChangeKey.oldKey] as! UIColor
print("\(String(describing: newColor.cgColor.components?.first),String(describing: newColor.cgColor.components?.index(of: 1)),String(describing : newColor.cgColor.components?.last))")
print("\(String(describing: oldColor.cgColor.components?.first),String(describing: oldColor.cgColor.components?.index(of: 1)),String(describing : oldColor.cgColor.components?.last))")
}
}
oc项目中遇到的问题
最近在iPhoneX过程中,由于在LaunchImage勾选了iOS8.0 and later第一个选项而没有填入iPhoneX的启动图,导致整个app的字体和状态栏字体变大
解决方法:在LaunchImage中添加全部的启动图,不能缺少。
网友评论