| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435 |
- //
- // MOGiftBottomMenuView.swift
- // MiMoLive
- //
- // Created by OneeChan on 2025/9/5.
- //
- import Foundation
- import UIKit
- import SnapKit
- @objc protocol MOGiftBottomMenuViewDelegate {
- // Recharge
- func giftBottomMenuViewClickRecharge(view: MOGiftBottomMenuView)
-
- // Gift
- func giftBottomMenuViewDidSelectNum(view: MOGiftBottomMenuView, num: Int, isCustom: Bool)
- func giftBottomMenuViewClickSendGift(view: MOGiftBottomMenuView)
- func giftBottomMenuViewClickCustom(view: MOGiftBottomMenuView)
-
- // Bag
- func giftBottomMenuViewClickSendBag(view: MOGiftBottomMenuView)
-
- // Props
- func giftBottomMenuViewClickUse(view: MOGiftBottomMenuView)
- }
- @objcMembers
- class MOGiftBottomMenuView: UIView {
- weak var delegate: MOGiftBottomMenuViewDelegate?
-
- @objc enum GiftBottomMenuType: Int {
- case none
- case gift
- case bag
- case props
- }
- private var curType: GiftBottomMenuType = .gift
-
- private var giftView: UIView?
- private var diamondLabel: UILabel?
- private var sendGiftView: MOSendGiftView?
-
- private var bagView: UIView?
-
- private var propView: UIView?
- private var usePropButon: MOGiftGradientButton?
-
- override init(frame: CGRect) {
- super.init(frame: frame)
-
- setupViews()
- updateViewType(curType)
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- func updateSelectItem(_ item: Any?) {
- guard let item else {
- updateViewType(.none)
- return
- }
- if let item = item as? MOGiftlist {
- if item.isBag {
- updateViewType(.bag)
- } else {
- enableGiftSendView(true)
- updateViewType(.gift)
- }
- } else if let item = item as? MOShopList {
- updateViewType(.props)
- if item.used {
- usePropButon?.title = NSLocalizedString("mimo_2_gift_list_prpps_a_use", comment: "")
- } else {
- usePropButon?.title = NSLocalizedString("mimo_2_gift_list_prpps_use", comment: "")
- }
- } else {
- updateViewType(.none)
- }
- }
-
- func updateViewType(_ type: GiftBottomMenuType) {
- curType = type
- let showView: UIView? = switch type {
- case .none:
- nil
- case .gift:
- self.giftView
- case .bag:
- self.bagView
- case .props:
- self.propView
- }
- let showings = [self.giftView, self.bagView, self.propView]
- .filter { $0?.isHidden == false && $0 != showView }
- showings.forEach { $0?.isHidden = true }
-
- // 已经在展示,不需要调整
- if showView?.isHidden == false { return }
- showView?.isHidden = false
- }
-
- func updateUserDiamond(_ count: Double) {
- var zuanNumStr = ""
- if count < 10_000_000 { // 1千万以下
- zuanNumStr = String(format: "%.f", count)
- } else if count < 1_000_000_000 { // 1千万到10亿
- let firstTwoDigits = Int(count / 10_000)
- zuanNumStr = String(
- format: "%ld.%02ldM",
- firstTwoDigits / 100,
- firstTwoDigits % 100
- )
- } else { // 10亿以上
- let firstTwoDigits = Int(count / 10_000_000)
- zuanNumStr = String(
- format: "%ld.%02ldB",
- firstTwoDigits / 100,
- firstTwoDigits % 100
- )
- }
- diamondLabel?.text = "\(zuanNumStr)"
- }
-
- func selectGiftNum(_ num: Int) {
- sendGiftView?.updateGiftNum(num)
- }
-
- func curGiftNum() -> Int {
- sendGiftView?.curGiftNum() ?? 1
- }
-
- func enableGiftSendView(_ enable: Bool) {
- sendGiftView?.isHidden = !enable
- }
-
- func enableGiftNum(_ enable: Bool) {
- sendGiftView?.enableGiftNum(enable)
- }
-
- func updateCustomNum(_ num: Int) {
- sendGiftView?.updateCustomNum(num)
- }
- }
- // MARK: Private
- extension MOGiftBottomMenuView {
- }
- // MARK: UI 事件
- extension MOGiftBottomMenuView {
- @objc func handleRechargeButtonClick() {
- delegate?.giftBottomMenuViewClickRecharge(view: self)
- }
-
- @objc func handleSendBagButtonClick() {
- delegate?.giftBottomMenuViewClickSendBag(view: self)
- }
-
- @objc func handleUsePropsButtonClick() {
- delegate?.giftBottomMenuViewClickUse(view: self)
- }
- }
- // MARK: MOSendGiftDelegate
- extension MOGiftBottomMenuView: MOSendGiftViewDelegate {
- func sendGiftViewDidSelectNum(
- view: MOSendGiftView,
- num: Int,
- isCustom: Bool
- ) {
- delegate?
- .giftBottomMenuViewDidSelectNum(
- view: self,
- num: num,
- isCustom: isCustom
- )
- }
-
- func sendGiftViewClickSend(view: MOSendGiftView) {
- delegate?.giftBottomMenuViewClickSendGift(view: self)
- }
-
- func sendGiftViewClickCustom(view: MOSendGiftView) {
- delegate?.giftBottomMenuViewClickCustom(view: self)
- }
- }
- // MARK: UI 视图构建
- extension MOGiftBottomMenuView {
- // UI 组装
- private func setupViews() {
- let giftView = buildGiftMenuView()
- giftView.isHidden = true
- addSubview(giftView)
- giftView.snp.makeConstraints { make in
- make.leading.trailing.bottom.equalToSuperview()
- make.top.bottom.equalToSuperview()
- }
- self.giftView = giftView
-
- let bagView = buildBagMenuView()
- bagView.isHidden = true
- addSubview(bagView)
- bagView.snp.makeConstraints { make in
- make.edges.equalToSuperview()
- }
- self.bagView = bagView
-
- let propsView = buildPropsMenuView()
- propsView.isHidden = true
- addSubview(propsView)
- propsView.snp.makeConstraints { make in
- make.edges.equalToSuperview()
- }
- self.propView = propsView
- }
-
- // Gift Menu
- private func buildGiftMenuView() -> UIView {
- let container = UIView()
-
- let diamondView = buildDiamondView()
- container.addSubview(diamondView)
- diamondView.snp.makeConstraints { make in
- make.top.bottom.equalToSuperview()
- make.leading.equalToSuperview().offset(12)
- }
-
- let sendView = MOSendGiftView()
- sendView.delegate = self
- container.addSubview(sendView)
- sendView.snp.makeConstraints { make in
- make.top.bottom.equalToSuperview()
- make.trailing.equalToSuperview().offset(-12)
- make.leading.equalTo(diamondView.snp.trailing).offset(8)
- }
- self.sendGiftView = sendView
-
- return container
- }
-
- // 钻石界面
- private func buildDiamondView() -> UIView {
- let container = UIView()
- container.layer.cornerRadius = 10
- container.backgroundColor = .white.withAlphaComponent(0.15)
-
- let diamond = UIImageView()
- diamond.image = UIImage(named: "icon_gift_zuan")
- container.addSubview(diamond)
- diamond.snp.makeConstraints { make in
- make.top.equalToSuperview().offset(7)
- make.bottom.equalToSuperview().offset(-7)
- make.leading.equalToSuperview().offset(10)
- make.width.height.equalTo(14)
- }
-
- let label = UILabel()
- label.textColor = .white
- label.font = MOTextTools.mediumFont(12.0)
- container.addSubview(label)
- label.snp.makeConstraints { make in
- make.centerY.equalToSuperview()
- make.leading.equalTo(diamond.snp.trailing).offset(3)
- }
- self.diamondLabel = label
-
- let arrow = UIImageView()
- arrow.image = UIImage(named: "icon_arrow_right_gray")
- container.addSubview(arrow)
- arrow.snp.makeConstraints { make in
- make.centerY.equalToSuperview()
- make.leading.equalTo(label.snp.trailing).offset(5)
- make.trailing.equalToSuperview().offset(-10)
- }
-
- let button = UIButton()
- button
- .addTarget(
- self,
- action: #selector(handleRechargeButtonClick),
- for: .touchUpInside
- )
- container.addSubview(button)
- button.snp.makeConstraints { make in
- make.edges.equalToSuperview()
- }
-
- return container
- }
-
- // Bag View
- private func buildBagMenuView() -> UIView {
- let container = UIView()
-
- let send = MOGiftGradientButton()
- send
- .addTarget(
- self,
- action: #selector(handleSendBagButtonClick),
- for: .touchUpInside
- )
- send.title = "Send"
- container.addSubview(send)
- send.snp.makeConstraints { make in
- make.top.bottom.equalToSuperview()
- make.trailing.equalToSuperview().offset(-12)
- }
-
- return container
- }
-
- // Props View
- private func buildPropsMenuView() -> UIView {
- let container = UIView()
-
- let use = MOGiftGradientButton()
- use.addTarget(
- self,
- action: #selector(handleUsePropsButtonClick),
- for: .touchUpInside
- )
- use.title = "Use"
- container.addSubview(use)
- use.snp.makeConstraints { make in
- make.top.bottom.equalToSuperview()
- make.trailing.equalToSuperview().offset(-12)
- }
- self.usePropButon = use
-
- return container
- }
- }
- private class MOGiftGradientButton: UIButton {
- var title = "" {
- didSet {
- textLabel.text = title
- }
- }
- private let gradientLayer = CAGradientLayer()
- private let textLabel = UILabel()
-
- override init(frame: CGRect) {
- super.init(frame: frame)
-
- setupViews()
- }
-
- required init?(coder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- override func layoutSubviews() {
- super.layoutSubviews()
-
- CATransaction.begin()
- CATransaction.setDisableActions(true)
- layer.sublayers?.forEach({ layer in
- if layer is CAGradientLayer {
- layer.frame = bounds
- }
- })
- CATransaction.commit()
- }
-
- private func setupViews() {
- let graLayer = CAGradientLayer()
- graLayer.startPoint = CGPoint(x: 0, y: 0.5)
- graLayer.endPoint = CGPoint(x: 1, y: 0.5)
- graLayer.colors = [
- UIColor(
- red: 255/255.0,
- green: 77/255.0,
- blue: 166/255.0,
- alpha: 1
- ).cgColor,
- UIColor(
- red: 67/255.0,
- green: 99/255.0,
- blue: 255/255.0,
- alpha: 1
- ).cgColor
- ];
- graLayer.locations = [0, 1.0]
- layer.insertSublayer(graLayer, at: 0)
- layer.cornerRadius = 10
- layer.masksToBounds = true
-
- textLabel.text = "Use"
- textLabel.font = MOTextTools.mediumFont(12.0)
- textLabel.textColor = .white
- addSubview(textLabel)
- textLabel.snp.makeConstraints { make in
- make.edges
- .equalToSuperview()
- .inset(UIEdgeInsets(top: 6, left: 16, bottom: 6, right: 16))
- }
- }
- }
- //#if DEBUG
- //import SwiftUI
- //
- //struct MOGiftBottomMenuViewPreview: UIViewRepresentable {
- // func makeUIView(context: Context) -> some UIView {
- // let container = UIView()
- // container.backgroundColor = .black
- //
- // let view = MOGiftBottomMenuView()
- // container.addSubview(view)
- // view.snp.makeConstraints { make in
- // make.centerY.equalToSuperview()
- // make.leading.trailing.equalToSuperview()
- // }
- //
- // return container
- // }
- //
- // func updateUIView(_ uiView: UIViewType, context: Context) {
- //
- // }
- //}
- //
- //#Preview(body: {
- // VStack {
- // MOGiftBottomMenuViewPreview()
- // }
- //})
- //
- //#endif
|