| | |
| | | |
| | | private var viewModel = FightAnswerViewModel() |
| | | |
| | | private var listen4Model:Listen4Model! |
| | | private var page:Int! |
| | | var rootViewModel:HomeListenFightViewModel! |
| | | |
| | | private var answerIndex:IndexPath? //答案的Index |
| | | private var answerCount:Int = 0 //回答计数 |
| | | |
| | | private lazy var stackView:UIStackView = { |
| | | let sta = UIStackView() |
| | | sta.spacing = 41 |
| | |
| | | return collection |
| | | }() |
| | | |
| | | required init(page:Int,listen4Model:Listen4Model){ |
| | | super.init(nibName: nil, bundle: nil) |
| | | self.page = page |
| | | self.listen4Model = listen4Model |
| | | } |
| | | |
| | | required init?(coder: NSCoder) { |
| | | fatalError("init(coder:) has not been implemented") |
| | | } |
| | | |
| | | override func viewDidLoad() { |
| | | super.viewDidLoad() |
| | | |
| | | VoicePlayer.share().playEnd { |
| | | print("播放完成") |
| | | } |
| | | } |
| | | |
| | | override func setUI() { |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | private func setAnswerStackView(){ |
| | | |
| | | for subView in stackView.arrangedSubviews{ |
| | | subView.removeFromSuperview() |
| | | guard viewModel.selectIndex.value != nil else{ |
| | | alertError(msg: "请先听题");return |
| | | } |
| | | |
| | | stackView.removeArrangedSubviews() |
| | | |
| | | if !view.subviews.contains(stackView){ |
| | | view.addSubview(stackView) |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | let answerId = listen4Model.data[viewModel.selectIndex.value!.section].answerSubject |
| | | var tempVoiceArray = [String]() |
| | | for v in listen4Model.data[viewModel.selectIndex.value!.section].subjectList{ |
| | | if v.id == answerId{ |
| | | tempVoiceArray.append(v.correct) |
| | | tempVoiceArray.append(v.error.components(separatedBy: ",").first ?? "") |
| | | tempVoiceArray.append(v.error.components(separatedBy: ",").last ?? "") |
| | | } |
| | | } |
| | | |
| | | tempVoiceArray.shuffle() |
| | | |
| | | |
| | | for i in 0...2{ |
| | | let answerView = Lesson_4_AnswerView.jq_loadNibView() |
| | | answerView.btn_choose.tag = 10 + i |
| | | answerView.voiceUrl = tempVoiceArray[i] |
| | | answerView.btn_choose.addTarget(self, action: #selector(answerAction), for: .touchUpInside) |
| | | answerView.alpha = 0 |
| | | answerView.snp.makeConstraints { make in |
| | |
| | | } |
| | | } |
| | | |
| | | var answerModel:Listen1SubModel? |
| | | for (index,v) in (listen4Model.data[viewModel.selectIndex.value!.section].subjectList).enumerated(){ |
| | | if v.id == listen4Model.data[viewModel.selectIndex.value!.section].answerSubject{ |
| | | answerModel = v |
| | | }else{ |
| | | answerIndex = IndexPath(row: index, section: viewModel.selectIndex.value!.section) |
| | | } |
| | | } |
| | | |
| | | var answerType:Fight_lessonType = .none |
| | | answerType = .success |
| | | if tempSubV?.voiceUrl == answerModel?.correct{ |
| | | answerType = .success |
| | | answerCount += 1 |
| | | rootViewModel.correctNum += 1 |
| | | }else{ |
| | | answerType = .fail |
| | | rootViewModel.errorNum += 1 |
| | | } |
| | | VoicePlayer.share().playerEnd() |
| | | |
| | | switch answerType { |
| | | case .success: |
| | |
| | | self.view.addSubview(copyView) |
| | | self.view.layoutIfNeeded() |
| | | |
| | | |
| | | //获取Cell的顶部试图 |
| | | if let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_lesson_4_CCell", for: self.viewModel.selectIndex.value!) as? ListenFight_lesson_4_CCell{ |
| | | if let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_lesson_4_CCell", for: self.answerIndex!) as? ListenFight_lesson_4_CCell{ |
| | | var newRect1 = cell.convert(cell.bounds, to: self.collectionView) |
| | | newRect1.origin.x += 76 |
| | | newRect1.origin.y += 33 |
| | |
| | | UIView.animate(withDuration: 0.4) { |
| | | copyView.frame = CGRect(origin: newRect1.origin, size: CGSize(width: 152, height: 52)) |
| | | } completion: { _ in |
| | | self.setAnswerStackView() |
| | | self.viewModel.selectIndex.accept(nil) |
| | | self.stackView.removeArrangedSubviews() |
| | | self.stackView.layoutIfNeeded() |
| | | |
| | | //回答完成,下一答题 |
| | | if self.answerCount == 2{ |
| | | self.rootViewModel.answerItems[self.page] = self.listen4Model |
| | | VoicePlayer.share().playerEnd() |
| | | NotificationCenter.default.post(name: NextLession_Noti, object: nil);return |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | tempSubV?.img_state.alpha = 1 |
| | | }completion: { _ in |
| | | DispatchQueue.main.asyncAfter(deadline: .now()+3) { |
| | | self.setAnswerStackView() |
| | | // self.setAnswerStackView() |
| | | } |
| | | } |
| | | case .none: |
| | |
| | | |
| | | extension HomeListenFight_lesson_4_VC:UICollectionViewDelegate{ |
| | | func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { |
| | | viewModel.selectIndex.accept(indexPath) |
| | | |
| | | UIView.animate(withDuration: 0.5) { |
| | | self.collectionView.snp.remakeConstraints { make in |
| | | make.left.equalTo(76) |
| | | make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(0) |
| | | make.width.equalTo(JQ_ScreenW - 164 * 2) |
| | | make.bottom.equalToSuperview() |
| | | } |
| | | self.view.layoutIfNeeded() |
| | | } |
| | | self.setAnswerStackView() |
| | | // viewModel.selectIndex.accept(indexPath) |
| | | } |
| | | } |
| | | |
| | |
| | | let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_lesson_4_CCell", for: indexPath) as! ListenFight_lesson_4_CCell |
| | | cell.backgroundColor = .clear |
| | | cell.contentView.backgroundColor = .clear |
| | | cell.indexPath = indexPath |
| | | cell.playAtIndex {[weak self] index in |
| | | guard let weakSelf = self else { return } |
| | | weakSelf.viewModel.selectIndex.accept(index) |
| | | UIView.animate(withDuration: 0.5) { |
| | | weakSelf.collectionView.snp.remakeConstraints { make in |
| | | make.left.equalTo(76) |
| | | make.top.equalTo(weakSelf.view.safeAreaLayoutGuide.snp.top).offset(0) |
| | | make.width.equalTo(JQ_ScreenW - 164 * 2) |
| | | make.bottom.equalToSuperview() |
| | | } |
| | | weakSelf.view.layoutIfNeeded() |
| | | }completion: { _ in |
| | | weakSelf.setAnswerStackView() |
| | | } |
| | | } |
| | | |
| | | if indexPath == viewModel.selectIndex.value{ |
| | | let m = listen4Model.data[indexPath.section].subjectList[indexPath.row] |
| | | if indexPath == answerIndex{ |
| | | UIView.animate(withDuration: 0.4, delay: 0, usingSpringWithDamping: 0.4, initialSpringVelocity: 0.4,options: .layoutSubviews) { |
| | | cell.img_state.alpha = 1 |
| | | cell.img_state.transform = .init(scaleX: 1.0, y: 1.0) |
| | |
| | | cell.img_state.alpha = 0 |
| | | cell.img_state.transform = .init(scaleX: 0.1, y: 0.1) |
| | | } |
| | | //问题 |
| | | if listen4Model.data[indexPath.section].subject == m.id{ |
| | | cell.btn_handle.setImage(UIImage(named: "icon_question"), for: .normal) |
| | | cell.view_handle.isHidden = true |
| | | }else{ |
| | | cell.view_handle.isHidden = false |
| | | } |
| | | cell.setModel(m) |
| | | return cell |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { |
| | | return 2 |
| | | return listen4Model.data[section].subjectList.count |
| | | } |
| | | |
| | | func numberOfSections(in collectionView: UICollectionView) -> Int { |
| | | return 2 |
| | | return listen4Model.data.count |
| | | } |
| | | |
| | | } |