ios - 如何在转到另一个 View Controller 时停止后台调用?

标签 ios memory-management memory-leaks swift3

如果不使用定时器概念,我不知道如何停止这个调用。今天,在尝试分析项目时,我发现由于以下功能每次分配内存都会增加:

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/

相关文章:

ios - 我可以在 PDF 创建过程中更新 UI 吗?

ios - Swift 将 Double 值分配给 malloc 的数组

iphone - 在 iPhone 上调试 applicationDidReceiveMemoryWarning 的最佳方法?

.net - 如何不删除其他对象引用的缓存项

c++ - **在C/C++中是什么意思?

iphone - Xcode 的 "build and analyze"报告此代码存在奇怪的内存泄漏

linux - 分析网络应用程序中的资源泄漏(套接字处理程序等)

ios - 试图从 UITableView 返回选定的单元格,但它只返回 nil

ios - 如何检测一个sknode悬停在另一个sknode上?

ios - 为什么在 viewDidUnload 里面有一个 release 语句?