如果不使用定时器概念,我不知道如何停止这个调用。今天,在尝试分析项目时,我发现由于以下功能每次分配内存都会增加:
func startAnimation(index: Int) {
UIView.animate(withDuration: 4.0, delay: 0.5, options:[UIViewAnimationOptions.allowUserInteraction, UIViewAnimationOptions.curveEaseInOut], animations: {
self.view.backgroundColor = self.colors[index]
}) { (finished) in
var currentIndex = index + 1
if currentIndex == self.colors.count { currentIndex = 0 }
self.startAnimation(index: currentIndex)
}
}
最佳答案
做一件简单的事,提一个flag,
最初它应该,
var isViewDissappear = false
然后,
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
isViewDissappear = true
}
然后再次调用时检查该标志,
func startAnimation(index: Int) {
UIView.animate(withDuration: 4.0, delay: 0.5, options:[UIViewAnimationOptions.allowUserInteraction, UIViewAnimationOptions.curveEaseInOut], animations: {
self.view.backgroundColor = self.colors[index]
}) { (finished) in
var currentIndex = index + 1
if currentIndex == self.colors.count { currentIndex = 0 }
if !self.isViewDissappear {
self.startAnimation(index: currentIndex)
}
}
}
就是这样。
关于ios - 如何在转到另一个 View Controller 时停止后台调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43363442/