add
无故事王国
2024-05-29 0e9f7cd7a295ff06a5b767d11c5a03d6030c3563
DolphinEnglishLearnStudent/Moudle/Home/Listen/VC/HomeListenGame_1_VC.swift
@@ -11,6 +11,31 @@
            private var viewModel = FightAnswerViewModel()
            private lazy var label_class:UILabel = {
                        let label = UILabel()
                        label.textColor = .white
                        label.text = "1"
                        label.textAlignment = .center
                        label.font = UIFont.init(name: "Impact", size: 21)
                        return label
            }()
            private lazy var label_hint:UILabel = {
                        let label = UILabel()
                        label.textColor = UIColor(hexStr: "#EE1111")
                        label.text = "请在10s内选择答案!"
                        label.textAlignment = .center
                        label.font = .systemFont(ofSize: 14, weight: .medium)
                        return label
            }()
            private lazy var view_studyHandleView:StudyHandleView = {
                        let studyHandleView = StudyHandleView.jq_loadNibView()
                        studyHandleView.view_choose.isHidden = true
                        return studyHandleView
            }()
            private lazy var collectionView:UICollectionView = {
                        let flowLayout = UICollectionViewFlowLayout()
                        let w = (JQ_ScreenW - 35 * 2 - 12) / 4.0
@@ -24,12 +49,15 @@
                        return collection
            }()
            private var timer:Timer?
            private var times:Int = 10
    override func viewDidLoad() {
        super.viewDidLoad()
                        ChooseLevelView.show { level in
                                    GameBeginTipView.show {
                                                self.startTimer()
                                    }
                        }
    }
@@ -37,9 +65,44 @@
            override func setUI() {
                        super.setUI()
                        let view_class_title = UIView()
                        view_class_title.jq_cornerRadius = 16
                        view_class_title.backgroundColor = UIColor(hexStr: "#FBCF0F")
                        view.addSubview(view_class_title)
                        view_class_title.snp.makeConstraints { make in
                                    make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(40)
                                    make.left.equalToSuperview().offset(40)
                                    make.height.equalTo(32)
                                    make.width.greaterThanOrEqualTo(32)
                        }
                        view_class_title.addSubview(label_class)
                        label_class.snp.makeConstraints { make in
                                    make.left.equalTo(11)
                                    make.right.equalTo(-12)
                                    make.centerY.equalToSuperview()
                        }
                        view.addSubview(view_studyHandleView)
                        view_studyHandleView.snp.makeConstraints { make in
                                    make.left.equalTo(view_class_title.snp.right).offset(12)
                                    make.centerY.equalTo(view_class_title)
                                    make.width.equalTo(159)
                                    make.height.equalTo(52)
                        }
                        view.addSubview(label_hint)
                        label_hint.snp.makeConstraints { make in
                                    make.left.equalTo(view_studyHandleView.snp.right).offset(23)
                                    make.centerY.equalTo(view_class_title)
                                    make.height.equalTo(20)
                        }
                        collectionView.delegate = self
                        collectionView.dataSource = self
                        collectionView.showsVerticalScrollIndicator = false
                        collectionView.jq_addShadows(shadowColor: UIColor.black.withAlphaComponent(0.1), corner: 8, radius: 10, offset: CGSize(width: 0, height: 2), opacity: 1)
                        collectionView.backgroundColor = .clear
                        view.addSubview(collectionView)
                        collectionView.snp.makeConstraints { make in
@@ -47,21 +110,98 @@
                                    make.left.right.equalToSuperview()
                                    make.bottom.equalToSuperview()
                        }
                        view.layoutIfNeeded()
            }
            private func startTimer(){
                        if timer == nil{
                                    timer = Timer(timeInterval: 1.0, target: self, selector: #selector(runloopTime), userInfo: nil, repeats: true)
                        }
                        timer?.fire()
                        RunLoop.current.add(timer!, forMode: .default)
            }
            @objc private func runloopTime(){
                        times -= 1
                        label_hint.text = "请在\(times)s内选择答案!"
                        if times == 0{
                                    timer?.fireDate = .distantFuture
                                    DispatchQueue.main.asyncAfter(deadline: .now()+3) {
                                                self.times = 10
                                                self.timer?.fireDate = .distantPast
                                    }
                        }
            }
            private func nextQuesAction(){
            }
            private func answerQuestion(){
                        view.layoutIfNeeded()
                        var answerType:Fight_lessonType = .none
                        answerType = .success
                        switch answerType {
                                    case .success:
                                                viewModel.answerType.accept(.success)
                                                collectionView.reloadData()
                                                if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_Game_CCell", for: viewModel.selectIndex.value!) as? ListenFight_Game_CCell{
                                                            let newRect = cell.contentView.convert(cell.bounds, from: self.collectionView)
                                                            let x = abs(newRect.origin.x) + self.collectionView.contentInset.left + 5
                                                            let y = abs(newRect.origin.y) + 99 + 5
                                                            let layout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
                                                            let copyView = view_studyHandleView.copyView()
                                                            copyView.view_choose.isHidden = true
                                                            view.addSubview(copyView)
                                                            UIView.animate(withDuration: 0.5) {
                                                                        copyView.frame = CGRect(x: x, y: y, width: layout.itemSize.width - 10, height: 40)
                                                            } completion: { _ in
                                                                        DispatchQueue.main.asyncAfter(deadline: .now()+0.5) {
                                                                                    self.viewModel.answerType.accept(.none)
                                                                                    self.collectionView.reloadData()
                                                                                    self.times = 11
                                                                                    self.nextQuesAction()
                                                                        }
                                                            }
                                                }
                                    case .fail:
                                                viewModel.answerType.accept(.fail)
                                                collectionView.reloadData()
                                                DispatchQueue.main.asyncAfter(wallDeadline: .now()+2.5) {
                                                            self.viewModel.answerType.accept(.none)
                                                            self.collectionView.reloadData()
                                                            self.times = 11
                                                            self.nextQuesAction()
                                                }
                                    case .none:
                                                break
                        }
            }
}
extension HomeListenGame_1_VC:UICollectionViewDelegate{
            func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
                        viewModel.selectIndex.accept(indexPath)
                        answerQuestion()
            }
}
extension HomeListenGame_1_VC:UICollectionViewDataSource{
            func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_Game_CCell", for: indexPath) as! ListenFight_Game_CCell
                        cell.backgroundColor = .jq_randomColor
                        cell.contentView.backgroundColor = .jq_randomColor
                        if viewModel.selectIndex.value == indexPath{
                                    cell.setState(state: viewModel.answerType.value)
                        }else{
                                    cell.setState(state: .none)
                        }
                        return cell
            }