|
| 1 | +// |
| 2 | +// ScreenCaptureMaskView.swift |
| 3 | +// TUIRoomKit |
| 4 | +// |
| 5 | +// Created by 唐佳宁 on 2023/7/17. |
| 6 | +// 开启屏幕共享的蒙层View |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | + |
| 11 | +class ScreenCaptureMaskView: UIView { |
| 12 | + let sharingScreenLabel: UILabel = { |
| 13 | + let label = UILabel() |
| 14 | + label.text = .sharingScreenText |
| 15 | + label.textColor = .white |
| 16 | + label.textAlignment = .center |
| 17 | + label.adjustsFontSizeToFitWidth = true |
| 18 | + label.font = UIFont(name: "PingFangSC-Regular", size: 18) |
| 19 | + return label |
| 20 | + }() |
| 21 | + |
| 22 | + let stopScreenButton: UIButton = { |
| 23 | + let button = UIButton(type: .custom) |
| 24 | + button.backgroundColor = .red |
| 25 | + button.setTitle(.stopShareScreen, for: .normal) |
| 26 | + button.setTitleColor(.white, for: .normal) |
| 27 | + button.isEnabled = true |
| 28 | + button.layer.cornerRadius = 12 |
| 29 | + return button |
| 30 | + }() |
| 31 | + |
| 32 | + let screenRecordingLabel: UILabel = { |
| 33 | + let label = UILabel() |
| 34 | + label.textColor = .white |
| 35 | + label.backgroundColor = .black |
| 36 | + label.text = .screenRecordingText |
| 37 | + label.numberOfLines = 0 |
| 38 | + label.textAlignment = .center |
| 39 | + label.font = UIFont(name: "PingFangSC-Regular", size: 18) |
| 40 | + return label |
| 41 | + }() |
| 42 | + |
| 43 | + // MARK: - view layout |
| 44 | + private var isViewReady: Bool = false |
| 45 | + override func didMoveToWindow() { |
| 46 | + super.didMoveToWindow() |
| 47 | + guard !isViewReady else { return } |
| 48 | + backgroundColor = UIColor(0x1B1E26) |
| 49 | + constructViewHierarchy() |
| 50 | + activateConstraints() |
| 51 | + bindInteraction() |
| 52 | + isViewReady = true |
| 53 | + } |
| 54 | + |
| 55 | + private func constructViewHierarchy() { |
| 56 | + addSubview(sharingScreenLabel) |
| 57 | + addSubview(stopScreenButton) |
| 58 | + addSubview(screenRecordingLabel) |
| 59 | + } |
| 60 | + |
| 61 | + private func activateConstraints() { |
| 62 | + sharingScreenLabel.snp.makeConstraints { make in |
| 63 | + make.centerX.equalToSuperview() |
| 64 | + make.centerY.equalToSuperview().offset(-40) |
| 65 | + } |
| 66 | + stopScreenButton.snp.makeConstraints { make in |
| 67 | + make.centerX.equalToSuperview() |
| 68 | + make.height.equalTo(50) |
| 69 | + make.width.equalTo(100) |
| 70 | + make.centerY.equalToSuperview().offset(40) |
| 71 | + } |
| 72 | + screenRecordingLabel.snp.makeConstraints { make in |
| 73 | + make.top.equalTo(sharingScreenLabel) |
| 74 | + make.right.equalTo(safeAreaLayoutGuide.snp.right) |
| 75 | + make.width.equalTo(30) |
| 76 | + make.height.equalTo(150) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private func bindInteraction() { |
| 81 | + stopScreenButton.addTarget(self, action: #selector(stopScreenCaptureAction(sender:)), for: .touchUpInside) |
| 82 | + } |
| 83 | + |
| 84 | + @objc func stopScreenCaptureAction(sender: UIButton) { |
| 85 | + EngineManager.shared.roomEngine.stopScreenCapture() |
| 86 | + } |
| 87 | + |
| 88 | + class func show() { |
| 89 | + let frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height) |
| 90 | + let currentWindow = RoomRouter.getCurrentWindow() |
| 91 | + currentWindow?.addSubview(ScreenCaptureMaskView(frame: frame)) |
| 92 | + } |
| 93 | + |
| 94 | + class func dismiss() { |
| 95 | + guard let currentWindow = RoomRouter.getCurrentWindow() else { return } |
| 96 | + for view in currentWindow.subviews where view is ScreenCaptureMaskView { |
| 97 | + view.removeFromSuperview() |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +private extension String { |
| 103 | + static var sharingScreenText: String { |
| 104 | + localized("TUIRoom.sharing.screen") |
| 105 | + } |
| 106 | + static var stopShareScreen: String { |
| 107 | + localized("TUIRoom.stop.share.screen") |
| 108 | + } |
| 109 | + static var screenRecordingText: String { |
| 110 | + localized("TUIRoom.screen.recording") |
| 111 | + } |
| 112 | +} |
0 commit comments