【问题标题】:Swift+UIImageView: switch between two animation frame sequences?Swift+UIImageView:在两个动画帧序列之间切换?
【发布时间】:2019-04-30 14:29:38
【问题描述】:

动画代码示例:

func startAnimation(index: Int) {
var jumpImages = ["Jump_1","Jump_2","Jump_3","Jump_4","Jump_5","Jump_6","Jump_7","Jump_8","Jump_9","Jump_10"]
if index == 1 {
     jumpImages = ["Jump_11","Jump_21","Jump_31","Jump_41","Jump_51","Jump_61","Jump_71","Jump_81","Jump_91","Jump_101"]
}
    var images = [UIImage]()
    for image in jumpImages{
        images.append(UIImage(named: image)!)
    }
    self.imageView.frame.origin.y = self.imageView.frame.origin.y + 100.0
    self.imageView.animationImages = images
    self.imageView.animationDuration = 2.0
    self.imageView.animationRepeatCount = 1
    self.imageView.startAnimating()
}
startAnimation(index: 0)
...
startAnimation(index: 1)

注意:两个startAnimation 调用都在主线程中,但不在同一个运行循环中。

就我而言,我想将jumpImages 更改为另一组图像。我应该取消之前的动画并开始新的动画,但看起来我将最后一张图像设置为 jumpImages 序列。

如何解决这个问题?

【问题讨论】:

  • 有什么问题?
  • 你能澄清你的问题吗?您正在为您的第一组图像设置动画。然后呢?你要取消动画?或者改变动画图像集?或两者?还是开始一个新的?请包含附加代码并重新表述您的问题。
  • @drewster 编辑了问题。希望现在很清楚。此代码不包括停止上一个动画,因为我没有找到任何类似可行的解决方案。
  • @Sh_Khan 尝试此代码或类似代码并尝试在开始第二个动画之前停止/取消第一个动画
  • 为什么不直接隐藏/删除这个图像视图并用不同的图像视图替换它?

标签: swift animation uiimageview uiimage frames


【解决方案1】:

好的,我想我现在理解你的问题了——从你的陈述来看,“[...] 但看起来我设置了 jumpImages 序列中的最后一张图片”,我想你的意思是当你调用 startAnimation(index: 1) 时,你看到的只是索引为 0 的动画的最后一帧,而没有看到索引为 1 的动画。

假设这是正确的,您的问题将是一个竞争条件。

当您第二次使用索引 1 调用 startAnimation() 时,第一个动画可能仍在进行中,也可能不在进行中。

解决方案是在更改所有图像并开始新动画之前调用 self.imageView.stopAnimating()。最佳做法是在调用之前检查 imageView.isAnimating 标志。像这样的:

func startAnimation(index: Int) {
    var jumpImages = ["Jump_1","Jump_2","Jump_3","Jump_4","Jump_5","Jump_6","Jump_7","Jump_8","Jump_9","Jump_10"]
    if index == 1 {
         jumpImages = ["Jump_11","Jump_21","Jump_31","Jump_41","Jump_51","Jump_61","Jump_71","Jump_81","Jump_91","Jump_101"]
    }
    var images = [UIImage]()
    for image in jumpImages{
        images.append(UIImage(named: image)!)
    }

    if self.imageView.isAnimating {
        self.imageView.stopAnimating()
    }
    self.imageView.frame.origin.y = self.imageView.frame.origin.y + 100.0
    self.imageView.animationImages = images
    self.imageView.animationDuration = 2.0
    self.imageView.animationRepeatCount = 1
    self.imageView.startAnimating()
}
startAnimation(index: 0)
...
startAnimation(index: 1)

此外,由于您在函数中而不是在闭包中,因此您可以删除所有对 self. 的引用,这会使事情变得更短:

func startAnimation(index: Int) {
    var jumpImages = ["Jump_1","Jump_2","Jump_3","Jump_4","Jump_5","Jump_6","Jump_7","Jump_8","Jump_9","Jump_10"]
    if index == 1 {
         jumpImages = ["Jump_11","Jump_21","Jump_31","Jump_41","Jump_51","Jump_61","Jump_71","Jump_81","Jump_91","Jump_101"]
    }
    var images = [UIImage]()
    for image in jumpImages{
        images.append(UIImage(named: image)!)
    }

    if imageView.isAnimating {
        imageView.stopAnimating()
    }
    imageView.frame.origin.y = imageView.frame.origin.y + 100.0
    imageView.animationImages = images
    imageView.animationDuration = 2.0
    imageView.animationRepeatCount = 1
    imageView.startAnimating()
}
startAnimation(index: 0)
...
startAnimation(index: 1)

【讨论】:

    猜你喜欢
    • 2012-02-08
    • 2011-11-24
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-07
    相关资源
    最近更新 更多