按 msext gameController.m:575-597 / SharePanel.m 模式落地分享框架。
新增 Source/Share/:
- SharePlatform.swift:
* SharePlatform 协议(name / isInstalled / share async → ShareResult)
* ShareContent struct(nonisolated Sendable,6 字段 sharefriend/sharetype/
type/webpageUrl/title/desc)
* ShareResult enum (success / cancelled / notInstalled / failed)
* ShareScene enum (friend / timeline)
- SharePanel.swift:
* @MainActor UIView,半透明黑底覆盖全屏 + 底部圆角内容视图滑入
* 三按钮(微信绿 / QQ 蓝 / 抖音黑)+ label 等间距居中布局
* 已安装平台按钮高亮,未装置灰
* 点击空白区 dismiss + completion(.cancelled)
* 点击按钮 dismiss + 触发对应 SharePlatform.share + completion(result)
* 0.25s 滑入/滑出动画
* 与 msext SharePanel.m 等价行为
- ShareCenter.swift:
* sharefriend == "1" → SharePanel.show 三选一
* sharefriend == "2" → WechatShare.share scene: .timeline 朋友圈
* 与 msext gameController.m:585 行为 1:1
- WechatShare.swift:stub(Phase 4.E 等微信 SDK 接入)
- QQShare.swift:stub + canOpenURL("mqqapi://") 检测(Phase 4.C 升级
URL Scheme 真实调起)
- DouyinShare.swift:stub + canOpenURL("snssdk1128://") 检测(Phase 4.D 升级
URL Scheme + Photos)
FriendsShareHandler 升级:
- 入参解析为 ShareContent
- 调 ShareCenter.dispatch
- completion 内根据 result 触发 sharesuccess 反向 callback:
* success / cancelled → bridge.call("sharesuccess", {success:"2", type:sharefriend})
* notInstalled / failed → 不发(H5 业务期 timeout 后自行 fallback,msext 乐观策略)
ShareContent 标 nonisolated 解 Swift 6 actor 隔离(项目默认 MainActor isolation
让 struct 跨 actor 受限)。
BuildProject 通过
Plan §5 Phase 4.2 勾选
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
167 lines
6.4 KiB
Swift
167 lines
6.4 KiB
Swift
//
|
||
// SharePanel.swift
|
||
// ylgamehall
|
||
//
|
||
// 原生分享面板:底部弹出三选一(微信好友 / QQ / 抖音)。
|
||
//
|
||
// 与 msext SharePanel.m 等价行为:
|
||
// - 半透明黑底覆盖全屏,点击空白区关闭
|
||
// - 内容视图从屏幕底部滑入,0.25s 动画
|
||
// - 三个圆形按钮 + label,等间距居中布局
|
||
// - 点击按钮后先 dismiss 再触发对应 SharePlatform.share
|
||
//
|
||
// Phase 4.B 阶段:UI 完整 + 调用 SharePlatform stub;
|
||
// Phase 4.C/4.D 阶段:SharePlatform 升级为真实 URL Scheme 实现,本类不变。
|
||
//
|
||
|
||
import UIKit
|
||
import os.log
|
||
|
||
@MainActor
|
||
public final class SharePanel: UIView {
|
||
|
||
private static let log = Logger(subsystem: "ylgamehall", category: "SharePanel")
|
||
|
||
private static let panelHeight: CGFloat = 200
|
||
private static let buttonSize: CGFloat = 60
|
||
private static let animationDuration: TimeInterval = 0.25
|
||
|
||
private let content: ShareContent
|
||
private let completion: (ShareResult) -> Void
|
||
|
||
private let contentView = UIView()
|
||
|
||
public init(content: ShareContent, completion: @escaping (ShareResult) -> Void) {
|
||
self.content = content
|
||
self.completion = completion
|
||
super.init(frame: UIScreen.main.bounds)
|
||
backgroundColor = UIColor(white: 0, alpha: 0)
|
||
setupUI()
|
||
}
|
||
|
||
@available(*, unavailable)
|
||
required init?(coder: NSCoder) {
|
||
fatalError("init(coder:) is not supported")
|
||
}
|
||
|
||
// MARK: - 公共入口
|
||
|
||
/// 弹出分享面板。任何时候同一个面板只允许一个实例。
|
||
public static func show(content: ShareContent, completion: @escaping (ShareResult) -> Void) {
|
||
guard let window = topWindow() else {
|
||
completion(.failed(reason: "no key window"))
|
||
return
|
||
}
|
||
let panel = SharePanel(content: content, completion: completion)
|
||
window.addSubview(panel)
|
||
panel.animateIn()
|
||
}
|
||
|
||
private static func topWindow() -> UIWindow? {
|
||
UIApplication.shared.connectedScenes
|
||
.compactMap { $0 as? UIWindowScene }
|
||
.flatMap(\.windows)
|
||
.first { $0.isKeyWindow }
|
||
}
|
||
|
||
// MARK: - UI 装配
|
||
|
||
private func setupUI() {
|
||
// 半透明背景手势
|
||
let tap = UITapGestureRecognizer(target: self, action: #selector(handleBackgroundTap(_:)))
|
||
addGestureRecognizer(tap)
|
||
|
||
// 内容视图(屏幕底部,初始 Y 在屏幕外)
|
||
let screen = UIScreen.main.bounds
|
||
contentView.frame = CGRect(x: 0, y: screen.height, width: screen.width, height: Self.panelHeight)
|
||
contentView.backgroundColor = .white
|
||
let mask = CAShapeLayer()
|
||
mask.path = UIBezierPath(
|
||
roundedRect: contentView.bounds,
|
||
byRoundingCorners: [.topLeft, .topRight],
|
||
cornerRadii: CGSize(width: 10, height: 10)
|
||
).cgPath
|
||
contentView.layer.mask = mask
|
||
addSubview(contentView)
|
||
|
||
// 三个按钮 + label 等间距居中布局
|
||
let buttonsContainer = UIView(frame: CGRect(x: 0, y: 30, width: screen.width, height: 110))
|
||
contentView.addSubview(buttonsContainer)
|
||
|
||
let buttons: [(title: String, platform: SharePlatform, color: UIColor)] = [
|
||
("微信", WechatShare.shared, UIColor(red: 0.06, green: 0.69, blue: 0.36, alpha: 1)),
|
||
("QQ", QQShare.shared, UIColor(red: 0.12, green: 0.51, blue: 0.97, alpha: 1)),
|
||
("抖音", DouyinShare.shared, .black)
|
||
]
|
||
|
||
let totalButtonsWidth = Self.buttonSize * CGFloat(buttons.count)
|
||
let spacing = (screen.width - totalButtonsWidth) / CGFloat(buttons.count + 1)
|
||
for (index, b) in buttons.enumerated() {
|
||
let x = spacing + CGFloat(index) * (Self.buttonSize + spacing)
|
||
let button = UIButton(type: .system)
|
||
button.frame = CGRect(x: x, y: 0, width: Self.buttonSize, height: Self.buttonSize)
|
||
button.backgroundColor = b.platform.isInstalled ? b.color : .lightGray
|
||
button.layer.cornerRadius = Self.buttonSize / 2
|
||
button.setTitle(b.title.prefix(1).description, for: .normal)
|
||
button.setTitleColor(.white, for: .normal)
|
||
button.titleLabel?.font = .systemFont(ofSize: 22, weight: .medium)
|
||
button.tag = index
|
||
button.addTarget(self, action: #selector(handleButtonTap(_:)), for: .touchUpInside)
|
||
buttonsContainer.addSubview(button)
|
||
|
||
let label = UILabel(frame: CGRect(x: x, y: Self.buttonSize + 8, width: Self.buttonSize, height: 20))
|
||
label.text = b.title
|
||
label.textAlignment = .center
|
||
label.font = .systemFont(ofSize: 13)
|
||
label.textColor = .darkGray
|
||
buttonsContainer.addSubview(label)
|
||
}
|
||
}
|
||
|
||
// MARK: - 动画
|
||
|
||
private func animateIn() {
|
||
UIView.animate(withDuration: Self.animationDuration) {
|
||
self.backgroundColor = UIColor(white: 0, alpha: 0.5)
|
||
var frame = self.contentView.frame
|
||
frame.origin.y = UIScreen.main.bounds.height - Self.panelHeight
|
||
self.contentView.frame = frame
|
||
}
|
||
}
|
||
|
||
private func dismiss(then action: (() -> Void)? = nil) {
|
||
UIView.animate(withDuration: Self.animationDuration, animations: {
|
||
self.backgroundColor = UIColor(white: 0, alpha: 0)
|
||
var frame = self.contentView.frame
|
||
frame.origin.y = UIScreen.main.bounds.height
|
||
self.contentView.frame = frame
|
||
}, completion: { _ in
|
||
self.removeFromSuperview()
|
||
action?()
|
||
})
|
||
}
|
||
|
||
// MARK: - 事件
|
||
|
||
@objc private func handleBackgroundTap(_ gesture: UITapGestureRecognizer) {
|
||
let point = gesture.location(in: self)
|
||
if !contentView.frame.contains(point) {
|
||
dismiss { [self] in completion(.cancelled) }
|
||
}
|
||
}
|
||
|
||
@objc private func handleButtonTap(_ sender: UIButton) {
|
||
let platforms: [SharePlatform] = [WechatShare.shared, QQShare.shared, DouyinShare.shared]
|
||
let platform = platforms[sender.tag]
|
||
let scene: ShareScene = content.sharefriend == "2" ? .timeline : .friend
|
||
let snapshotContent = content
|
||
dismiss {
|
||
Task { @MainActor in
|
||
let result = await platform.share(snapshotContent, scene: scene)
|
||
Self.log.debug("share via \(platform.name, privacy: .public) result=\(String(describing: result), privacy: .public)")
|
||
self.completion(result)
|
||
}
|
||
}
|
||
}
|
||
}
|