| | |
| | | private var answterCount:Int = 0 //回答计数,用于确定角标 |
| | | var rootViewModel:HomeListenFightViewModel! |
| | | private var voicePlayer = VoicePlayer.share() |
| | | private var playIndex = Set<IndexPath>() //顺序播放 |
| | | private var isPlayingIndex:IndexPath? //正在播放中 |
| | | |
| | | required init(page:Int,listenNewModel:ListenNewModel){ |
| | | super.init(nibName: nil, bundle: nil) |
| | | self.page = page |
| | | self.listenNewModel = listenNewModel |
| | | // self.listen1Model.subjectList.shuffle() |
| | | } |
| | | |
| | | required init?(coder: NSCoder) { |
| | |
| | | override func viewDidDisappear(_ animated: Bool) { |
| | | super.viewDidDisappear(animated) |
| | | voicePlayer.delegate = nil |
| | | VoicePlayer.share().playerInterrupt() |
| | | } |
| | | |
| | | |
| | | |
| | | override func viewDidLoad() { |
| | | super.viewDidLoad() |
| | | navigationItem.titleView = UIView() |
| | | // viewModel.selectIndex.accept(IndexPath(row: 0, section: 0)) |
| | | |
| | | setAnswerStackView() |
| | | playIndex.insert(IndexPath(row: 0, section: 0)) |
| | | // setAnswerStackView() |
| | | } |
| | | |
| | | func restore(){ |
| | |
| | | } |
| | | |
| | | override func setRx() { |
| | | viewModel.selectIndex.subscribe(onNext: {[weak self] index in |
| | | |
| | | guard let index = index else { return } |
| | | //判断选中是否正确逻辑 |
| | | if let cell = self?.collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_lesson_3_CCell", for: index) as? ListenFight_lesson_1_CCell{ |
| | | var answerType:Fight_lessonType = .none |
| | | answerType = .success |
| | | |
| | | switch answerType { |
| | | case .success: |
| | | self?.viewModel.answerType.accept(.success) |
| | | case .fail: |
| | | self?.viewModel.answerType.accept(.fail) |
| | | default:break |
| | | } |
| | | } |
| | | }).disposed(by: disposeBag) |
| | | } |
| | | |
| | | private func setAnswerStackView(){ |
| | |
| | | tempImageArray.append(listenNewModel.subjectList[page][2].img) |
| | | tempImageArray.append(listenNewModel.subjectList[page][4].img) |
| | | tempImageArray.append(listenNewModel.subjectList[page][5].img) |
| | | // tempImageArray.shuffle() |
| | | |
| | | view.addSubview(stackView) |
| | | stackView.snp.makeConstraints { make in |
| | |
| | | make.centerY.equalToSuperview() |
| | | make.height.equalTo(52) |
| | | } |
| | | |
| | | var tempAnswerViews = [Lesson_3_AnswerView]() |
| | | for i in 0...2{ |
| | | let answerView = Lesson_3_AnswerView.jq_loadNibView() |
| | | answerView.alpha = 0 |
| | |
| | | UIView.animate(withDuration: 0.05 + Double(i)) { |
| | | answerView.alpha = 1 |
| | | } |
| | | |
| | | stackView.insertArrangedSubview(answerView, at: 0) |
| | | tempAnswerViews.append(answerView) |
| | | } |
| | | tempAnswerViews.shuffle() |
| | | stackView.addArrangedSubviews(tempAnswerViews) |
| | | } |
| | | |
| | | @objc private func chooseAnswerAction(btn:UIButton){ |
| | | if viewModel.selectIndex.value == nil{alertError(msg: "请先听题");return} |
| | | |
| | | let index = btn.tag - 10 |
| | | if isPlayingIndex != nil { |
| | | alertError(msg: "请先听题");return |
| | | } |
| | | |
| | | if rootViewModel.correctNum == 0 && !playIndex.contains(IndexPath(row: 2, section: 0)){ |
| | | alertError(msg: "请先听题");return |
| | | } |
| | | |
| | | if rootViewModel.correctNum == 1 && !playIndex.contains(IndexPath(row: 1, section: 1)){ |
| | | alertError(msg: "请先听题");return |
| | | } |
| | | |
| | | if rootViewModel.correctNum == 2 && !playIndex.contains(IndexPath(row: 2, section: 1)){ |
| | | alertError(msg: "请先听题");return |
| | | } |
| | | |
| | | var subV:Lesson_3_AnswerView? |
| | | |
| | | for (i,v) in stackView.arrangedSubviews.reversed().enumerated(){ |
| | | if index == i{subV = v as? Lesson_3_AnswerView;break} |
| | | for (_,v) in (stackView.arrangedSubviews as! [Lesson_3_AnswerView]).enumerated(){ |
| | | if v.btn_choose.tag == btn.tag{ |
| | | subV = v;break |
| | | } |
| | | } |
| | | |
| | | var answerType:Fight_lessonType = .none |
| | |
| | | |
| | | if subV?.imageUrl == listenNewModel.subjectList[page][valueIndex].img{ |
| | | answerType = .success |
| | | voicePlayer.playSuccessVoice() |
| | | }else{ |
| | | answerType = .fail |
| | | voicePlayer.playFailVoice() |
| | | } |
| | | |
| | | switch answerType { |
| | |
| | | var ansterIndePath:IndexPath? |
| | | if viewModel.selectIndex.value?.section == 0{ |
| | | ansterIndePath = IndexPath(row: 2, section: 0) |
| | | playIndex.insert(IndexPath(row: 0, section: 1)) //下一个准备播放 |
| | | } |
| | | |
| | | if viewModel.selectIndex.value?.section == 1 && (viewModel.selectIndex.value?.row == 0 || viewModel.selectIndex.value?.row == 1){ |
| | | ansterIndePath = IndexPath(row: 1, section: 1) |
| | | playIndex.insert(IndexPath(row: 2, section: 1)) //下一个准备播放 |
| | | } |
| | | |
| | | if viewModel.selectIndex.value?.section == 1 && viewModel.selectIndex.value?.row == 2{ |
| | |
| | | self.viewModel.selectIndex.accept(nil) |
| | | let v = self.rootViewModel.answerCount.value + 1 |
| | | self.rootViewModel.answerCount.accept(v) |
| | | self.collectionView.reloadData() |
| | | } |
| | | } |
| | | } |
| | |
| | | cell.backgroundColor = .clear |
| | | cell.indexPath = indexPath |
| | | cell.contentView.backgroundColor = .clear |
| | | cell.canClick(playIndex.contains(indexPath)) |
| | | cell.palyVoiceAt {[weak self] index in |
| | | self?.viewModel.selectIndex.accept(index) |
| | | guard let weakSelf = self else { return } |
| | | weakSelf.isPlayingIndex = index |
| | | |
| | | weakSelf.voicePlayer.playerEnd() |
| | | weakSelf.voicePlayer.playerAt(url: weakSelf.listenNewModel.subjectList[weakSelf.page][indexPath.row].correct) |
| | | |
| | | weakSelf.viewModel.selectIndex.accept(index) |
| | | //点击答案,就显示 |
| | | if (index.section == 0 && index.row == 2) || (index.section == 1 && index.row > 0){ |
| | | self?.viewModel.selectIndex.accept(index) |
| | | self?.setAnswerStackView() |
| | | weakSelf.viewModel.selectIndex.accept(index) |
| | | weakSelf.setAnswerStackView() |
| | | } |
| | | |
| | | collectionView.reloadItems(at: [index]) |
| | |
| | | }else{ |
| | | cell.img_cover.image = nil |
| | | } |
| | | cell.setModel(model,isplaying: viewModel.selectIndex.value == indexPath) |
| | | cell.setModel(model,isplaying: isPlayingIndex == indexPath) |
| | | } |
| | | |
| | | if indexPath.section == 1{ |
| | |
| | | }else{ |
| | | cell.img_cover.image = nil |
| | | } |
| | | cell.setModel(model,isplaying: viewModel.selectIndex.value == indexPath) |
| | | cell.setModel(model,isplaying: isPlayingIndex == indexPath) |
| | | } |
| | | return cell |
| | | } |
| | |
| | | |
| | | extension HomeListenFight_lesson_3_VC:VoicePlayerDelegate{ |
| | | func playComplete() { |
| | | collectionView.reloadData() |
| | | isPlayingIndex = nil |
| | | |
| | | var nextRow = (viewModel.selectIndex.value?.row ?? 0) + 1 |
| | | var section = (viewModel.selectIndex.value?.section ?? 0) + 0 |
| | | |
| | | if nextRow >= 3{ |
| | | nextRow = 0;section = 1 |
| | | } |
| | | |
| | | |
| | | if self.answterCount == 3{ |
| | | self.rootViewModel.answerItems[self.page] = self.listenNewModel.subjectList[self.page] |
| | | NotificationCenter.default.post(name: NextLession_Noti, object: nil) |
| | | VoicePlayer.share().playerEnd() |
| | | return |
| | | } |
| | | |
| | | if (viewModel.selectIndex.value?.section == 0 && viewModel.selectIndex.value?.row == 2) || (viewModel.selectIndex.value?.section == 1 && viewModel.selectIndex.value?.row == 1){ |
| | | collectionView.reloadData() |
| | | return |
| | | } |
| | | playIndex.insert(IndexPath(row: nextRow, section: section)) //下一个准备播放 |
| | | collectionView.reloadData() |
| | | } |
| | | |
| | | func playing() { |