简体   繁体   English

集合视图单元格中的 UIView Animation

[英]UIView Animation in Collectionview cell

I make a custom UICollectionViewCell which is use to display BarChart graph.我制作了一个自定义 UICollectionViewCell,用于显示 BarChart 图。 Now everything is working but I also want to put some animations.现在一切正常,但我还想放一些动画。 I want to put animations on dark green bars to animate but When I used animation code It's not working properly.我想将动画放在深绿色条上进行动画处理,但是当我使用 animation 代码时,它无法正常工作。

Here's the UICollectionView Xib Screen shot这是 UICollectionView Xib 屏幕截图

在此处输入图像描述

Here's the photos of animation staring like this这是animation这样盯着看的照片在此处输入图像描述

在此处输入图像描述

and complete the animation like this并像这样完成 animation 在此处输入图像描述

CollectionView cell class code 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()
        }
    }

}

Kindly tell me or suggest me how I can achieve my desired result Thanks.请告诉我或建议我如何才能达到我想要的结果谢谢。

Try to constant value before UIView.animate尝试在 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