PublishMusicChooseView.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. //
  2. // PublishMusicChooseView.swift
  3. // RainbowPlanet
  4. //
  5. // Created by Christopher on 2019/7/16.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. // 编辑视频--选择音乐のView
  8. import UIKit
  9. import RxSwift
  10. import SwiftyMediator
  11. import JXSegmentedView
  12. class PublishMusicChooseView: BaseView {
  13. @objc public var attachedView: UIView?
  14. @objc public var originVolumn: CGFloat = 0
  15. @objc public var backVolumn: CGFloat = 0
  16. typealias RecommendClosure = () -> Void
  17. var recommendClosure : RecommendClosure?
  18. @objc public var curMusicUrl: String? {
  19. didSet {
  20. // 获取分类列表
  21. self.communityGetMusicCategoryApi()
  22. }
  23. }
  24. override init(frame: CGRect) {
  25. super.init(frame: frame)
  26. }
  27. required init?(coder aDecoder: NSCoder) {
  28. fatalError("init(coder:) has not been implemented")
  29. }
  30. var categoryTitleArr = Array<String>()
  31. var categoryListMdlArr : Array<MusicCategoryItemModel>? {
  32. didSet {
  33. guard categoryListMdlArr?.isEmpty ?? true else {
  34. for categoryMdl in categoryListMdlArr! {
  35. categoryTitleArr.append(categoryMdl.name!)
  36. }
  37. setupSegmentedView()
  38. return
  39. }
  40. setupSegmentedView()
  41. return
  42. }
  43. }
  44. override func setupViews() {
  45. self.backgroundColor = UIColor.clear
  46. configRectCorner(corner: [.topLeft,.topRight], radii: CGSize(width: 8, height: 8))
  47. addSubview(cancelButton)
  48. addSubview(noSelectedMusicButton)
  49. addSubview(recommendLabel)
  50. addSubview(recommendButton)
  51. addSubview(sepLineView)
  52. addSubview(musicButton)
  53. addSubview(volumnButton)
  54. addSubview(v_lineLabel)
  55. }
  56. override func setupLayouts() {
  57. cancelButton.snp.makeConstraints { (make) in
  58. make.top.equalTo(2)
  59. make.left.equalTo(3)
  60. make.size.equalTo(44)
  61. }
  62. noSelectedMusicButton.snp.makeConstraints { (make) in
  63. make.centerY.equalTo(cancelButton)
  64. make.right.equalTo(-14)
  65. }
  66. // 底部分割线
  67. sepLineView.snp.makeConstraints { (make) in
  68. make.left.right.equalToSuperview()
  69. make.bottom.equalTo(-48-kSafeTabBarHeight)
  70. make.height.equalTo(0.5)
  71. }
  72. recommendLabel.snp.makeConstraints { (make) in
  73. make.centerX.equalTo(kScreenWidth*0.5-20)
  74. make.bottom.equalTo(sepLineView.snp.top).offset(-20)
  75. make.height.equalTo(20)
  76. }
  77. recommendButton.snp.makeConstraints { (make) in
  78. make.left.equalTo(recommendLabel.snp.right)
  79. make.height.equalTo(30)
  80. make.centerY.equalTo(recommendLabel.snp.centerY)
  81. make.width.equalTo(60)
  82. }
  83. musicButton.snp.makeConstraints { (make) in
  84. make.centerX.equalTo(kScreenWidth*0.25)
  85. make.bottom.equalTo(-17-kSafeTabBarHeight)
  86. make.width.equalTo(40)
  87. make.height.equalTo(21)
  88. }
  89. volumnButton.snp.makeConstraints { (make) in
  90. make.centerX.equalTo(kScreenWidth*0.75)
  91. make.bottom.equalTo(-17-kSafeTabBarHeight)
  92. make.width.equalTo(40)
  93. make.height.equalTo(21)
  94. }
  95. v_lineLabel.snp.makeConstraints { (make) in
  96. make.centerX.equalTo(musicButton.snp.centerX)
  97. make.width.equalTo(20)
  98. make.height.equalTo(3)
  99. make.bottom.equalTo(-11-kSafeTabBarHeight)
  100. }
  101. }
  102. func setupSegmentedView() {
  103. addSubview(segmentedView)
  104. addSubview(listContainerView)
  105. listContainerView.snp.makeConstraints { (make) in
  106. make.top.equalTo(48+44)
  107. make.left.right.equalToSuperview()
  108. make.bottom.equalTo(recommendButton.snp.top).offset(-20)
  109. }
  110. // listContainerView.frame = CGRect(x: 0, y: 48+44, width: kScreenWidth, height: kScreenHeight - kSafeTabBarHeight - kSafeStatusBarHeight - 103 - 114)
  111. }
  112. // MARK: - 视图创建
  113. lazy var cancelButton: UIButton = {
  114. let cancelButton = UIButton(type: UIButton.ButtonType.custom)
  115. cancelButton.setImage(kImage(name: "video_btn_close_white"), for: .normal)
  116. cancelButton.rx.tap.subscribe(onNext: { [weak self] in
  117. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "EditCloseMusicChooseViewNoti"), object: "1")
  118. MusicPlayManager.shared().destroyPlayer()
  119. MusicPlayManager.shared().curPlayingId = -1
  120. }).disposed(by: disposeBag)
  121. return cancelButton
  122. }()
  123. lazy var noSelectedMusicButton: UIButton = {
  124. let noSelectedMusicButton = UIButton()
  125. noSelectedMusicButton.setTitle("不使用音乐", for: UIControl.State.normal)
  126. noSelectedMusicButton.setTitleColor(UIColor.white, for: UIControl.State.normal)
  127. noSelectedMusicButton.titleLabel?.font = kRegularFont15
  128. noSelectedMusicButton.rx.tap.subscribe(onNext: { [weak self] in
  129. self?.chooseMusicAction("")
  130. }).disposed(by: disposeBag)
  131. return noSelectedMusicButton
  132. }()
  133. lazy var recommendLabel: UILabel = {
  134. let recommendLabel = UILabel()
  135. recommendLabel.text = "没有找到想要的音乐?"
  136. recommendLabel.textColor = k999999Color
  137. recommendLabel.font = kRegularFont14
  138. return recommendLabel
  139. }()
  140. lazy var recommendButton: UIButton = {
  141. let recommendButton = UIButton(type: UIButton.ButtonType.custom)
  142. recommendButton.setTitle("点击推荐", for: UIControl.State.normal)
  143. recommendButton.setTitleColor(kffffffColor, for: UIControl.State.normal)
  144. recommendButton.titleLabel?.font = kMediumFont14
  145. recommendButton.rx.tap.subscribe(onNext: { [weak self] in
  146. Mediator.push(PublishRouterModuleType.pushMucisChooseView)
  147. }).disposed(by: disposeBag)
  148. return recommendButton
  149. }()
  150. lazy var sepLineView: UIView = {
  151. let sepLineView = UIView()
  152. sepLineView.backgroundColor = k999999Color
  153. return sepLineView
  154. }()
  155. lazy var musicButton: UIButton = {
  156. let musicButton = UIButton(type: UIButton.ButtonType.custom)
  157. musicButton.setTitle("配乐", for: UIControl.State.normal)
  158. musicButton.setTitleColor(kffffffColor, for: UIControl.State.normal)
  159. musicButton.titleLabel?.font = kMediumFont17
  160. musicButton.rx.tap.subscribe(onNext: { [weak self] in
  161. }).disposed(by: disposeBag)
  162. return musicButton
  163. }()
  164. lazy var volumnButton: UIButton = {
  165. let volumnButton = UIButton(type: UIButton.ButtonType.custom)
  166. volumnButton.setTitle("音量", for: UIControl.State.normal)
  167. volumnButton.setTitleColor(k999999Color, for: UIControl.State.normal)
  168. volumnButton.titleLabel?.font = kRegularFont16
  169. volumnButton.rx.tap.subscribe(onNext: { [weak self] in
  170. _ = PublishMusicVolumnView.publishMusicVolumnView(attachedView: self?.attachedView ?? UIView(), oriVolumn: self?.originVolumn ?? 0, backVolumn: self?.backVolumn ?? 0)
  171. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "EditCloseMusicChooseViewNoti"), object: "0")
  172. }).disposed(by: disposeBag)
  173. return volumnButton
  174. }()
  175. lazy var v_lineLabel: UILabel = {
  176. let v_lineLabel = UILabel()
  177. v_lineLabel.backgroundColor = kffffffColor
  178. v_lineLabel.cornerRadius = 1
  179. return v_lineLabel
  180. }()
  181. //MARK: -
  182. //1.初始化JXSegmentedView
  183. lazy var segmentedView: JXSegmentedView = {
  184. let segmentedView = JXSegmentedView(frame: CGRect(x: 0, y: 48, width: kScreenWidth, height: 44))
  185. segmentedView.delegate = self
  186. segmentedView.dataSource = segmentedDataSource
  187. segmentedView.indicators = [indicator]
  188. segmentedView.contentScrollView = listContainerView.scrollView
  189. segmentedView.defaultSelectedIndex = 0
  190. let lineWidth: CGFloat = 0.5
  191. let lineLayer = CALayer()
  192. lineLayer.backgroundColor = k999999Color.cgColor
  193. lineLayer.frame = CGRect(x: 0, y: segmentedView.bounds.height - lineWidth, width: segmentedView.bounds.width, height: lineWidth)
  194. segmentedView.layer.addSublayer(lineLayer)
  195. return segmentedView
  196. }()
  197. //2.初始化dataSource
  198. lazy var segmentedDataSource: JXSegmentedTitleDataSource = {
  199. let segmentedDataSource = JXSegmentedTitleDataSource()
  200. segmentedDataSource.titles = categoryTitleArr
  201. segmentedDataSource.isTitleColorGradientEnabled = true
  202. segmentedDataSource.isItemSpacingAverageEnabled = true
  203. segmentedDataSource.isTitleZoomEnabled = true
  204. segmentedDataSource.titleNormalColor = kbbbbbbColor
  205. segmentedDataSource.titleSelectedColor = kffffffColor
  206. segmentedDataSource.titleNormalFont = kRegularFont14!
  207. segmentedDataSource.titleSelectedFont = kMediumFont14
  208. //reloadData(selectedIndex:)方法一定要调用,方法内部会刷新数据源数组
  209. segmentedDataSource.reloadData(selectedIndex: 0)
  210. return segmentedDataSource
  211. }()
  212. //3.初始化指示器indicator
  213. lazy var indicator: JXSegmentedIndicatorLineView = {
  214. let indicator = JXSegmentedIndicatorLineView()
  215. indicator.indicatorColor = kffffffColor
  216. indicator.indicatorHeight = 3
  217. indicator.indicatorWidth = 20
  218. indicator.cornerRadius = 1.5
  219. indicator.masksToBounds = true
  220. return indicator
  221. }()
  222. //4.初始化JXSegmentedListContainerView
  223. private lazy var listContainerView: JXSegmentedListContainerView = {
  224. let listContainerView = JXSegmentedListContainerView(dataSource: self)
  225. listContainerView.didAppearPercent = 0.01
  226. listContainerView.defaultSelectedIndex = 0
  227. return listContainerView
  228. }()
  229. }
  230. // MARK: - JXSegmentedViewDelegate
  231. extension PublishMusicChooseView : JXSegmentedViewDelegate {
  232. //点击选中或者滚动选中都会调用该方法。适用于只关心选中事件,而不关心具体是点击还是滚动选中的情况。
  233. func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
  234. //传递didClickSelectedItemAt事件给listContainerView,必须调用!!!
  235. listContainerView.didClickSelectedItem(at: index)
  236. }
  237. // 点击选中的情况才会调用该方法
  238. func segmentedView(_ segmentedView: JXSegmentedView, didClickSelectedItemAt index: Int) {
  239. listContainerView.didClickSelectedItem(at: index)
  240. }
  241. // 滚动选中的情况才会调用该方法
  242. func segmentedView(_ segmentedView: JXSegmentedView, didScrollSelectedItemAt index: Int) {
  243. }
  244. // 正在滚动中的回调
  245. func segmentedView(_ segmentedView: JXSegmentedView, scrollingFrom leftIndex: Int, to rightIndex: Int, percent: CGFloat) {
  246. //传递scrolling事件给listContainerView,必须调用!!!
  247. listContainerView.segmentedViewScrolling(from: leftIndex, to: rightIndex, percent: percent, selectedIndex: segmentedView.selectedIndex)
  248. }
  249. /// 是否允许点击选中目标index的item
  250. func segmentedView(_ segmentedView: JXSegmentedView, canClickItemAt index: Int) -> Bool {
  251. return true
  252. }
  253. }
  254. // MARK: - JXSegmentedListContainerViewDataSource
  255. extension PublishMusicChooseView :JXSegmentedListContainerViewDataSource {
  256. func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
  257. if let titleDataSource = segmentedView.dataSource as? JXSegmentedBaseDataSource {
  258. return titleDataSource.dataSource.count
  259. }
  260. return 0
  261. }
  262. func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
  263. let listVc = PublishMusicListController()
  264. listVc.curMusicUrl = self.curMusicUrl
  265. listVc.categoryItemMdl = categoryListMdlArr![index]
  266. listVc.playMusicClosure = {
  267. [weak self] (musicUrl) in
  268. if musicUrl == "" {
  269. // 点击了不选择音乐
  270. self?.chooseMusicAction(musicUrl)
  271. } else {
  272. MusicPlayManager.shared().initPlay()
  273. MusicPlayManager.shared().audioUrl = musicUrl
  274. MusicPlayManager.shared().playAudio()
  275. }
  276. }
  277. listVc.chooseMusicClosure = {
  278. [weak self] (musicUrl) in
  279. self?.chooseMusicAction(musicUrl)
  280. }
  281. return listVc
  282. }
  283. func chooseMusicAction(_ musicUrl: String) {
  284. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "DownloadMusicAndCombineNoti"), object: musicUrl)
  285. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "EditCloseMusicChooseViewNoti"), object: "1")
  286. MusicPlayManager.shared().destroyPlayer()
  287. MusicPlayManager.shared().curPlayingId = -1
  288. }
  289. }
  290. // MARK: - 网络请求
  291. extension PublishMusicChooseView {
  292. /// 获取音乐分类
  293. func communityGetMusicCategoryApi() {
  294. SwiftMoyaNetWorkServiceCommunity.shared().communityGetMusicCategoryApi() {
  295. [weak self] (musicCategoryListModel) -> (Void) in
  296. let musicCategoryListModel = musicCategoryListModel as? CommunityMusicCategoryListModel
  297. self?.categoryListMdlArr = musicCategoryListModel?.data
  298. }
  299. }
  300. }