BFSubtitleSettingView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. //
  2. // BFSubtitleSettingView.swift
  3. // BFRecordScreenKit
  4. //
  5. // Created by ak on 2021/12/7.
  6. // 功能:设置字幕操作面板
  7. import BFCommonKit
  8. import BFMediaKit
  9. import BFUIKit
  10. import Foundation
  11. import UIKit
  12. typealias SubtitleSettingCallBack = (_ subtitileModel: PQEditSubTitleModel) -> Void
  13. class BFSubtitleSettingView: UIView {
  14. required init?(coder _: NSCoder) {
  15. fatalError("init(coder:) has not been implemented")
  16. }
  17. // 最后一次选择的样式 BTN
  18. var lastSelectStyleBtn: UIButton = UIButton()
  19. // 最后一次选择的位置 BTN
  20. var lastSelectPointBtn: UIButton = UIButton()
  21. // 最后一次选择的字号的 BTN
  22. var lastSelectwordSizeBtn: UIButton = UIButton()
  23. var subtitleSettingCallBack: SubtitleSettingCallBack?
  24. var subtitle: PQEditSubTitleModel = PQEditSubTitleModel()
  25. // 样式配置
  26. var styleConfig: [Int: [String: Any]] = Dictionary()
  27. //操作板背景
  28. let backView = UIButton()
  29. override init(frame: CGRect) {
  30. super.init(frame: frame)
  31. backgroundColor = UIColor.clear
  32. addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(hidden)))
  33. backView.backgroundColor = .black
  34. addSubview(backView)
  35. backView.snp.makeConstraints { make in
  36. make.right.equalTo(self.snp.right)
  37. make.bottom.equalTo(self.snp.bottom)
  38. make.width.equalTo(cScreenWidth)
  39. make.height.equalTo(220)
  40. }
  41. backView.setNeedsUpdateConstraints()
  42. styleConfig = [0: ["fontColor": UIColor.hexColor(hexadecimal: "#FFCF53"), "backgroundColor": UIColor.clear, "strokeColor": UIColor.black, "backgroundAlpha": 0.0],
  43. 1: ["fontColor": UIColor.hexColor(hexadecimal: "#FF9292"), "backgroundColor": UIColor.clear, "strokeColor": UIColor.black, "backgroundAlpha": 0.0],
  44. 2: ["fontColor": UIColor.hexColor(hexadecimal: "#80C2FF"), "backgroundColor": UIColor.clear, "strokeColor": UIColor.black, "backgroundAlpha": 0.0],
  45. 3: ["fontColor": UIColor.hexColor(hexadecimal: "#80E4AB"), "backgroundColor": UIColor.clear, "strokeColor": UIColor.black, "backgroundAlpha": 0.0],
  46. 4: ["fontColor": UIColor.hexColor(hexadecimal: "#FFFFFF"), "backgroundColor": UIColor.clear, "strokeColor": UIColor.black, "backgroundAlpha": 0.0],
  47. 5: ["fontColor": UIColor.hexColor(hexadecimal: "#000000"), "backgroundColor": UIColor.clear, "strokeColor": UIColor.white, "backgroundAlpha": 0.0],
  48. 6: ["fontColor": UIColor.hexColor(hexadecimal: "#FFFFFF"), "backgroundColor":UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.6), "strokeColor": UIColor.clear, "backgroundAlpha": 0.6]]
  49. // 字体样式
  50. for i in 0...6 {
  51. let btn = UIButton(type: .custom)
  52. btn.setImage(imageInRecordScreenKit(by: "wordStyle\(i + 1)"), for: .normal)
  53. btn.setBackgroundImage(UIImage(color: .black), for: .normal)
  54. btn.frame = CGRect(x: 18 + i * (40 + 10), y: 22, width: 40, height: 40)
  55. btn.tag = i
  56. if i == 0 {
  57. btn.isSelected = true
  58. btn.layer.borderColor = UIColor.hexColor(hexadecimal: "#389AFF").cgColor
  59. btn.layer.borderWidth = 1.5
  60. lastSelectStyleBtn = btn
  61. styleSetting(sender: lastSelectStyleBtn)
  62. }
  63. btn.addTarget(self, action: #selector(styleSetting(sender:)), for: .touchUpInside)
  64. backView.addSubview(btn)
  65. btn.addCorner(corner: 20.0)
  66. }
  67. // 位置
  68. let pointTitle = UILabel()
  69. pointTitle.textAlignment = .center
  70. pointTitle.font = UIFont.systemFont(ofSize: 15)
  71. pointTitle.textColor = .white
  72. pointTitle.text = "位置"
  73. pointTitle.alpha = 0.6
  74. backView.addSubview(pointTitle)
  75. pointTitle.snp.makeConstraints { make in
  76. make.width.equalTo(40)
  77. make.height.equalTo(25)
  78. make.left.equalToSuperview().offset(16)
  79. make.top.equalToSuperview().offset(89)
  80. }
  81. for i in 0...2 {
  82. let pointBtn = UIButton(type: .custom)
  83. pointBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#389AFF"), for: .selected)
  84. pointBtn.setTitleColor(.white, for: .normal)
  85. pointBtn.addTarget(self, action: #selector(pointSetting(sender:)), for: .touchUpInside)
  86. pointBtn.backgroundColor = UIColor.hexColor(hexadecimal: "#1A1A1A")
  87. if i == 0 {
  88. pointBtn.isSelected = true
  89. pointBtn.layer.borderColor = UIColor.hexColor(hexadecimal: "#389AFF").cgColor
  90. pointBtn.layer.borderWidth = 1.5
  91. lastSelectPointBtn = pointBtn
  92. pointBtn.setTitle("下", for: .normal)
  93. } else if i == 1 {
  94. pointBtn.setTitle("中", for: .normal)
  95. } else {
  96. pointBtn.setTitle("上", for: .normal)
  97. }
  98. pointBtn.addCorner(corner: 10)
  99. pointBtn.tag = i
  100. backView.addSubview(pointBtn)
  101. pointBtn.snp.makeConstraints { make in
  102. make.width.equalTo(40)
  103. make.height.equalTo(30)
  104. make.centerX.equalTo(pointTitle.snp.centerX).offset(60 + i * Int(40 + 10))
  105. make.centerY.equalTo(pointTitle.snp.centerY)
  106. }
  107. }
  108. // 字号
  109. let wordSizeTitle = UILabel()
  110. wordSizeTitle.textAlignment = .center
  111. wordSizeTitle.font = UIFont.systemFont(ofSize: 15)
  112. wordSizeTitle.textColor = .white
  113. wordSizeTitle.text = "字号"
  114. wordSizeTitle.alpha = 0.6
  115. backView.addSubview(wordSizeTitle)
  116. wordSizeTitle.snp.makeConstraints { make in
  117. make.width.equalTo(40)
  118. make.height.equalTo(25)
  119. make.right.equalToSuperview().offset(-100)
  120. make.top.equalTo(pointTitle.snp.top)
  121. }
  122. for i in 0...1 {
  123. let wordSizeBtn = UIButton(type: .custom)
  124. wordSizeBtn.addTarget(self, action: #selector(wordSizeSetting(sender:)), for: .touchUpInside)
  125. backView.addSubview(wordSizeBtn)
  126. wordSizeBtn.tag = i
  127. wordSizeBtn.setBackgroundImage(imageInRecordScreenKit(by: i == 0 ? "wordSizeSetting-_h" : "wordSizeSetting+_h"), for: .normal)
  128. wordSizeBtn.snp.makeConstraints { make in
  129. make.width.equalTo(24)
  130. make.height.equalTo(24)
  131. make.left.equalTo(wordSizeTitle.snp.right).offset(10 + i * Int(30 + 10))
  132. make.centerY.equalTo(pointTitle.snp.centerY)
  133. }
  134. }
  135. // 就是一个线
  136. let line = UIView()
  137. line.backgroundColor = UIColor.hexColor(hexadecimal: "#121212")
  138. backView.addSubview(line)
  139. line.snp.makeConstraints { make in
  140. make.width.equalTo(340)
  141. make.height.equalTo(2)
  142. make.left.equalToSuperview().offset(18)
  143. make.top.equalTo(wordSizeTitle.snp.bottom).offset(23)
  144. }
  145. // 不显示字幕
  146. let disabelTitle = UILabel()
  147. disabelTitle.textAlignment = .left
  148. disabelTitle.font = UIFont.boldSystemFont(ofSize: 17)
  149. disabelTitle.textColor = .white
  150. disabelTitle.text = "关闭字幕"
  151. backView.addSubview(disabelTitle)
  152. disabelTitle.snp.makeConstraints { make in
  153. make.width.equalTo(100)
  154. make.height.equalTo(24)
  155. make.left.equalToSuperview().offset(18)
  156. make.top.equalTo(line.snp.top).offset(17)
  157. }
  158. // 创建switch
  159. let disabeSwitch = UISwitch()
  160. disabeSwitch.isOn = false
  161. backView.addSubview(disabeSwitch)
  162. disabeSwitch.addTarget(self, action: #selector(switchChange(uiswitch:)), for: .valueChanged)
  163. disabeSwitch.snp.makeConstraints { make in
  164. make.width.equalTo(51)
  165. make.height.equalTo(31)
  166. make.right.equalToSuperview().offset(-18)
  167. make.top.equalTo(line.snp.top).offset(13)
  168. }
  169. }
  170. override func layoutSubviews() {
  171. super.layoutSubviews()
  172. self.backView.addCorner(roundingCorners: [.topLeft, .topRight], corner: 10)
  173. }
  174. @objc func hidden() {
  175. isHidden = true
  176. }
  177. @objc func styleSetting(sender: UIButton) {
  178. lastSelectStyleBtn.isSelected = false
  179. lastSelectStyleBtn.layer.borderColor = UIColor.clear.cgColor
  180. sender.isSelected = true
  181. lastSelectStyleBtn = sender
  182. sender.layer.borderColor = UIColor.hexColor(hexadecimal: "#389AFF").cgColor
  183. sender.layer.borderWidth = 1.5
  184. subtitle.setting.subtitleStyle = sender.tag
  185. let config = styleConfig[sender.tag]
  186. subtitle.setting.fontColor = config?["fontColor"] as! UIColor
  187. subtitle.setting.backgroundColor = config?["backgroundColor"] as! UIColor
  188. subtitle.setting.backgroundAlpha = Float(config?["backgroundAlpha"] as! Double)
  189. subtitle.setting.strokeColor = config?["strokeColor"] as! UIColor
  190. if subtitleSettingCallBack != nil {
  191. subtitleSettingCallBack!(subtitle)
  192. }
  193. }
  194. @objc func pointSetting(sender: UIButton) {
  195. lastSelectPointBtn.isSelected = false
  196. lastSelectPointBtn.layer.borderColor = UIColor.clear.cgColor
  197. sender.isSelected = true
  198. lastSelectPointBtn = sender
  199. sender.layer.borderColor = UIColor.hexColor(hexadecimal: "#389AFF").cgColor
  200. sender.layer.borderWidth = 1.5
  201. subtitle.setting.subtitlePoint = sender.tag
  202. if subtitleSettingCallBack != nil {
  203. subtitleSettingCallBack!(subtitle)
  204. }
  205. }
  206. @objc func wordSizeSetting(sender: UIButton) {
  207. if(sender.tag == 0){
  208. if(subtitle.setting.subtitleSize > 20){
  209. subtitle.setting.subtitleSize -= 10
  210. lastSelectwordSizeBtn.setImage(imageInRecordScreenKit(by: "wordSizeSetting+_h"), for: .normal)
  211. sender.setImage(imageInRecordScreenKit(by: "wordSizeSetting-_h"), for: .normal)
  212. }else{
  213. sender.setImage(imageInRecordScreenKit(by: "wordSizeSetting-_n"), for: .normal)
  214. }
  215. }else if(sender.tag == 1){
  216. if(subtitle.setting.subtitleSize < 120){
  217. subtitle.setting.subtitleSize += 10
  218. sender.setImage(imageInRecordScreenKit(by: "wordSizeSetting+_h"), for: .normal)
  219. lastSelectwordSizeBtn.setImage(imageInRecordScreenKit(by: "wordSizeSetting-_h"), for: .normal)
  220. }else{
  221. sender.setImage(imageInRecordScreenKit(by: "wordSizeSetting+_n"), for: .normal)
  222. }
  223. }
  224. lastSelectwordSizeBtn = sender
  225. if subtitleSettingCallBack != nil {
  226. subtitleSettingCallBack!(subtitle)
  227. }
  228. }
  229. @objc func switchChange(uiswitch: UISwitch) {
  230. print("字幕开关状态\(uiswitch.isOn)")
  231. subtitle.setting.subtitleIsShow = !uiswitch.isOn
  232. if subtitleSettingCallBack != nil {
  233. subtitleSettingCallBack!(subtitle)
  234. }
  235. }
  236. }