swift - ios - Swift 4 : How to remove UIImageViews when UIImageView. 动画图像完成了吗?

标签 swift

我在我的项目中使用 .animationImages 作为 UIImageViews(这是一个游戏)。当用户按下提示按钮时,我以编程方式创建数量可变的 UIImageView,这些 View 将出现在屏幕上并播放相同的图像序列一次。当动画停止时,我需要从 View 中删除 UIImageViews,但我不知道如何执行此操作。

我已经浏览了 .animationImages 和 .startAnimating 的快速文档,但它实际上只是一个段落和一行,两者都与完成或完成通知等无关。任何帮助将不胜感激。

private func hideAnswerButtonsHint() {

    // Clear all of the answer boxes that aren't hint locked
    resetAnswerBoxes()

    var imageArray = [UIImage]()
    var remainingLetters = [String]()

    for imageCount in 1...8 {
        let imageName = "green_smoke_puff_0\(imageCount)"
        let image = UIImage(named: imageName)!
        imageArray.append(image)
    }

    for answerBox in answerBoxArray where answerBox.hintLock == false {
        if let letter = answerBoxDict[answerBox.tag] {
            remainingLetters.append(letter)
        }
    }

    for answerButton in answerButtonArray where answerButton.hintLock == false {
        if let index = remainingLetters.index(of: answerButton.titleText) {
            answerButton.decompress()
            remainingLetters.remove(at: index)
        } else {
            let frame = answerButton.superview?.convert(answerButton.frame.origin, to: nil)

            let imageView = UIImageView()
            imageView.animationImages = imageArray
            imageView.animationDuration = 0.5
            imageView.animationRepeatCount = 1
            imageView.frame = CGRect(x: frame!.x, y: frame!.y, width: answerButton.frame.width, height: answerButton.frame.height)

            view.addSubview(imageView)
            imageView.startAnimating()

            answerButton.compress()
            answerButton.hintLock = true
        }
    }
}

最佳答案

UIImageView 是一个 NSObject,因此您始终可以使用 KVO查看其属性:

    var observation: NSKeyValueObservation?
    override func viewDidLoad() {
        super.viewDidLoad()
        observation = imageView.observe(\.isAnimating) { [weak self] imageView, change in
            if let value = change.newValue,
               value == true {
                imageView.removeFromSuperview()
                self?.observation = nil
            }
        }
    }

关于swift - ios - Swift 4 : How to remove UIImageViews when UIImageView. 动画图像完成了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53576633/

相关文章:

ios - Sirikit - Siri 有没有办法让我们自定义对讲

swift - 以编程方式添加 View 和手势 - "unrecognized selector sent to instance"

ios - Alamofire 快速接收并解析字符串数组

ios - 无法传递对指定了 self 的协议(protocol)的引用

ios - Swift - 如何连接 UIPickerView 中的 didSelectRow 选择

ios - 使用 switch 语句创建泛型函数,该函数根据传入参数的类型执行不同的代码

带有 Swift 3 和 pod 的 iOS 构建大小

Swift - 正则表达式与组件/连接 : which is more efficient for String operations

ios - 一些 uitableviewcell 在我滚动 iPhone 之前不会显示所有数据

ios - 协议(protocol)委托(delegate)未按预期传递数据 [Swift5]