| | |
| | | class HomeListenFight_lesson_5_VC: BaseVC { |
| | | |
| | | private var viewModel = FightAnswerViewModel() |
| | | private var listen1Model:Listen1Model! |
| | | private var page:Int! |
| | | private var answterCount:Int = 0 //回答计数,用于确定角标 |
| | | private var playVoiceAt:Int? //播放声音的View |
| | | private var playVoiceRealAt:Int? //播放声音的View -被乱序后,真实Index |
| | | var rootViewModel:HomeListenFightViewModel! |
| | | |
| | | private lazy var collectionView:UICollectionView = { |
| | | let flowLayout = UICollectionViewFlowLayout() |
| | | let w = (JQ_ScreenW - 189 * 2 - 18) / 2.0 |
| | | flowLayout.itemSize = CGSize(width: w, height: w * 0.75) |
| | | flowLayout.itemSize = CGSize(width: w, height: w * 0.70) |
| | | flowLayout.minimumInteritemSpacing = 15 |
| | | flowLayout.minimumLineSpacing = 15 |
| | | flowLayout.scrollDirection = .vertical |
| | |
| | | return sta |
| | | }() |
| | | |
| | | private lazy var label_hint:UILabel = { |
| | | let label = UILabel() |
| | | label.font = .systemFont(ofSize: 18, weight: .medium) |
| | | label.textColor = .black |
| | | label.text = "语音对应内容" |
| | | label.textAlignment = .center |
| | | return label |
| | | }() |
| | | |
| | | required init(page:Int,listen1Model:Listen1Model){ |
| | | super.init(nibName: nil, bundle: nil) |
| | | self.page = page |
| | | self.listen1Model = listen1Model |
| | | } |
| | | |
| | | required init?(coder: NSCoder) { |
| | | fatalError("init(coder:) has not been implemented") |
| | | } |
| | | |
| | | |
| | | override func viewDidLoad() { |
| | | super.viewDidLoad() |
| | | |
| | | collectionView.reloadData() |
| | | } |
| | | |
| | | override func setUI() { |
| | | super.setUI() |
| | | |
| | | collectionView.delegate = self |
| | | collectionView.dataSource = self |
| | | collectionView.showsVerticalScrollIndicator = false |
| | | collectionView.backgroundColor = .clear |
| | | view.addSubview(collectionView) |
| | | collectionView.snp.makeConstraints { make in |
| | | make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(89) |
| | | make.left.equalTo(189) |
| | | make.width.equalTo(JQ_ScreenW - 189 * 2) |
| | | make.bottom.equalToSuperview() |
| | | } |
| | | |
| | | view.addSubview(stackView) |
| | | stackView.snp.makeConstraints { make in |
| | |
| | | make.height.equalTo(52) |
| | | } |
| | | |
| | | view.addSubview(label_hint) |
| | | label_hint.snp.makeConstraints { make in |
| | | make.left.right.equalToSuperview() |
| | | make.top.equalTo(stackView.snp.bottom).offset(6) |
| | | make.height.equalTo(0) |
| | | } |
| | | |
| | | collectionView.delegate = self |
| | | collectionView.dataSource = self |
| | | collectionView.showsVerticalScrollIndicator = false |
| | | collectionView.backgroundColor = .clear |
| | | view.addSubview(collectionView) |
| | | collectionView.snp.makeConstraints { make in |
| | | make.top.equalTo(self.label_hint.snp.bottom).offset(11) |
| | | make.left.equalTo(189) |
| | | make.width.equalTo(JQ_ScreenW - 189 * 2) |
| | | make.bottom.equalToSuperview() |
| | | } |
| | | |
| | | showHintText(false) |
| | | |
| | | setAnswerStackView() |
| | | } |
| | | |
| | | override func setRx() { |
| | | // viewModel.selectIndex.subscribe(onNext: {indexPath in |
| | | // if let index = indexPath{ |
| | | // |
| | | // } |
| | | // }).disposed(by: disposeBag) |
| | | } |
| | | |
| | | private func setAnswerStackView(){ |
| | |
| | | } |
| | | } |
| | | |
| | | for i in 0...3{ |
| | | var tempArray = [StudyHandleView]() |
| | | for (i,value) in listen1Model.subjectList.enumerated(){ |
| | | let answerView = StudyHandleView.jq_loadNibView() |
| | | answerView.listenType = .lesson5 |
| | | answerView.view_choose.isHidden = true |
| | | answerView.tag = 1000+i |
| | | answerView.playAt {[weak self] index in |
| | | print("--->\(index)--\(answerView.frame.origin.x / 248)") |
| | | self?.playVoiceRealAt = Int(answerView.frame.origin.x) / 248 |
| | | self?.playVoiceAt = index - 1000 |
| | | self?.showHintText(true) |
| | | } |
| | | answerView.vioceSoundUrl = value.correct |
| | | answerView.snp.makeConstraints { make in |
| | | make.width.equalTo(159) |
| | | make.height.equalTo(52) |
| | |
| | | UIView.animate(withDuration: 0.05 + Double(i)) { |
| | | answerView.alpha = 1 |
| | | } |
| | | tempArray.append(answerView) |
| | | } |
| | | tempArray.shuffle() |
| | | stackView.addArrangedSubviews(tempArray) |
| | | } |
| | | |
| | | stackView.insertArrangedSubview(answerView, at: 0) |
| | | private func showHintText(_ state:Bool){ |
| | | if let at = playVoiceAt{ |
| | | label_hint.text = listen1Model.subjectList[at].name |
| | | UIView.animate(withDuration: 0.5) { |
| | | if state{ |
| | | self.label_hint.snp.remakeConstraints { make in |
| | | make.left.right.equalToSuperview() |
| | | make.top.equalTo(self.stackView.snp.bottom).offset(6) |
| | | make.height.equalTo(25) |
| | | } |
| | | }else{ |
| | | self.label_hint.snp.remakeConstraints { make in |
| | | make.left.right.equalToSuperview() |
| | | make.top.equalTo(self.stackView.snp.bottom).offset(6) |
| | | make.height.equalTo(0) |
| | | } |
| | | } |
| | | self.view.layoutIfNeeded() |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | extension HomeListenFight_lesson_5_VC:UICollectionViewDelegate{ |
| | | func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { |
| | | |
| | | if playVoiceAt == nil{ |
| | | alertError(msg: "请先听题");return |
| | | } |
| | | |
| | | viewModel.selectIndex.accept(indexPath) |
| | | |
| | | var answerType:Fight_lessonType = .none |
| | | answerType = .success |
| | | let answer = listen1Model.subjectList[playVoiceAt!] |
| | | let selectAnswer = listen1Model.subjectList[indexPath.row] |
| | | |
| | | let tempSubV = stackView.arrangedSubviews[indexPath.row] as! StudyHandleView |
| | | var answerType:Fight_lessonType = .none |
| | | |
| | | if answer.id == selectAnswer.id{ |
| | | answerType = .success |
| | | }else{ |
| | | answerType = .fail |
| | | } |
| | | |
| | | let tempSubV = stackView.arrangedSubviews[self.playVoiceRealAt!] as! StudyHandleView |
| | | |
| | | switch answerType { |
| | | case .success: |
| | | answterCount += 1 |
| | | rootViewModel.correctNum += 1 |
| | | viewModel.answerType.accept(.success) |
| | | DispatchQueue.main.asyncAfter(deadline: .now()+0.4) { |
| | | let copyView = tempSubV.copyView() |
| | | copyView.view_choose.isHidden = true |
| | |
| | | //获取Cell的顶部试图 |
| | | let flowLayout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout |
| | | |
| | | if let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_lesson_1_CCell", for: self.viewModel.selectIndex.value!) as? ListenFight_lesson_1_CCell{ |
| | | if let cell = self.collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_lesson_1_CCell", for: indexPath) as? ListenFight_lesson_1_CCell{ |
| | | var newRect1 = cell.convert(cell.bounds, to: self.collectionView) |
| | | newRect1.origin.x += (collectionView.frame.origin.x + 5) |
| | | newRect1.origin.y += 94 |
| | | newRect1.origin.y += 94 + 25 |
| | | |
| | | UIView.animateKeyframes(withDuration: 0.4, delay: 0,options: .calculationModeLinear) { |
| | | copyView.frame = CGRect(origin: newRect1.origin, size: CGSize(width: flowLayout.itemSize.width - 10 , height: 40)) |
| | | |
| | | }completion: { _ in |
| | | self.playVoiceRealAt = nil |
| | | self.playVoiceAt = nil |
| | | |
| | | if self.answterCount == 4{ |
| | | self.rootViewModel.answerItems[self.page] = self.listen1Model |
| | | VoicePlayer.share().playerEnd() |
| | | NotificationCenter.default.post(name: NextLession_Noti, object: nil) |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | case .fail: |
| | | viewModel.answerType.accept(.fail) |
| | | rootViewModel.errorNum += 1 |
| | | collectionView.reloadData() |
| | | case .none: |
| | | break |
| | |
| | | |
| | | extension HomeListenFight_lesson_5_VC:UICollectionViewDataSource{ |
| | | func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { |
| | | let model = listen1Model.subjectList[indexPath.row] |
| | | let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_lesson_1_CCell", for: indexPath) as! ListenFight_lesson_1_CCell |
| | | cell.jq_addShadows(shadowColor: .black.withAlphaComponent(0.31), corner: 8, radius: 3, offset: CGSize(width: 0, height: 1), opacity: 1) |
| | | if viewModel.selectIndex.value == indexPath{ |
| | | cell.setState(state: .success) |
| | | cell.setState(state: viewModel.answerType.value) |
| | | }else{ |
| | | cell.setState(state: .none) |
| | | } |
| | | cell.setListen1SubModel(model) |
| | | return cell |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { |
| | | return 4 |
| | | return listen1Model.subjectList.count |
| | | } |
| | | |
| | | func numberOfSections(in collectionView: UICollectionView) -> Int { |