繁体   English   中英

集合视图单元格中的 UIView Animation

[英]UIView Animation in Collectionview cell

我制作了一个自定义 UICollectionViewCell,用于显示 BarChart 图。 现在一切正常,但我还想放一些动画。 我想将动画放在深绿色条上进行动画处理,但是当我使用 animation 代码时,它无法正常工作。

这是 UICollectionView Xib 屏幕截图

在此处输入图像描述

这是animation这样盯着看的照片在此处输入图像描述

在此处输入图像描述

并像这样完成 animation 在此处输入图像描述

CollectionView 单元格 class 代码

class BarChartCollectionViewCell: UICollectionViewCell {
    
    //MARK: - OUTLETS
    @IBOutlet weak var totalAmount: UILabel!
    @IBOutlet weak var monthLabel: UILabel!
    
    @IBOutlet weak var barChartTotalView: UIView!
    @IBOutlet weak var barChartSpendAmountView: UIView!
    @IBOutlet weak var barChartSpendAmountViewHeight: NSLayoutConstraint!
    
    //MARK: - PROPERTIES
    class var identifier: String {
        "BarChartCollectionViewCell"
    }
    
    class var nibName: String {
        "BarChartCollectionViewCell"
    }
    
    var barChartValue: CGFloat = 0.0
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let height = barChartTotalView.frame.size.height
        self.barChartSpendAmountViewHeight.constant = height * self.barChartValue

    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        barChartTotalView.layer.cornerRadius = barChartTotalView.frame.size.width / 2
        barChartSpendAmountView.layer.cornerRadius = barChartSpendAmountView.frame.size.width / 2
        barChartTotalView.clipsToBounds = true
        
    }
    
    func configureCell(month: String,amount: Double) {
        self.monthLabel.text = String.LanguageStringFor(month)
        
        self.totalAmount.text = "QAR \(amount)"
        
        let percentage = amount / 190.0
        self.barChartValue = percentage
        
        UIView.animate(withDuration: 1, delay: 0.1, options: .curveEaseInOut) {
            self.barChartTotalView.layoutIfNeeded()
        }
    }

}

请告诉我或建议我如何才能达到我想要的结果谢谢。

尝试在 UIView.animate 之前设置常量值

self.barChartSpendAmountViewHeight.constant = height * self.barChartValue
UIView.animate(withDuration: 1, delay: 0.1, options: .curveEaseInOut) {
           self.barChartTotalView.layoutIfNeeded()
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM