Swift UI 布局和通知销毁

   //发通知
     NotificationCenter.default.post(name: Notification.Name("yjbindWechat"), object: nil)
      //接收分享图片的通知
     NotificationCenter.default.addObserver(self, selector: #selector(yjpopalertcancelfirmAction), name: Notification.Name("yjpopalertcancel"), object: nil)
    //  NotificationCenter.default.addObserver(self, selector: #selector(yjTopalertcancelfirmAction), name: Notification.Name("yjTopalertcancel"), object: nil)
      NotificationCenter.default.addObserver(self, selector: #selector(yjLookcancelfirmAction), name: Notification.Name("yjLookcancel"), object: nil)
  /// 析构函数.类似于OC的 dealloc
  deinit {
   // 清除指定通知的方法
   // UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: ["yjbindWechat"])
   /// 移除通知
   NotificationCenter.default.removeObserver(self)
      
  }
创建通知中心
设置监听方法
设置通知的名字
       NotificationCenter.default.addObserver(self, selector: #selector(test), name: NSNotification.Name(rawValue:"isTest"), object: nil)

实现通知监听方法
@objc func test(nofi : Notification){
       let str = nofi.userInfo!["post"]
       print(String(describing: str!) + "this notifi")
   }

点击发送通知进行
   override func touchesBegan(_ touches: Set, with event: UIEvent?) {
       NotificationCenter.default.post(name: NSNotification.Name("isTest"), object: self, userInfo: ["post":"NewTest"])
   }

最后要记得移除通知
   deinit {
       /// 移除通知
       NotificationCenter.default.removeObserver(self)
   }

//var dataArr:NSMutableArray = ["点赞","评论","动态","粉丝"]
```
class MessageNewsMainHeaderView: UIView {

//消息 小红点
open var zangredNum :Int = 22
open var commentredNum :Int = 33
open var dtredNum :Int = 66
open var fsredNum :Int = 88


// 最底层容器view
lazy var mainView: UIView = {
    let v = UIView(frame: CGRect(x: 16, y: 12, width:YJScreen.width - 16 * 2 , height: 107))
    v.layer.cornerRadius = 10
    v.layer.masksToBounds = true
    v.backgroundColor = .white
    v.setShadow(.rgba(192,192,192,0.1), CGSize(width: 0, height: 2), 5, 1)
    return v
}()

/// 按钮个数
var index :Int  = 0
// 计算空隙 空隙 = (总宽 - 所有button的宽)/空隙个数
var gap:CGFloat = CGFloat((YJScreen.width - 16 * 2 - (40*CGFloat(4)))/(CGFloat(4 + 1)))

/// 点赞按钮
lazy var zanBtn: UIButton = {
    index = 0
    let btn = UIButton(frame:CGRect(x: (CGFloat(index+1))*gap + ((CGFloat(index)*40)), y: 29, width: 40, height: 40))
    btn.titleLabel?.font = UIFont.systemFont(ofSize: 14,weight:.medium)
    btn.setTitleColor(.rgb(51, 51, 51), for: UIControl.State.normal)
    btn.setImageInsets(image: UIImage(named: "new_zan"), title: "点赞", titlePosition: .bottom, additionalSpacing: 6, state: UIControl.State.normal)
    btn.backgroundColor = AppColor.bgFBFBFB
    //小红点
    let Label = UILabel(frame:CGRect(x: 40 , y: -8, width: 20, height: 20))
    Label.font = UIFont.systemFont(ofSize: 11, weight: .regular)
    Label.textColor = .white
    Label.text = String(zangredNum)
    Label.textAlignment = .center
    Label.backgroundColor = .rgba(238,68,61,1)
    Label.layer.cornerRadius = 10
    Label.layer.masksToBounds = true
    btn.addSubview(Label)
    
    return btn
}()

/// 评价按钮
lazy var commentBtn: UIButton = {
    index = 1
    let btn = UIButton(frame:CGRect(x: (CGFloat(index+1))*gap + ((CGFloat(index)*40)), y: 29, width: 40, height: 40))
    btn.titleLabel?.font = UIFont.systemFont(ofSize: 14,weight:.medium)
    btn.setTitleColor(.rgb(51, 51, 51), for: UIControl.State.normal)
    btn.setImageInsets(image: UIImage(named: "new_comment"), title: "评论", titlePosition: .bottom, additionalSpacing: 6, state: UIControl.State.normal)
    //小红点
    let Label = UILabel(frame:CGRect(x:40, y: -8, width: 20, height: 20))
    Label.font = UIFont.systemFont(ofSize: 11, weight: .regular)
    Label.textColor = .white
    Label.text = String(commentredNum)
    
    Label.textAlignment = .center
    Label.backgroundColor = .rgba(238,68,61,1)
    Label.layer.cornerRadius = 10
    Label.layer.masksToBounds = true

    btn.addSubview(Label)
    
    return btn
}()

/// 动态按钮
lazy var dtBtn: UIButton = {
    index = 2
    let btn = UIButton(frame:CGRect(x: (CGFloat(index+1))*gap + ((CGFloat(index)*40)), y: 29, width: 40, height: 40))
    btn.titleLabel?.font = UIFont.systemFont(ofSize: 14,weight:.medium)
    btn.backgroundColor = AppColor.bgFBFBFB
    btn.setTitleColor(.rgb(51, 51, 51), for: UIControl.State.normal)
    btn.setImageInsets(image: UIImage(named: "new_dt"), title: "动态", titlePosition: .bottom, additionalSpacing: 6, state: UIControl.State.normal)
    //小红点
    let Label = UILabel(frame:CGRect(x:40 , y: -8, width: 20, height: 20))
    Label.font = UIFont.systemFont(ofSize: 11, weight: .regular)
    Label.textColor = .white
    Label.text = String(dtredNum)
    Label.textAlignment = .center
    Label.backgroundColor = .rgba(238,68,61,1)
    Label.layer.cornerRadius = 10
    Label.layer.masksToBounds = true

    btn.addSubview(Label)
    
    return btn
}()

/// 粉丝按钮
lazy var fsBtn: UIButton = {
    index = 3
    let btn = UIButton(frame:CGRect(x: (CGFloat(index+1))*gap + ((CGFloat(index)*40)), y: 29, width: 40, height: 40))
    btn.titleLabel?.font = UIFont.systemFont(ofSize: 14,weight:.medium)
    btn.setTitleColor(.rgb(51, 51, 51), for: UIControl.State.normal)
    btn.backgroundColor = AppColor.bgFBFBFB
    btn.setImageInsets(image: UIImage(named: "new_fs"), title: "粉丝", titlePosition: .bottom, additionalSpacing: 6, state: UIControl.State.normal)
    //小红点
    let Label = UILabel(frame:CGRect(x: 40, y: -8, width: 20, height: 20))
    Label.font = UIFont.systemFont(ofSize: 11, weight: .regular)
    Label.textColor = .white
    Label.text = String(fsredNum)
    Label.textAlignment = .center
    Label.backgroundColor = .rgba(238,68,61,1)
    Label.layer.cornerRadius = 10
    Label.layer.masksToBounds = true

    btn.addSubview(Label)
    
    return btn
}()


required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}


override init(frame: CGRect) {
    super.init(frame: frame)
    initUI()
    initBind()
    initData(
    
    )
}

}

extension MessageNewsMainHeaderView{

func initUI() {
    layoutsubview()
}

func initBind(){
    
    // 点赞
    zanBtn.rx.tap.subscribe(onNext:  { [unowned self] () in
        
    let vc = ZanMessagesViewController()
        vc.hidesBottomBarWhenPushed = true
        getCurrentVC()?.navigationController?.pushViewController(vc, animated: true)
    
    }).disposed(by:bag)
    
    // 评论
    commentBtn.rx.tap.subscribe(onNext:  { [unowned self] () in
        
    let vc = CommentMessagesViewController()
        vc.hidesBottomBarWhenPushed = true
        getCurrentVC()?.navigationController?.pushViewController(vc, animated: true)
    
    }).disposed(by:bag)
    
    // 动态
    dtBtn.rx.tap.subscribe(onNext:  { [unowned self] () in
        
    let vc = DynamicNewsController()
        vc.hidesBottomBarWhenPushed = true
        getCurrentVC()?.navigationController?.pushViewController(vc, animated: true)
    
    }).disposed(by:bag)
    
    // 粉丝
    
    fsBtn.rx.tap.subscribe(onNext:  { [unowned self] () in
        
    let vc = FansNewsController()
        vc.hidesBottomBarWhenPushed = true
        getCurrentVC()?.navigationController?.pushViewController(vc, animated: true)
    
    }).disposed(by:bag)

}

/// MARK: 赋值模块
func initData() {
 
}

}

/// MARK: UI布局

extension MessageNewsMainHeaderView{

func layoutsubview() {

    self.backgroundColor = AppColor.bgFBFBFB
    self.addSubview(mainView)
    mainView.addSubview(zanBtn)
    mainView.addSubview(commentBtn)
    mainView.addSubview(dtBtn)
    mainView.addSubview(fsBtn)

    // 把类定义成属性
    // let linebutton = LineButton()
    // 再创建
    // mainView = linebutton.creatLineButton(dataArr: dataArr, buttonSize: CGSize(width: 44, height: 44))
    // self.addSubview(mainView)
    
}

}

```

你可能感兴趣的:(Swift UI 布局和通知销毁)