jsdw_ios/QuickLocation/Section/Share/SharePopView.swift

200 lines
5.8 KiB
Swift

//
// SharePopView.swift
// QuickLocation
//
// Created by on 2026/7/3.
//
import UIKit
class SharePopView: UIView {
private static let shared = SharePopView(frame: CGRect(origin: .zero, size: kScreenSize))
private var shareURL: String = ""
private let contentHeight: CGFloat = 191 + kSafeBottomMargin
static func show(shareURL: String) {
guard let superView = kKeyWindow else { return }
if SharePopView.shared.superview != nil {
SharePopView.shared.dismiss(animated: false)
}
SharePopView.shared.shareURL = shareURL
SharePopView.shared.bgView.alpha = 0
superView.addSubview(SharePopView.shared)
superView.bringSubviewToFront(SharePopView.shared)
SharePopView.shared.contentView.transform = CGAffineTransform(translationX: 0, y: SharePopView.shared.contentHeight)
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut) {
SharePopView.shared.bgView.alpha = 1
SharePopView.shared.contentView.transform = .identity
}
}
private func dismiss(animated: Bool = true) {
if animated {
UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseIn) {
self.bgView.alpha = 0
self.contentView.transform = CGAffineTransform(translationX: 0, y: self.contentHeight)
} completion: { _ in
self.removeFromSuperview()
}
} else {
removeFromSuperview()
}
}
@objc private func handleBgTap() {
dismiss()
}
@objc private func handleWeChatTap() {
shareToWeChat(scene: 0)
}
@objc private func handleMomentsTap() {
shareToWeChat(scene: 1)
}
@objc private func handleLinkTap() {
UIPasteboard.general.string = shareURL
DLToast.showSuccess(text: "链接已复制")
dismiss()
}
private func shareToWeChat(scene: Int32) {
let message = WXMediaMessage()
message.title = kAppName
let webpage = WXWebpageObject()
webpage.webpageUrl = shareURL
message.mediaObject = webpage
let req = SendMessageToWXReq()
req.bText = false
req.message = message
req.scene = scene
WXApi.send(req)
dismiss()
}
// MARK: - UI
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = .clear
addSubview(bgView)
bgView.addSubview(contentView)
contentView.addSubview(titleLabel)
contentView.addSubview(shareStack)
contentView.addSubview(closeBtn)
bgView.layoutChain.edges()
contentView.layoutChain
.edges(excludingEdge: .top)
.height(contentHeight)
titleLabel.layoutChain
.top(20)
.centerX()
closeBtn.layoutChain
.centerY(titleLabel)
.right(20)
.width(20)
.height(20)
shareStack.layoutChain
.topToBottomOfView(titleLabel, offset: 30)
.centerX()
let tap = UITapGestureRecognizer(target: self, action: #selector(handleBgTap))
bgView.addGestureRecognizer(tap)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Lazy Views
private lazy var bgView: UIView = {
let view = UIView()
view.backgroundColor = .black.withAlphaComponent(0.5)
return view
}()
private lazy var contentView: UIView = {
let view = UIView()
view.backgroundColor = .white
view.layer.cornerRadius = 26
view.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
view.clipsToBounds = true
return view
}()
private lazy var titleLabel: UILabel = {
let label = UILabel()
label.text = "分享"
label.font = .systemFont(ofSize: 16, weight: .medium)
label.textColor = UIColor(hexStr: "#333333")
return label
}()
private lazy var shareStack: UIStackView = {
let stack = UIStackView(arrangedSubviews: [
makeShareItem(icon: "Share/friend", title: "微信", action: #selector(handleWeChatTap)),
makeShareItem(icon: "Share/moments", title: "朋友圈", action: #selector(handleMomentsTap)),
makeShareItem(icon: "Share/url", title: "链接", action: #selector(handleLinkTap))
])
stack.axis = .horizontal
stack.spacing = 50
stack.alignment = .center
return stack
}()
private lazy var closeBtn: UIButton = {
let btn = UIButton(type: .custom)
btn.setImage(UIImage(named: "Group/close"), for: .normal)
btn.addTarget(self, action: #selector(handleBgTap), for: .touchUpInside)
btn.extendEdgeInsets = UIEdgeInsets(top: 15, left: 20, bottom: 20, right: 20)
return btn
}()
private func makeShareItem(icon: String, title: String, action: Selector) -> UIView {
let container = UIView()
let imgView = UIImageView(image: UIImage(named: icon))
imgView.contentMode = .scaleAspectFit
container.addSubview(imgView)
imgView.layoutChain
.top(0)
.centerX()
.width(50)
.height(50)
let label = UILabel()
label.text = title
label.font = .systemFont(ofSize: 12)
label.textColor = UIColor(hexStr: "#666666")
label.textAlignment = .center
container.addSubview(label)
label.layoutChain
.topToBottomOfView(imgView, offset: 8)
.centerX()
.bottom(0)
container.layoutChain.width(60).height(78)
let btn = UIButton(type: .custom)
container.addSubview(btn)
btn.layoutChain.edges()
btn.addTarget(self, action: action, for: .touchUpInside)
return container
}
}