首页 > 解决方案 > 一系列绘图的 Coreplot 动画

问题描述

我有许多图,它们以不同的时间间隔描绘(x,y)数据,并希望一个接一个地按顺序绘制它们(如 gif 文件)。我的方法是生成所有图,使用 Timer.scheduledTimer 并最初隐藏所有图,取消隐藏当前图并在每个触发的时间表中隐藏以前的图。每个绘图隐藏/取消隐藏之间的时间显示空白图表的时间比显示的绘图时间长。每个图有 32x32 个数据点。我怎样才能加快速度,这样我就再也看不到空白图表了?另一种方法是淡出一个情节,同时引入下一个情节,但我看到了相同的效果。

   @objc func tapAnimationButton(_ sender: Any) {
        isAnimating = !isAnimating
        plotSpacesUserInteraction = !plotSpacesUserInteraction
        if let _animationButton = animationButton {
            _animationButton.isSelected = isAnimating
            previousAnimatedPlot = nil
            if isAnimating {
                animationCounter = 0
                for i in 0..<plotDetails.count {
//                    fieldsplots[i].isHidden = true
                   fieldsplots[i].opacity = 0.0
                }
                animationTimer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(animateGraph(_:)), userInfo: nil, repeats: true)
                if let _animationTimer = animationTimer {
                    animateGraph(_animationTimer)
                }
            }
            else {
                animationTimer?.invalidate()
                animationTimer = nil
            }
        }
    }
    
    @objc func animateGraph(_ timer: Timer) {
        if animationSeconds > 120.0  {
            timer.invalidate()
            self.animationTimer = nil
            animationSeconds = 0.0
            animationCounter = 0
            previousAnimatedPlot = nil
        }
        else {
            if let currentAnimatedPlot = self.graph.plot(at: animationCounter) {
//                previousAnimatedPlot?.isHidden = true
//                currentAnimatedPlot.isHidden = false

                previousAnimatedPlot?.opacity = 1.0
                let fadeOutAnimation = CABasicAnimation(keyPath: "opacity")
                fadeOutAnimation.duration = 0.1
                fadeOutAnimation.isRemovedOnCompletion = false
                fadeOutAnimation.fillMode = CAMediaTimingFillMode.forwards
                fadeOutAnimation.toValue = Float(0.0)
                previousAnimatedPlot?.add(fadeOutAnimation, forKey: "animateOpacity")
                
                currentAnimatedPlot.opacity = 0.0
                let fadeInAnimation = CABasicAnimation(keyPath: "opacity")
                fadeInAnimation.duration = 0.1
                fadeInAnimation.isRemovedOnCompletion = false
                fadeInAnimation.fillMode = CAMediaTimingFillMode.forwards
                fadeInAnimation.toValue = Float(1.0)
                currentAnimatedPlot.add(fadeInAnimation, forKey: "animateOpacity")

                previousAnimatedPlot = currentAnimatedPlot
            }
            animationSeconds += 0.5
            animationCounter += 1;
            if animationCounter >= plotDetails.count {
                animationCounter = 0
            }
        }
    }

标签: core-plot

解决方案


作为回答的附加组件,为了生成 gif 图像,需要按顺序隐藏/取消隐藏图

      for currentPlot in self.graph.allPlots() {
            currentPlot.isHidden = true
        }
        var images: [UIImage] = []
        var previousPlot: CPTPlot?
        
        for currentPlot in self.graph.allPlots() {
            if let _previousPlot = previousPlot {
                _previousPlot.isHidden = true
            }
            currentPlot.isHidden = false
        
            if let image = graph.imageOfLayer() {
                images.append(image)
            }
            previousPlot = currentPlot
        }
        for currentPlot in self.graph.allPlots() {
            currentPlot.isHidden = false
        }
        
        if images.count > 2 {
            TwoDPlot_Utilities.GIFExport(with: images, plot: thisplot, plotIndex: plotIndex, frameDelay: 0.5)
        }

在此处输入图像描述


推荐阅读