123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- //
- // PublishRecordMusicView.swift
- // RainbowPlanet
- //
- // Created by Christopher on 2019/7/19.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- // 录制视频--选择音乐のView
- import UIKit
- import FWPopupView
- import RxSwift
- import SwiftyMediator
- import JXSegmentedView
- class PublishRecordMusicView: FWPopupView {
-
- let disposeBag = DisposeBag()
-
- typealias RecommendClosure = () -> Void
- var recommendClosure : RecommendClosure?
-
- var curMusicUrl: String?
-
- override init(frame: CGRect) {
- super.init(frame: frame)
- setupViews()
- setupLayouts()
-
- // 获取分类列表
- self.communityGetMusicCategoryApi()
- }
-
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- var categoryTitleArr = Array<String>()
-
- var categoryListMdlArr : Array<MusicCategoryItemModel>? {
- didSet {
- guard categoryListMdlArr?.isEmpty ?? true else {
- categoryTitleArr.removeAll()
- categoryTitleArr.append("推荐")
- for categoryMdl in categoryListMdlArr! {
- categoryTitleArr.append(categoryMdl.name!)
- }
- setupSegmentedView()
- return
- }
- setupSegmentedView()
- return
- }
- }
-
- func setupViews() {
- frame = CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight - kSafeTabBarHeight - kSafeStatusBarHeight - 22)
-
- // 添加毛玻璃效果,需使用frame设置位置
- let blurEffect = UIBlurEffect(style: .dark)
- let blurEffectView = UIVisualEffectView(effect: blurEffect)
- blurEffectView.frame = CGRect(x: CGFloat(0), y: 0, width: kScreenWidth, height: kScreenHeight - kSafeTabBarHeight - kSafeStatusBarHeight - 22)
- addSubview(blurEffectView)
-
- self.backgroundColor = UIColor.clear
-
- configRectCorner(corner: [.topLeft,.topRight], radii: CGSize(width: 8, height: 8))
-
- addSubview(cancelButton)
-
- addSubview(recommendLabel)
- addSubview(recommendButton)
- }
-
- func setupLayouts() {
-
- cancelButton.snp.makeConstraints { (make) in
- make.top.equalTo(2)
- make.left.equalTo(3)
- make.size.equalTo(44)
- }
-
- recommendLabel.snp.makeConstraints { (make) in
- make.centerX.equalTo(kScreenWidth*0.5-20)
- make.bottom.equalTo(-(kSafeTabBarHeight+20))
- make.height.equalTo(20)
- }
- recommendButton.snp.makeConstraints { (make) in
- make.left.equalTo(recommendLabel.snp_right)
- make.height.equalTo(30)
- make.centerY.equalTo(recommendLabel.snp_centerY)
- make.width.equalTo(60)
- }
-
- }
-
- func setupSegmentedView() {
- addSubview(segmentedView)
- addSubview(listContainerView)
- }
-
- // MARK: - 视图创建
- lazy var cancelButton: UIButton = {
- let cancelButton = UIButton(type: UIButton.ButtonType.custom)
- cancelButton.setImage(kImage(name: "video_btn_close_white"), for: .normal)
- return cancelButton
- }()
-
- lazy var recommendLabel: UILabel = {
- let recommendLabel = UILabel()
- recommendLabel.text = "没有找到想要的音乐?"
- recommendLabel.textColor = k999999Color
- recommendLabel.font = kRegularFont14
- return recommendLabel
- }()
-
- lazy var recommendButton: UIButton = {
- let recommendButton = UIButton(type: UIButton.ButtonType.custom)
- recommendButton.setTitle("点击推荐", for: UIControl.State.normal)
- recommendButton.setTitleColor(kffffffColor, for: UIControl.State.normal)
- recommendButton.titleLabel?.font = kMediumFont14
- return recommendButton
- }()
-
- //MARK: -
- //1.初始化JXSegmentedView
- lazy var segmentedView: JXSegmentedView = {
- let segmentedView = JXSegmentedView(frame: CGRect(x: 0, y: 48, width: kScreenWidth, height: 44))
- segmentedView.delegate = self
- segmentedView.dataSource = segmentedDataSource
- segmentedView.indicators = [indicator]
- segmentedView.contentScrollView = listContainerView.scrollView
- segmentedView.defaultSelectedIndex = 0
-
- let lineWidth: CGFloat = 0.5
- let lineLayer = CALayer()
- lineLayer.backgroundColor = k999999Color.cgColor
- lineLayer.frame = CGRect(x: 0, y: segmentedView.bounds.height - lineWidth, width: segmentedView.bounds.width, height: lineWidth)
- segmentedView.layer.addSublayer(lineLayer)
- return segmentedView
- }()
-
- //2.初始化dataSource
- lazy var segmentedDataSource: JXSegmentedTitleDataSource = {
- let segmentedDataSource = JXSegmentedTitleDataSource()
- segmentedDataSource.titles = categoryTitleArr
- segmentedDataSource.isTitleColorGradientEnabled = true
- segmentedDataSource.isItemSpacingAverageEnabled = true
- segmentedDataSource.isTitleZoomEnabled = true
- segmentedDataSource.titleNormalColor = kbbbbbbColor
- segmentedDataSource.titleSelectedColor = kffffffColor
- segmentedDataSource.titleNormalFont = kRegularFont14!
- segmentedDataSource.titleSelectedFont = kMediumFont14
-
- //reloadData(selectedIndex:)方法一定要调用,方法内部会刷新数据源数组
- segmentedDataSource.reloadData(selectedIndex: 0)
- return segmentedDataSource
- }()
-
- //3.初始化指示器indicator
- lazy var indicator: JXSegmentedIndicatorLineView = {
- let indicator = JXSegmentedIndicatorLineView()
- indicator.indicatorColor = kffffffColor
- indicator.indicatorHeight = 3
- indicator.indicatorWidth = 20
- indicator.cornerRadius = 1.5
- indicator.masksToBounds = true
- return indicator
- }()
-
- //4.初始化JXSegmentedListContainerView
- private lazy var listContainerView: JXSegmentedListContainerView = {
- let listContainerView = JXSegmentedListContainerView(dataSource: self)
- listContainerView.didAppearPercent = 0.01
- listContainerView.defaultSelectedIndex = 0
- listContainerView.frame = CGRect(x: 0, y: 48+44, width: kScreenWidth, height: kScreenHeight - kSafeTabBarHeight - kSafeStatusBarHeight - 55 - 114)
- return listContainerView
- }()
-
- /// 初始化View
- @objc class func publishRecordMusicView(attachedView:UIView, curMusicUrl:String) -> PublishRecordMusicView {
- let view = PublishRecordMusicView()
- view.attachedView = attachedView
- view.curMusicUrl = curMusicUrl
- let vProperty = FWPopupViewProperty()
- vProperty.popupCustomAlignment = .bottomCenter
- vProperty.popupViewEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
- vProperty.popupAnimationType = .frame
- vProperty.maskViewColor = UIColor(white: 0, alpha: 0.2)
- // 用户点击外部遮罩层页面是否可以消失
- vProperty.touchWildToHide = "0"
- view.vProperty = vProperty
- view.show()
-
- view.cancelButton.rx.tap.subscribe(onNext: { (data) in
- view.hide()
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "RecordCloseMusicChooseViewNoti"), object: nil)
-
- MusicPlayManager.shared().destroyPlayer()
- MusicPlayManager.shared().curPlayingId = -1
-
- }).disposed(by: view.disposeBag)
-
- view.recommendButton.rx.tap.subscribe(onNext: { (data) in
- Mediator.push(PublishRouterModuleType.pushMucisChooseView)
-
- }).disposed(by: view.disposeBag)
-
- return view
- }
-
- }
- // MARK: - JXSegmentedViewDelegate
- extension PublishRecordMusicView : JXSegmentedViewDelegate {
- //点击选中或者滚动选中都会调用该方法。适用于只关心选中事件,而不关心具体是点击还是滚动选中的情况。
- func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
- //传递didClickSelectedItemAt事件给listContainerView,必须调用!!!
- listContainerView.didClickSelectedItem(at: index)
- }
-
- // 点击选中的情况才会调用该方法
- func segmentedView(_ segmentedView: JXSegmentedView, didClickSelectedItemAt index: Int) {
- listContainerView.didClickSelectedItem(at: index)
- }
-
- // 滚动选中的情况才会调用该方法
- func segmentedView(_ segmentedView: JXSegmentedView, didScrollSelectedItemAt index: Int) {
- }
-
- // 正在滚动中的回调
- func segmentedView(_ segmentedView: JXSegmentedView, scrollingFrom leftIndex: Int, to rightIndex: Int, percent: CGFloat) {
- //传递scrolling事件给listContainerView,必须调用!!!
- listContainerView.segmentedViewScrolling(from: leftIndex, to: rightIndex, percent: percent, selectedIndex: segmentedView.selectedIndex)
- }
-
- /// 是否允许点击选中目标index的item
- func segmentedView(_ segmentedView: JXSegmentedView, canClickItemAt index: Int) -> Bool {
- return true
- }
- }
- // MARK: - JXSegmentedListContainerViewDataSource
- extension PublishRecordMusicView :JXSegmentedListContainerViewDataSource {
-
- func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
- if let titleDataSource = segmentedView.dataSource as? JXSegmentedBaseDataSource {
- return titleDataSource.dataSource.count
- }
- return 0
- }
- func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
- let listVc = PublishMusicListController()
- listVc.curMusicUrl = self.curMusicUrl
- if index == 0 {
- listVc.isReccomendList = true
- } else {
- listVc.categoryItemMdl = categoryListMdlArr![index-1]
- }
- listVc.playMusicClosure = {
- [weak self] (musicUrl) in
- if musicUrl == "" {
- // 点击了不选择音乐
- self?.chooseMusicAction(musicUrl)
- } else {
- MusicPlayManager.shared().initPlay()
- MusicPlayManager.shared().audioUrl = musicUrl
- MusicPlayManager.shared().playAudio()
- }
-
- }
- listVc.chooseMusicClosure = {
- [weak self] (musicUrl) in
- self?.chooseMusicAction(musicUrl)
- }
- return listVc
- }
-
- func chooseMusicAction(_ musicUrl: String) {
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "RecordDownloadMusicAndCombineNoti"), object: musicUrl)
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "RecordCloseMusicChooseViewNoti"), object: nil)
-
- MusicPlayManager.shared().destroyPlayer()
- MusicPlayManager.shared().curPlayingId = -1
- self.hide()
- }
- }
- // MARK: - 网络请求
- extension PublishRecordMusicView {
- /// 获取音乐分类
- func communityGetMusicCategoryApi() {
- SwiftMoyaNetWorkServiceCommunity.shared().communityGetMusicCategoryApi() {
- [weak self] (musicCategoryListModel) -> (Void) in
- let musicCategoryListModel = musicCategoryListModel as? CommunityMusicCategoryListModel
- self?.categoryListMdlArr = musicCategoryListModel?.data
- }
- }
- }
|