PublishRecordMusicView.swift 9.7 KB

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