ios – Swift 4: How to remove UIImageViews when UIImageView.animationImages is finished?

I am using .animationImages for UIImageViews in my project (it’s a game). When the user presses a hint button I programmatically create a variable number of UIImageViews that will appear on screen and play the same image sequence once. I need to remove the UIImageViews from the view when the animation has stopped but I can’t figure out how to do this.

I’ve been through the swift documentation for both .animationImages and .startAnimating but it’s literally one paragraph and one line, neither of which is in relation to a completion or finish notifications etc. Any help will be much appreciated.

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         }     } }