PublishRecordMusicView.swift 11 KB

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