ios 表视图单元格中UI视图的动画不一致

yqkkidmi  于 2023-02-14  发布在  iOS
关注(0)|答案(1)|浏览(118)

由于某种原因,当正在进行动画的单元格离开屏幕并返回时,动画速度会发生变化。点击单元格时,会打开一个新的视图控制器。当我从视图控制器返回到初始视图时,动画完全停止。
到目前为止,我已经尝试在cellForRowAt中启动动画,但似乎也不起作用。
问题视频链接:https://drive.google.com/file/d/1jt5IM1Ya4gIfzb1ok-NTmS2QTnrSTNoG/view?usp=sharing
下面是willDisplay单元格的代码,以及在表格视图单元格中动画显示UI视图的函数。

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if let cell = cell as? DirectionSummaryTableViewCell {
            if let isLive = cell.isLive {
                if isLive {
                    cell.animateBusStatusBackground()
                } else {
                    cell.removeAnimation()
                }
            }
        }
    }
func animateBusStatusBackground() {

        UIView.animate(withDuration: 1.0, delay: 0.0, options: [.repeat, .autoreverse, .curveEaseInOut], animations: { [weak self] in
            if self?.busStatusView.backgroundColor == .red {
                self?.busStatusView.backgroundColor = .grey6
            } else {
                self?.busStatusView.backgroundColor = .red
            }
        }, completion: nil)
    }
func removeAnimation() {
        self.busStatusView.layer.removeAllAnimations()
        self.layer.removeAllAnimations()
        self.layoutIfNeeded()
    }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        guard let cell = tableView.dequeueReusableCell(withIdentifier: DirectionSummaryTableViewCell.identifier, for: indexPath) as?  DirectionSummaryTableViewCell else { return UITableViewCell() }

        cell.configure(with: viewModel.placeToPlacePossibleDirections[indexPath.section][indexPath.row].directions, 
tripDuration: viewModel.placeToPlacePossibleDirections[indexPath.section][indexPath.row].tripTime,
destinationArrivalTime:viewModel.placeToPlacePossibleDirections[indexPath.section][indexPath.row].reachBy,
busDepartureTime: viewModel.placeToPlacePossibleDirections[indexPath.section][indexPath.row].directions.routes[0].departureTime, 
startLocation: viewModel.placeToPlacePossibleDirections[indexPath.section][indexPath.row].directions.routes[0].stops[0].name, addFullLabel: true, 
isLive: viewModel.placeToPlacePossibleDirections[indexPath.section][indexPath.row].responseType == "realtime")

        return cell
    }
func configure(with directions: PlaceToPlaceBusDirections, tripDuration: Double, destinationArrivalTime: String, busDepartureTime: String, startLocation: String, addFullLabel: Bool, isLive: Bool) {
        self.directions = directions
        self.isLive = isLive
        self.collectionView.reloadData()
        self.collectionView.layoutIfNeeded()
        var formattedTripDuration = ""
        if tripDuration > 60 {
            let hrs = Int(tripDuration / 60)
            formattedTripDuration += String(hrs) + " hr"
            if hrs > 1 { formattedTripDuration += "s " } else { formattedTripDuration += " " }
        }
        formattedTripDuration += String(Int(tripDuration) % 60)

        self.tripDurationLabel.text = formattedTripDuration + " mins"
        self.destinationArrivalTimeLabel.text = Date.dateStringFromString(dateString: destinationArrivalTime)
        if addFullLabel {
            self.busDeparturePlaceAndTimeLabel.text = ("Leaves at " + Date.dateStringFromString(dateString: busDepartureTime) + " from " + startLocation).maxLength(length: 40)
        } else {
            self.busDeparturePlaceAndTimeLabel.text = ("Leaves at " + Date.dateStringFromString(dateString: busDepartureTime)).maxLength(length: 40)
        }

        if !isLive {
            busStatusText.text = "Live"
            busStatusText.textColor = .white
        } else {
            busStatusText.text = "Scheduled"
            busStatusText.textColor = .grey2
            busStatusView.backgroundColor = .grey6
        }

        removeAnimation()
    }

我如何修复这个问题,使动画是相同的所有时间?

nx7onnlm

nx7onnlm1#

要在从不同屏幕返回时设置单元格的动画效果,可以使用以下方法:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    if let visibleCells = tableView.visibleCells as? [DirectionSummaryTableViewCell] {
        visibleCells.forEach {
            if let isLive = $0.isLive {
                if isLive {
                    $0.animateBusStatusBackground()
                } else {
                    $0.removeAnimation()
                }
            }
        }
    }
}

相关问题