core-plot - 一系列绘图的 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
}
}
}
解决方案
作为回答的附加组件,为了生成 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)
}
推荐阅读
- json - 为什么正则表达式模式会导致 jsonschema 中出现“错误字符串”错误?
- r - 什么时候在 RStudio 中将数据存储在内存之外变得有益?
- r - 在 R 中将字符编码转换为 UTF-8(不使用 stringi)
- android - 电子邮件意图不允许我仅过滤电子邮件应用程序并放置自定义标题
- swift - 如何向上移动视图“X”点?
- python - Heroku dyno 中的 DATABASE_URL 为空
- spring - 方法级别的角色和权限 Spring boot
- reactjs - 无法从输入reactjs中删除值字符
- kubernetes - ingress 无法获取默认的 http 后端
- kotlin - 如何从 Kotlin 中的字符串数组中输出随机字符串