| | |
| | | return sta |
| | | }() |
| | | |
| | | private var listen1Model:Listen1Model! |
| | | private var page:Int! |
| | | private var answterCount:Int = 0 //回答计数,用于确定角标 |
| | | var rootViewModel:HomeListenFightViewModel! |
| | | |
| | | 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() |
| | | navigationItem.titleView = UIView() |
| | | viewModel.selectIndex.accept(IndexPath(row: 0, section: 0)) |
| | | // viewModel.selectIndex.accept(IndexPath(row: 0, section: 0)) |
| | | |
| | | setAnswerStackView() |
| | | |
| | | VoicePlayer.share().playEnd { [weak self] in |
| | | guard let weakSelf = self else { return } |
| | | if weakSelf.viewModel.selectIndex.value != nil{ |
| | | weakSelf.collectionView.reloadItems(at: [weakSelf.viewModel.selectIndex.value!]) |
| | | weakSelf.viewModel.selectIndex.accept(nil) |
| | | } |
| | | weakSelf.collectionView.reloadData() |
| | | } |
| | | } |
| | | |
| | | override func setUI() { |
| | |
| | | |
| | | private func setAnswerStackView(){ |
| | | |
| | | for subView in stackView.arrangedSubviews{ |
| | | subView.removeFromSuperview() |
| | | } |
| | | stackView.removeArrangedSubviews() |
| | | |
| | | var tempImageArray = [String]() |
| | | |
| | | tempImageArray.append(listen1Model.subjectList[2].img ?? "") |
| | | tempImageArray.append(listen1Model.subjectList[4].img ?? "") |
| | | tempImageArray.append(listen1Model.subjectList[5].img ?? "") |
| | | // tempImageArray.shuffle() |
| | | |
| | | view.addSubview(stackView) |
| | | stackView.snp.makeConstraints { make in |
| | |
| | | answerView.alpha = 0 |
| | | answerView.btn_choose.addTarget(self, action: #selector(chooseAnswerAction), for: .touchUpInside) |
| | | answerView.btn_fullscreen.addTarget(self, action: #selector(fullscreenAction), for: .touchUpInside) |
| | | answerView.img_cover.contentMode = .scaleToFill |
| | | answerView.btn_choose.tag = 10+i |
| | | answerView.btn_fullscreen.tag = 20+i |
| | | answerView.imageUrl = tempImageArray[i] |
| | | answerView.img_cover.sd_setImage(with: URL(string: tempImageArray[i])) |
| | | answerView.snp.makeConstraints { make in |
| | | make.width.equalTo(85) |
| | | make.height.equalTo(52) |
| | |
| | | } |
| | | |
| | | @objc private func chooseAnswerAction(btn:UIButton){ |
| | | |
| | | if viewModel.selectIndex.value == nil{alertError(msg: "请先听题");return} |
| | | |
| | | let index = btn.tag - 10 |
| | | |
| | | var subV:Lesson_3_AnswerView? |
| | |
| | | } |
| | | |
| | | var answerType:Fight_lessonType = .none |
| | | answerType = .success |
| | | |
| | | var valueIndex:Int! //图片的角标 |
| | | if viewModel.selectIndex.value?.section == 0{ |
| | | valueIndex = viewModel.selectIndex.value!.row |
| | | } |
| | | if viewModel.selectIndex.value?.section == 1{ |
| | | valueIndex = viewModel.selectIndex.value!.row + 3 |
| | | } |
| | | |
| | | if subV?.imageUrl == listen1Model.subjectList[valueIndex].img{ |
| | | answerType = .success |
| | | }else{ |
| | | answerType = .fail |
| | | } |
| | | |
| | | switch answerType { |
| | | case .success: |
| | | rootViewModel.correctNum += 1 |
| | | viewModel.answerType.accept(.success) |
| | | |
| | | let copyViewFrame = subV?.convert(subV!.bounds, to: self.view) |
| | | |
| | | let copyView = subV?.copyView() |
| | | copyView?.frame = copyViewFrame! |
| | | copyView?.backgroundColor = .red |
| | | copyView?.img_cover.image = subV?.img_cover.image |
| | | copyView?.img_cover.contentMode = .scaleToFill |
| | | self.view.addSubview(copyView!) |
| | | copyView?.layoutIfNeeded() |
| | | |
| | | if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_lesson_3_CCell", for: viewModel.selectIndex.value!) as? ListenFight_lesson_3_CCell{ |
| | | cell.layoutIfNeeded() |
| | | var ansterIndePath:IndexPath? |
| | | if viewModel.selectIndex.value?.section == 0{ |
| | | ansterIndePath = IndexPath(row: 2, section: 0) |
| | | } |
| | | |
| | | if viewModel.selectIndex.value?.section == 1 && (viewModel.selectIndex.value?.row == 0 || viewModel.selectIndex.value?.row == 1){ |
| | | ansterIndePath = IndexPath(row: 1, section: 1) |
| | | } |
| | | |
| | | if viewModel.selectIndex.value?.section == 1 && viewModel.selectIndex.value?.row == 2{ |
| | | ansterIndePath = IndexPath(row: 2, section: 1) |
| | | } |
| | | |
| | | guard ansterIndePath != nil else {return} |
| | | |
| | | if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_lesson_3_CCell", for: ansterIndePath!) as? ListenFight_lesson_3_CCell{ |
| | | var newFrame = cell.img_cover.convert(cell.img_cover.bounds, to: self.view) |
| | | copyView?.img_cover.image = cell.img_cover.image |
| | | newFrame.origin.x += 97 |
| | | newFrame.origin.y += (JQ_NavBarHeight - 15) |
| | | newFrame.origin.x += 0 |
| | | newFrame.origin.y += 0 |
| | | UIView.animate(withDuration: 0.4) { |
| | | copyView?.frame = newFrame |
| | | } completion: { _ in |
| | | for subView in self.stackView.arrangedSubviews{ |
| | | subView.removeFromSuperview() |
| | | self.answterCount += 1 |
| | | if self.answterCount == 3{ |
| | | self.rootViewModel.answerItems[self.page] = self.listen1Model |
| | | NotificationCenter.default.post(name: NextLession_Noti, object: nil);return |
| | | } |
| | | |
| | | DispatchQueue.main.asyncAfter(deadline: .now()+1.5) { |
| | | self.setAnswerStackView() |
| | | self.viewModel.selectIndex.accept(nil) |
| | | } |
| | | } |
| | | } |
| | | |
| | | case .fail: |
| | | rootViewModel.errorNum += 1 |
| | | viewModel.answerType.accept(.fail) |
| | | UIView.animate(withDuration: 0.4) { |
| | | subV?.img_state.alpha = 1 |
| | |
| | | |
| | | extension HomeListenFight_lesson_3_VC:UICollectionViewDelegate{ |
| | | func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { |
| | | viewModel.selectIndex.accept(indexPath) |
| | | setAnswerStackView() |
| | | |
| | | } |
| | | } |
| | | |
| | |
| | | func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { |
| | | let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_lesson_3_CCell", for: indexPath) as! ListenFight_lesson_3_CCell |
| | | cell.backgroundColor = .clear |
| | | cell.indexPath = indexPath |
| | | cell.contentView.backgroundColor = .clear |
| | | cell.palyVoiceAt {[weak self] index in |
| | | self?.viewModel.selectIndex.accept(index) |
| | | if (index.section == 0 && index.row == 2) || (index.section == 1 && index.row > 0){ |
| | | self?.viewModel.selectIndex.accept(index) |
| | | self?.setAnswerStackView() |
| | | } |
| | | |
| | | collectionView.reloadItems(at: [index]) |
| | | } |
| | | |
| | | if indexPath.section == 0{ |
| | | let model = listen1Model.subjectList[indexPath.row] |
| | | if indexPath.row != 2{ |
| | | cell.img_cover.sd_setImage(with: URL(string: model.img)) |
| | | }else{ |
| | | cell.img_cover.image = nil |
| | | } |
| | | cell.setModel(model,isplaying: viewModel.selectIndex.value == indexPath) |
| | | } |
| | | |
| | | if indexPath.section == 1{ |
| | | let model = listen1Model.subjectList[indexPath.row + 3] |
| | | if indexPath.row == 0{ |
| | | cell.img_cover.sd_setImage(with: URL(string: model.img)) |
| | | }else{ |
| | | cell.img_cover.image = nil |
| | | } |
| | | cell.setModel(model,isplaying: viewModel.selectIndex.value == indexPath) |
| | | } |
| | | return cell |
| | | } |
| | | |