71 lines
2.3 KiB
Swift
71 lines
2.3 KiB
Swift
//
|
||
// Notification.swift
|
||
// MusicPlayer
|
||
//
|
||
// Created by Mr.Zhou on 2024/4/1.
|
||
//
|
||
|
||
import Foundation
|
||
///通知关键词协议
|
||
protocol NotificationKey {
|
||
associatedtype Keys: RawRepresentable
|
||
}
|
||
///将RawValue转化为字符串
|
||
extension NotificationKey where Keys.RawValue == String {
|
||
///发通知
|
||
static func post(notificationName key:Keys, object:Any? = nil) {
|
||
let rawValue = key.rawValue
|
||
NotificationCenter.default.post(name: NSNotification.Name(rawValue), object: object)
|
||
}
|
||
///监听通知
|
||
static func add(observer: AnyObject, selector: Selector, notificationName key: Keys, object:Any? = nil) {
|
||
let rawValue = key.rawValue
|
||
NotificationCenter.default.addObserver(observer, selector: selector, name: NSNotification.Name(rawValue), object: object)
|
||
}
|
||
///移除通知
|
||
static func remove(observer: AnyObject, notificationName key: Keys, object:Any? = nil) {
|
||
let rawValue = key.rawValue
|
||
NotificationCenter.default.removeObserver(observer, name: NSNotification.Name(rawValue), object: object)
|
||
}
|
||
}
|
||
//MARK: - 扩写通知
|
||
extension NotificationCenter{
|
||
struct notificationKey:NotificationKey {
|
||
///关键词
|
||
enum Keys:String {
|
||
///切换taBarItem
|
||
case switch_tabBarItem
|
||
///倒计时过程(携带时间值)
|
||
case time_times
|
||
///音量值改变
|
||
case volume_change
|
||
///启动音乐播放器
|
||
case play_music
|
||
///终止音乐播放器与倒计时
|
||
case stop_music
|
||
///暂停音乐播放器
|
||
case pause_music
|
||
///继续音乐播放器
|
||
case resume_music
|
||
///开启麦克风监听
|
||
case open_monitor
|
||
///关闭麦克风监听
|
||
case stop_monitor
|
||
///新增音乐选择页
|
||
case new_choice
|
||
///音乐创建成功
|
||
case creat_music
|
||
///音乐实体为空
|
||
case null_music
|
||
///收起音乐展示框
|
||
case close_show
|
||
///显示音乐展示框
|
||
case display_show
|
||
///隐藏音乐展示框
|
||
case hidden_show
|
||
///音乐实体重命名
|
||
case rename_music
|
||
}
|
||
}
|
||
}
|