iOS自定义通知

最近在玩一些小控件,自己也写了点东西,这个自定义通知是在学习别人代码和自己敲代码的过程中学习到的,今天记下来万一以后还要用呢。

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.在需要发出通知的地方post

  NotificationCenter.default.post(name: NSNotification.Name(rawValue: ROverlayWillAppearNotification), object: nil, userInfo: userInfo)

4.使用结束后记得移除

        NotificationCenter.default.removeObserver(self)

你可能感兴趣的:(iOS自定义通知)