add
无故事王国
2024-05-29 0e9f7cd7a295ff06a5b767d11c5a03d6030c3563
DolphinEnglishLearnStudent/Moudle/Home/Listen/VC/HomeListenGame_2_VC.swift
@@ -9,11 +9,178 @@
class HomeListenGame_2_VC: BaseVC {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
            private var viewModel = FightAnswerViewModel()
            private lazy var label_time:UILabel = {
                        let label = UILabel()
                        label.textColor = UIColor(hexStr: "#EE1111")
                        label.text = "0s"
                        label.font = UIFont.init(name: "Impact", size: 36)
                        return label
            }()
            private lazy var collectionView:UICollectionView = {
                        let flowLayout = UICollectionViewFlowLayout()
                        let w = (JQ_ScreenW - 40 * 2 - 30) / 4.0
                        flowLayout.itemSize = CGSize(width: w, height: w * 0.675)
                        flowLayout.minimumInteritemSpacing = 10
                        flowLayout.minimumLineSpacing = 20
                        flowLayout.scrollDirection = .vertical
                        let collection = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
                        collection.contentInset = UIEdgeInsets(top: 101, left: 40, bottom: 0, right: 40)
                        collection.register(ListenFight_Game_Pocket_CCell.self, forCellWithReuseIdentifier: "_ListenFight_Game_Pocket_CCell")
                        return collection
            }()
            private var timer:Timer?
            private var times:Int = 600
            override func viewDidLoad() {
                        super.viewDidLoad()
                        startTimer()
            }
            override func setUI() {
                        super.setUI()
                        let label_surplusTitle = UILabel()
                        label_surplusTitle.textColor = UIColor.black
                        label_surplusTitle.text = "答题剩余时间:"
                        label_surplusTitle.font = .systemFont(ofSize: 14, weight: .medium)
                        view.addSubview(label_surplusTitle)
                        label_surplusTitle.snp.makeConstraints { make in
                                    make.left.equalTo(40)
                                    make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top).offset(46)
                                    make.height.equalTo(20)
                        }
                        view.addSubview(label_time)
                        label_time.snp.makeConstraints { make in
                                    make.left.equalTo(label_surplusTitle.snp.right).offset(0)
                                    make.centerY.equalTo(label_surplusTitle)
                                    make.height.equalTo(44)
                        }
                        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
                                    make.top.equalTo(self.view.safeAreaLayoutGuide.snp.top)
                                    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_time.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()
                                    case .fail:
                                                viewModel.answerType.accept(.fail)
                                                collectionView.reloadData()
                                                DispatchQueue.main.asyncAfter(wallDeadline: .now()+2.5) {
                                                            self.viewModel.answerType.accept(.none)
                                                            self.collectionView.reloadData()
                                                }
                                    case .none:
                                                break
                        }
            }
}
extension HomeListenGame_2_VC:UICollectionViewDelegate{
            func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
                        viewModel.selectIndex.accept(indexPath)
                        _ = self.collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_Game_Pocket_CCell", for: indexPath) as! ListenFight_Game_Pocket_CCell
            }
}
extension HomeListenGame_2_VC:UICollectionViewDataSource{
            func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_ListenFight_Game_Pocket_CCell", for: indexPath) as! ListenFight_Game_Pocket_CCell
                        return cell
            }
            func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
                        return 12
            }
}
extension HomeListenGame_2_VC: UICollectionViewDelegateFlowLayout {
            // 实现以下方法以支持转场动画
            func collectionView(_ collectionView: UICollectionView, transitionLayoutForOldLayout fromLayout: UICollectionViewLayout, newLayout toLayout: UICollectionViewLayout) -> UICollectionViewTransitionLayout {
                        return UICollectionViewTransitionLayout(currentLayout: fromLayout, nextLayout: toLayout)
            }
            // 实现以下方法以返回自定义的转场动画控制器
            func collectionView(_ collectionView: UICollectionView, transitionAnimationControllerForOldCell: UICollectionViewCell, newCell: UICollectionViewCell) -> UIViewControllerAnimatedTransitioning? {
                        return FlipAnimationController()
            }
            // FlipAnimationController实现转场动画的具体细节
            class FlipAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
                        func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
                                    return 0.3
                        }
                        func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
                                    guard let fromView = transitionContext.view(forKey: UITransitionContextViewKey.from) else { return }
                                    guard let toView = transitionContext.view(forKey: UITransitionContextViewKey.to) else { return }
                                    let containerView = transitionContext.containerView
                                    // 初始化动画参数
                                    toView.frame = transitionContext.finalFrame(for: transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!)
                                    containerView.addSubview(toView)
                                    // 翻牌动画
                                    UIView.transition(from: fromView, to: toView, duration: transitionDuration(using: transitionContext), options: [.transitionFlipFromLeft]) { completed in
                                                transitionContext.completeTransition(completed)
                                    }
                        }
            }
}