最近在玩一些小控件饭于,自己也寫了點東西,這個自定義通知是在學(xué)習(xí)別人代碼和自己敲代碼的過程中學(xué)習(xí)到的毅人,今天記下來萬一以后還要用呢滤愕。
1.首先需要自定義通知的名字
//MARK: Overlay所用通知
let ROverlayWillDisappearNotification = "ROverlayWillDisappearNotification"
let ROverlayDidDisappearNotification = "ROverlayDidDisappearNotification"
let ROverlayWillAppearNotification = "ROverlayWillAppearNotification"
let ROverlayDidAppearNotification = "ROverlayDidAppearNotification"
let ROverlayProgressCompletedNotification = "ROverlayProgressCompletedNotification"
2.然后同使用時一樣线椰,添加觀察者
NotificationCenter.default.addObserver(self, selector: #selector(overlayDimensionsWithNotification(_:)), name: NSNotification.Name.UIApplicationDidChangeStatusBarOrientation, object: nil)
3.在需要發(fā)出通知的地方post
NotificationCenter.default.post(name: NSNotification.Name(rawValue: ROverlayWillAppearNotification), object: nil, userInfo: userInfo)
4.使用結(jié)束后記得移除
NotificationCenter.default.removeObserver(self)