// // YinUserInfoHeadView.swift // yinmeng-ios // // Created by duoban on 2024/4/1. // import UIKit import Nuke enum YinUserInfoHeadViewType { case grade,myRoom,fans,attention,visitor,footprint,pay,nobility } typealias clickBtnBlock = (_ type:YinUserInfoHeadViewType)->Void class YinUserInfoHeadView: UIView{ override init(frame: CGRect) { super.init(frame: frame) setUILayout() addViewTarget() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } private func setUILayout(){ addSubview(backgroundImageView) addSubview(avatarView) addSubview(headwearView) addSubview(nickContainer) addSubview(tagContainer) addSubview(experIconView) addSubview(charmIconView) addSubview(headItemStackView) addSubview(bgBtnStackView) addSubview(arrowView) bgBtnStackView.addSubview(btnStackView) nickContainer.addArrangedSubview(nickView) nickContainer.addArrangedSubview(genderView) tagContainer.addArrangedSubview(idView) tagContainer.addArrangedSubview(copyView) headItemStackView.addArrangedSubview(attentionItemView) headItemStackView.addArrangedSubview(fansItemView) headItemStackView.addArrangedSubview(visitorItemView) headItemStackView.addArrangedSubview(footprintItemView) btnStackView.addArrangedSubview(gradeView) btnStackView.addArrangedSubview(myRoomView) btnStackView.addArrangedSubview(walletView) btnStackView.addArrangedSubview(nobleView) backgroundImageView.snp.makeConstraints { make in make.leading.trailing.top.equalTo(0) make.height.equalTo(UIDevice.scaleWidth(width: 125)) } avatarView.snp.makeConstraints { make in make.width.height.equalTo(UIDevice.scaleWidth(width: 68)) make.leading.equalTo(UIDevice.scaleWidth(width: 25)) make.top.equalTo(UIDevice.scaleWidth(width: 35)+StatusBarHeight) } headwearView.snp.makeConstraints { make in make.center.equalTo(avatarView) make.width.equalTo(avatarView.snp.width).multipliedBy(1.31) make.height.equalTo(headwearView.snp.width) } nickContainer.snp.makeConstraints { make in make.top.equalTo(avatarView.snp.top).offset(3) make.leading.equalTo(avatarView.snp.trailing).offset((UIDevice.scaleWidth(width: 16))) make.height.equalTo(UIDevice.scaleWidth(width: 21)) } tagContainer.snp.makeConstraints { make in make.leading.equalTo(nickContainer) make.top.equalTo(nickContainer.snp.bottom).offset(UIDevice.scaleWidth(width: 2)) make.height.equalTo(UIDevice.scaleWidth(width: 20)) } copyView.snp.makeConstraints { make in make.width.height.equalTo(UIDevice.scaleWidth(width: 10)) } experIconView.snp.makeConstraints { make in make.leading.equalTo(nickContainer) make.top.equalTo(tagContainer.snp.bottom).offset(UIDevice.scaleWidth(width: 2)) make.height.equalTo(UIDevice.scaleWidth(width: 18)) make.width.equalTo(UIDevice.scaleWidth(width: 21)) } charmIconView.snp.makeConstraints { make in make.top.equalTo(tagContainer.snp.bottom).offset(UIDevice.scaleWidth(width: 2)) make.height.equalTo(UIDevice.scaleWidth(width: 18)) make.width.equalTo(UIDevice.scaleWidth(width: 21)) make.leading.equalTo(experIconView.snp.trailing).offset((UIDevice.scaleWidth(width: 5))) } arrowView.snp.makeConstraints { make in make.width.height.equalTo(UIDevice.scaleWidth(width: 30)) make.trailing.equalTo(-UIDevice.scaleWidth(width: 10)) make.centerY.equalTo(avatarView) } headItemStackView.snp.makeConstraints { make in make.top.equalTo(avatarView.snp.bottom).offset(UIDevice.scaleWidth(width: 16)) make.height.equalTo(UIDevice.scaleWidth(width: 45)) make.leading.trailing.equalTo(self).inset(UIDevice.scaleWidth(width: 15)) } let width = (ScreenWidth - UIDevice.scaleWidth(width: 30))/4.0 let height = UIDevice.scaleWidth(width: 45) attentionItemView.snp.makeConstraints { make in make.size.equalTo(CGSize(width: width, height: height)) } fansItemView.snp.makeConstraints { make in make.size.equalTo(CGSize(width: width, height: height)) } visitorItemView.snp.makeConstraints { make in make.size.equalTo(CGSize(width: width, height: height)) } footprintItemView.snp.makeConstraints { make in make.size.equalTo(CGSize(width: width, height: height)) } bgBtnStackView.snp.makeConstraints { make in make.leading.trailing.equalTo(self).inset(UIDevice.scaleWidth(width: 15)) make.top.equalTo(headItemStackView.snp.bottom).offset(UIDevice.scaleWidth(width: 2)) make.height.equalTo(UIDevice.scaleWidth(width: 108)) make.bottom.equalTo(-UIDevice.scaleWidth(width: 0)) } let space = (ScreenWidth - UIDevice.scaleWidth(width: 310)) / 5.0 btnStackView.snp.makeConstraints { make in make.leading.trailing.equalTo(bgBtnStackView).inset(space) make.centerY.equalTo(bgBtnStackView) make.height.equalTo(UIDevice.scaleWidth(width: 74)) } } private func addViewTarget(){ let copyTap = UITapGestureRecognizer(target: self, action: #selector(copyAction)) tagContainer.addGestureRecognizer(copyTap) let attentionTap = UITapGestureRecognizer(target: self, action: #selector(attentionAction)) attentionItemView.addGestureRecognizer(attentionTap) let fansTap = UITapGestureRecognizer(target: self, action: #selector(fansAction)) fansItemView.addGestureRecognizer(fansTap) let visitorTap = UITapGestureRecognizer(target: self, action: #selector( visitorAction)) visitorItemView.addGestureRecognizer(visitorTap) let footprintTap = UITapGestureRecognizer(target: self, action: #selector(footprintAction)) footprintItemView.addGestureRecognizer(footprintTap) } @objc func visitorAction(){ if let clickBtnBlock = self.clickBtnBlock{ clickBtnBlock(.visitor) } } @objc func footprintAction(){ if let clickBtnBlock = self.clickBtnBlock{ clickBtnBlock(.footprint) } } @objc func fansAction(){ if let clickBtnBlock = self.clickBtnBlock{ clickBtnBlock(.fans) } } @objc func attentionAction(){ if let clickBtnBlock = self.clickBtnBlock{ clickBtnBlock(.attention) } } @objc func copyAction(){ if let erbanNo = userData?.erbanNo{ UIPasteboard.general.string = "\(erbanNo)" HUDTool.show(with: "复制成功") } } @objc func clickGradeBtnAction(){ if let clickBtnBlock = self.clickBtnBlock{ clickBtnBlock(.grade) } } @objc func clickMyRoomBtnAction(){ if let clickBtnBlock = self.clickBtnBlock{ clickBtnBlock(.myRoom) } } @objc func clickPayBtnAction(){ if let clickBtnBlock = self.clickBtnBlock{ clickBtnBlock(.pay) } } @objc func clickNobilityBtnAction(){ if let clickBtnBlock = self.clickBtnBlock{ clickBtnBlock(.nobility) } } var visitorCount = 0{ didSet{ visitorItemView.visitorCount = visitorCount } } //MARK: - 懒加载 var userData:UserObject? = nil{ didSet{ guard let _userData = userData else { return } headwearView.isHidden = true if let erbanNo = _userData.erbanNo{ idView.text = "ID:\(erbanNo)" } if let nick = _userData.nick{ nickView.text = nick.isEmpty ? "" : nick } if _userData.isReview { Nuke.loadImage(with: _userData.reviewingAvatar, into: avatarView) { result in switch result { case .success(let imageResponse): let image = imageResponse.image let waterImage = UIImage(named: "yin_mine_avatar_reviewing")! let ratio = waterImage.size.height / waterImage.size.width let y = image.size.height * (1 - ratio) self.avatarView.image = UIImage.getWatermarkImage(image: image, watermark: waterImage, watermarkRect: CGRect(x: 0, y: y, width: image.size.width, height: image.size.height * ratio)) break default: break } } }else{ Nuke.loadImage(with: _userData.avatar, into: avatarView, completion: nil) } genderView.image = _userData.gender == .Boy ? UIImage(named: "yin_plane_star_man") : UIImage(named: "yin_plane_star__woman") if let experImage = _userData.userLevelVo?.experNewUrl{ experIconView.image = UIImage(named: experImage) let experWidth = (experIconView.image?.size.width ?? 0) * UIDevice.scaleWidth(width: 18) / (experIconView.image?.size.height ?? 1) experIconView.snp.updateConstraints { make in make.width.equalTo(experWidth) } } if let charmNewUrl = _userData.userLevelVo?.charmNewUrl{ charmIconView.image = UIImage(named: charmNewUrl) let charmWidth = (charmIconView.image?.size.width ?? 0) * UIDevice.scaleWidth(width: 18) / (charmIconView.image?.size.height ?? 1) charmIconView.snp.updateConstraints { make in make.width.equalTo(charmWidth) } } fansItemView.count = _userData.fansNum attentionItemView.count = _userData.followNum visitorItemView.count = _userData.visitNum footprintItemView.count = _userData.inRoomNum } } var clickBtnBlock:clickBtnBlock? private lazy var backgroundImageView:UIImageView = { let _backgroundImageView = UIImageView() let image = UIImage.gradient([ThemeColor(hexStr: "#DCF6FF"),ThemeColor(hexStr: "#F8F8FA"),ThemeColor(hexStr: "#F8F8FB")],size:CGSizeMake(ScreenWidth, UIDevice.scaleWidth(width: 125)),direction: .vertical) _backgroundImageView.image = image return _backgroundImageView }() private lazy var avatarView:UIImageView = { let _avatarView = UIImageView() _avatarView.layer.masksToBounds = true _avatarView.layer.cornerRadius = UIDevice.scaleWidth(width: 68)/2 _avatarView.layer.borderWidth = 1 _avatarView.layer.borderColor = UIColor.white.cgColor return _avatarView }() private lazy var nickContainer:UIStackView = { let _nickContainer = UIStackView() _nickContainer.axis = .horizontal _nickContainer.distribution = .fill _nickContainer.alignment = .center _nickContainer.spacing = UIDevice.scaleWidth(width: 4) return _nickContainer }() private lazy var nickView:UILabel = { let _nickView = UILabel.getCustomLabel(text: "",font: UIFont.getScaleFont(ofSize: 16, weight: .medium),color: ThemeColor(hexStr: "#282828")) return _nickView }() private lazy var tagContainer:UIStackView = { let _tagContainer = UIStackView() _tagContainer.axis = .horizontal _tagContainer.distribution = .fill _tagContainer.alignment = .center _tagContainer.spacing = UIDevice.scaleWidth(width: 5) return _tagContainer }() private lazy var idView:UILabel = { let _idView = UILabel.getCustomLabel(text: "",font: UIFont.getScaleFont(ofSize: 13, weight: .regular),color: ThemeColor(hexStr: "#878B9C")) return _idView }() private lazy var genderView:UIImageView = { let _genderView = UIImageView() return _genderView }() private lazy var experIconView:UIImageView = { let _experView = UIImageView() return _experView }() private lazy var charmIconView:UIImageView = { let _charmView = UIImageView() return _charmView }() private lazy var copyView:UIImageView = { let _copyView = UIImageView() _copyView.image = UIImage(named: "yin_mine_user_head_copy") return _copyView }() private lazy var headwearView:YYAnimatedImageView = { let _headwearView = YYAnimatedImageView() _headwearView.contentMode = .scaleAspectFit _headwearView.layer.masksToBounds = true _headwearView.layer.cornerRadius = UIDevice.scaleWidth(width: 10) return _headwearView }() private lazy var headItemStackView:UIStackView = { let _headItemStackView = UIStackView() _headItemStackView.axis = .horizontal _headItemStackView.distribution = .fill _headItemStackView.alignment = .fill return _headItemStackView }() private lazy var attentionItemView:YinUserInfoHeadItemView = { let _attentionItemView = YinUserInfoHeadItemView(frame: .zero) _attentionItemView.text = "关注" return _attentionItemView }() private lazy var fansItemView:YinUserInfoHeadItemView = { let _fansItemView = YinUserInfoHeadItemView(frame: .zero) _fansItemView.text = "粉丝" return _fansItemView }() private lazy var visitorItemView:YinUserInfoHeadItemView = { let _visitorItemView = YinUserInfoHeadItemView(frame: .zero) _visitorItemView.text = "访客" _visitorItemView.isMine = true return _visitorItemView }() private lazy var footprintItemView:YinUserInfoHeadItemView = { let _footprintItemView = YinUserInfoHeadItemView(frame: .zero) _footprintItemView.text = "足迹" return _footprintItemView }() private lazy var bgBtnStackView:UIView = { let _bgBtnStackView = UIView() _bgBtnStackView.backgroundColor = .white _bgBtnStackView.layer.cornerRadius = UIDevice.scaleWidth(width: 10) return _bgBtnStackView }() private lazy var btnStackView:UIStackView = { let _btnStackView = UIStackView() _btnStackView.axis = .horizontal _btnStackView.distribution = .equalSpacing _btnStackView.alignment = .fill let space = (ScreenWidth - UIDevice.scaleWidth(width: 310)) / 5.0 _btnStackView.spacing = space return _btnStackView }() private lazy var gradeView:UIButton = { let _gradeView = UIButton.getCustomBtn(text: "我的等级",font: UIFont.getScaleFont(ofSize: 12, weight: .medium),color: ThemeColor(hexStr: "#282828"),bgImage: UIImage(named: "yin_mine_btn_my_grade")) _gradeView.addTarget(self, action: #selector(clickGradeBtnAction), for: .touchUpInside) _gradeView.titleEdgeInsets = UIEdgeInsets(top: UIDevice.scaleWidth(width: 12), left: 0, bottom: 0, right: 0) return _gradeView }() private lazy var myRoomView:UIButton = { let _myRoomView = UIButton.getCustomBtn(text: "我的房间",font: UIFont.getScaleFont(ofSize: 12, weight: .medium),color: ThemeColor(hexStr: "#282828"),bgImage: UIImage(named: "yin_mine_btn_my_room")) _myRoomView.titleEdgeInsets = UIEdgeInsets(top: UIDevice.scaleWidth(width: 12), left: 0, bottom: 0, right: 0) _myRoomView.addTarget(self, action: #selector(clickMyRoomBtnAction), for: .touchUpInside) return _myRoomView }() private lazy var walletView:UIButton = { let _walletView = UIButton.getCustomBtn(text: "钱包充值",font: UIFont.getScaleFont(ofSize: 12, weight: .medium),color: ThemeColor(hexStr: "#282828"),bgImage: UIImage(named: "yin_mine_btn_wallet_recharge")) _walletView.titleEdgeInsets = UIEdgeInsets(top: UIDevice.scaleWidth(width: 12), left: 0, bottom: 0, right: 0) _walletView.addTarget(self, action: #selector(clickPayBtnAction), for: .touchUpInside) return _walletView }() private lazy var nobleView:UIButton = { let _nobleView = UIButton.getCustomBtn(text: "特权贵族",font: UIFont.getScaleFont(ofSize: 12, weight: .medium),color: ThemeColor(hexStr: "#282828"),bgImage: UIImage(named: "yin_mine_btn_noble_privilege")) _nobleView.titleEdgeInsets = UIEdgeInsets(top: UIDevice.scaleWidth(width: 12), left: 0, bottom: 0, right: 0) _nobleView.addTarget(self, action: #selector(clickNobilityBtnAction), for: .touchUpInside) return _nobleView }() private lazy var arrowView:UIButton = { let _arrowView = UIButton.getCustomBtn(image: UIImage(named: "yin_mine_header_arrow")) _arrowView.isHidden = true return _arrowView }() }