//
|
// HomeListenFight_lesson_1_VC.swift
|
// DolphinEnglishLearnStudent
|
//
|
// Created by 无故事王国 on 2024/5/24.
|
//
|
|
import UIKit
|
import JQTools
|
import RxRelay
|
|
class FightAnswerViewModel{
|
var selectIndex = BehaviorRelay<IndexPath?>(value: nil)
|
var answerType = BehaviorRelay<Fight_lessonType>(value: .none)
|
}
|
|
/// 题目类型一
|
class HomeListenFight_lesson_1_VC: BaseVC {
|
|
private var viewModel = FightAnswerViewModel()
|
|
private lazy var menuView:StudyHandleView = {
|
let menu = StudyHandleView.jq_loadNibView()
|
menu.listenType = .lesson1
|
return menu
|
}()
|
|
private lazy var collectionView:UICollectionView = {
|
let flowLayout = UICollectionViewFlowLayout()
|
let w = (JQ_ScreenW - 194 * 2 - 25) / 2
|
flowLayout.itemSize = CGSize(width: w, height: w * 0.745)
|
flowLayout.minimumLineSpacing = 25
|
flowLayout.minimumInteritemSpacing = 25
|
flowLayout.scrollDirection = .vertical
|
let collection = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
|
collection.register(UINib(nibName: "ListenFight_lesson_1_CCell", bundle: nil), forCellWithReuseIdentifier: "_ListenFight_lesson_1_CCell")
|
return collection
|
}()
|
|
override func viewDidLoad() {
|
super.viewDidLoad()
|
navigationItem.titleView = UIView()
|
}
|
|
override func setUI() {
|
super.setUI()
|
|
collectionView.delegate = self
|
collectionView.dataSource = self
|
collectionView.backgroundColor = UIColor(hexStr: "#C3BFB3")
|
view.addSubview(collectionView)
|
collectionView.snp.makeConstraints { make in
|
make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(101)
|
make.left.equalTo(194)
|
make.right.equalTo(-194)
|
make.bottom.equalToSuperview()
|
}
|
|
view.addSubview(menuView)
|
menuView.snp.makeConstraints { make in
|
make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(24)
|
make.centerX.equalToSuperview()
|
make.height.equalTo(52)
|
make.width.equalTo(199)
|
}
|
}
|
|
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_1_CCell", for: index) as? ListenFight_lesson_1_CCell{
|
var answer:Fight_lessonType = .none
|
|
answer = .success
|
|
switch answer {
|
case .success:
|
self?.viewModel.answerType.accept(.success)
|
self?.answerSuccess(cell)
|
case .fail:
|
self?.viewModel.answerType.accept(.fail)
|
self?.answerFail()
|
default:break
|
}
|
}
|
}).disposed(by: disposeBag)
|
}
|
|
//回答正确
|
private func answerSuccess(_ cell:ListenFight_lesson_1_CCell){
|
menuView.snp.removeConstraints()
|
collectionView.isUserInteractionEnabled = false
|
let v = cell.view_topHandle.convert(cell.bounds, to: self.view)
|
UIView.animate(withDuration: 0.3) {
|
self.menuView.snp.updateConstraints { make in
|
make.top.equalTo(self.view).offset(v.origin.y + UIDevice.jq_safeEdges.top + 101 + 50)
|
make.left.equalToSuperview().offset(v.origin.x + 194)
|
make.width.equalTo(v.size.width + 50)
|
make.height.equalTo(40)
|
}
|
self.view.layoutIfNeeded()
|
}completion: { _ in
|
self.collectionView.reloadData()
|
DispatchQueue.main.asyncAfter(deadline: .now()+2){
|
NotificationCenter.default.post(name: NextLession_Noti, object: nil)
|
self.collectionView.isUserInteractionEnabled = true
|
self.viewModel.answerType.accept(.none)
|
self.viewModel.selectIndex.accept(nil)
|
}
|
}
|
}
|
|
private func answerFail(){
|
self.collectionView.reloadData()
|
}
|
}
|
|
extension HomeListenFight_lesson_1_VC:UICollectionViewDelegate{
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
viewModel.selectIndex.accept(indexPath)
|
}
|
}
|
|
extension HomeListenFight_lesson_1_VC:UICollectionViewDataSource{
|
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
|
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: 5, radius: 5, offset: CGSize(width: 0, height: 1), opacity: 1)
|
cell.backgroundColor = .white
|
|
if viewModel.selectIndex.value == indexPath{
|
cell.setState(state: viewModel.answerType.value)
|
}else{
|
cell.setState(state: .none)
|
}
|
return cell
|
}
|
|
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
|
return 4
|
}
|
}
|