123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- //
- // PublishMusicChooseView.swift
- // RainbowPlanet
- //
- // Created by Christopher on 2019/7/16.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- // 编辑视频--选择音乐のView
- import UIKit
- import RxSwift
- import SwiftyMediator
- import JXSegmentedView
- class PublishMusicChooseView: BaseView {
-
- @objc public var attachedView: UIView?
- @objc public var originVolumn: CGFloat = 0
- @objc public var backVolumn: CGFloat = 0
-
- typealias RecommendClosure = () -> Void
- var recommendClosure : RecommendClosure?
-
- @objc public var curMusicUrl: String? {
- didSet {
- // 获取分类列表
- self.communityGetMusicCategoryApi()
- }
- }
-
- override init(frame: CGRect) {
- super.init(frame: frame)
-
- }
-
- 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 {
- for categoryMdl in categoryListMdlArr! {
- categoryTitleArr.append(categoryMdl.name!)
- }
- setupSegmentedView()
- return
- }
- setupSegmentedView()
- return
- }
- }
-
- override func setupViews() {
- self.backgroundColor = UIColor.clear
-
- configRectCorner(corner: [.topLeft,.topRight], radii: CGSize(width: 8, height: 8))
-
- addSubview(cancelButton)
- addSubview(noSelectedMusicButton)
-
-
- addSubview(recommendLabel)
- addSubview(recommendButton)
-
- addSubview(sepLineView)
- addSubview(musicButton)
- addSubview(volumnButton)
- addSubview(v_lineLabel)
- }
-
- override func setupLayouts() {
-
- cancelButton.snp.makeConstraints { (make) in
- make.top.equalTo(2)
- make.left.equalTo(3)
- make.size.equalTo(44)
- }
-
- noSelectedMusicButton.snp.makeConstraints { (make) in
- make.centerY.equalTo(cancelButton)
- make.right.equalTo(-14)
- }
-
- // 底部分割线
- sepLineView.snp.makeConstraints { (make) in
- make.left.right.equalToSuperview()
- make.bottom.equalTo(-48-kSafeTabBarHeight)
- make.height.equalTo(0.5)
- }
-
- recommendLabel.snp.makeConstraints { (make) in
- make.centerX.equalTo(kScreenWidth*0.5-20)
- make.bottom.equalTo(sepLineView.snp.top).offset(-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)
- }
-
- musicButton.snp.makeConstraints { (make) in
- make.centerX.equalTo(kScreenWidth*0.25)
- make.bottom.equalTo(-17-kSafeTabBarHeight)
- make.width.equalTo(40)
- make.height.equalTo(21)
- }
- volumnButton.snp.makeConstraints { (make) in
- make.centerX.equalTo(kScreenWidth*0.75)
- make.bottom.equalTo(-17-kSafeTabBarHeight)
- make.width.equalTo(40)
- make.height.equalTo(21)
- }
- v_lineLabel.snp.makeConstraints { (make) in
- make.centerX.equalTo(musicButton.snp.centerX)
- make.width.equalTo(20)
- make.height.equalTo(3)
- make.bottom.equalTo(-11-kSafeTabBarHeight)
- }
-
- }
-
- func setupSegmentedView() {
- addSubview(segmentedView)
- addSubview(listContainerView)
- listContainerView.snp.makeConstraints { (make) in
- make.top.equalTo(48+44)
- make.left.right.equalToSuperview()
- make.bottom.equalTo(recommendButton.snp.top).offset(-20)
- }
- // listContainerView.frame = CGRect(x: 0, y: 48+44, width: kScreenWidth, height: kScreenHeight - kSafeTabBarHeight - kSafeStatusBarHeight - 103 - 114)
- }
-
- // MARK: - 视图创建
- lazy var cancelButton: UIButton = {
- let cancelButton = UIButton(type: UIButton.ButtonType.custom)
- cancelButton.setImage(kImage(name: "video_btn_close_white"), for: .normal)
- cancelButton.rx.tap.subscribe(onNext: { [weak self] in
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "EditCloseMusicChooseViewNoti"), object: "1")
-
- MusicPlayManager.shared().destroyPlayer()
- MusicPlayManager.shared().curPlayingId = -1
- }).disposed(by: disposeBag)
- return cancelButton
- }()
-
- lazy var noSelectedMusicButton: UIButton = {
- let noSelectedMusicButton = UIButton()
- noSelectedMusicButton.setTitle("不使用音乐", for: UIControl.State.normal)
- noSelectedMusicButton.setTitleColor(UIColor.white, for: UIControl.State.normal)
- noSelectedMusicButton.titleLabel?.font = kRegularFont15
- noSelectedMusicButton.rx.tap.subscribe(onNext: { [weak self] in
- self?.chooseMusicAction("")
- }).disposed(by: disposeBag)
- return noSelectedMusicButton
- }()
-
- 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
- recommendButton.rx.tap.subscribe(onNext: { [weak self] in
- Mediator.push(PublishRouterModuleType.pushMucisChooseView)
- }).disposed(by: disposeBag)
- return recommendButton
- }()
-
- lazy var sepLineView: UIView = {
- let sepLineView = UIView()
- sepLineView.backgroundColor = k999999Color
- return sepLineView
- }()
-
- lazy var musicButton: UIButton = {
- let musicButton = UIButton(type: UIButton.ButtonType.custom)
- musicButton.setTitle("配乐", for: UIControl.State.normal)
- musicButton.setTitleColor(kffffffColor, for: UIControl.State.normal)
- musicButton.titleLabel?.font = kMediumFont17
- musicButton.rx.tap.subscribe(onNext: { [weak self] in
-
- }).disposed(by: disposeBag)
- return musicButton
- }()
-
- lazy var volumnButton: UIButton = {
- let volumnButton = UIButton(type: UIButton.ButtonType.custom)
- volumnButton.setTitle("音量", for: UIControl.State.normal)
- volumnButton.setTitleColor(k999999Color, for: UIControl.State.normal)
- volumnButton.titleLabel?.font = kRegularFont16
- volumnButton.rx.tap.subscribe(onNext: { [weak self] in
- _ = PublishMusicVolumnView.publishMusicVolumnView(attachedView: self?.attachedView ?? UIView(), oriVolumn: self?.originVolumn ?? 0, backVolumn: self?.backVolumn ?? 0)
-
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "EditCloseMusicChooseViewNoti"), object: "0")
- }).disposed(by: disposeBag)
- return volumnButton
- }()
-
- lazy var v_lineLabel: UILabel = {
- let v_lineLabel = UILabel()
- v_lineLabel.backgroundColor = kffffffColor
- v_lineLabel.cornerRadius = 1
- return v_lineLabel
- }()
-
- //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
- return listContainerView
- }()
- }
- // MARK: - JXSegmentedViewDelegate
- extension PublishMusicChooseView : 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 PublishMusicChooseView :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
- listVc.categoryItemMdl = categoryListMdlArr![index]
- 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: "DownloadMusicAndCombineNoti"), object: musicUrl)
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "EditCloseMusicChooseViewNoti"), object: "1")
-
- MusicPlayManager.shared().destroyPlayer()
- MusicPlayManager.shared().curPlayingId = -1
- }
- }
- // MARK: - 网络请求
- extension PublishMusicChooseView {
- /// 获取音乐分类
- func communityGetMusicCategoryApi() {
- SwiftMoyaNetWorkServiceCommunity.shared().communityGetMusicCategoryApi() {
- [weak self] (musicCategoryListModel) -> (Void) in
- let musicCategoryListModel = musicCategoryListModel as? CommunityMusicCategoryListModel
- self?.categoryListMdlArr = musicCategoryListModel?.data
- }
- }
- }
|