[英]UITableView Scroll performing animation high CPU Usage
我有一个UITableView
在滚动上执行动画,基本上是一个粘性标头,但它调整了UIView
的高度约束。 我在滚动中获得了很高的CPU使用率。 有没有更好的方法来执行此动画?
编辑:我在cellForRow中没有做任何会导致此问题的函数。 绝对是scrollViewDidScroll函数。
DidScroll:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let offset = scrollView.contentOffset.y + tableViewContentInsets
if (lastPoint != nil) {
if (lastPoint! < offset) {
if (portfolioSummaryInitialHeight - offset < portfolioSummaryInitialHeight){
if (portfolioSummaryInitialHeight - offset > 0 ){
portfolioSummaryHeightConstraint.constant = portfolioSummaryInitialHeight - offset
} else {
portfolioSummaryHeightConstraint.constant = 0
}
}
} else {
if (portfolioSummaryInitialHeight - offset < portfolioSummaryInitialHeight){
if (portfolioSummaryInitialHeight - offset > 0 ){
portfolioSummaryHeightConstraint.constant = portfolioSummaryInitialHeight - offset
}
} else {
portfolioSummaryHeightConstraint.constant = portfolioSummaryInitialHeight
}
}
}
lastPoint = offset
}
其他变量:
var tableViewContentInsets: CGFloat = 80
var portfolioSummaryInitialHeight: CGFloat = 0 // equals portfolioSummaryInitialHeight at viewDidLoad
var lastPoint: CGFloat?
首先,我建议您检查是否在主线程上执行了其他繁重的操作,例如数据获取或类似操作。 我创建了一个具有纯表视图和标头的简单测试项目。 这是执行调整大小标头的代码。 根据CPU报告,在高峰时它使用8%的CPU。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var headerHeightConstr: NSLayoutConstraint! //header's height constraint
@IBOutlet weak var tableView: UITableView!
private var headerHeight: CGFloat = 128.0 ////header's height constraint initial value
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
}
extension ViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "reuseID") else {
return UITableViewCell(style: .default, reuseIdentifier: "reuseID")
}
return cell
}
}
extension ViewController: UITableViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.y < 0 {
self.headerHeightConstr.constant += abs(scrollView.contentOffset.y)
} else if scrollView.contentOffset.y > 0 && self.headerHeightConstr.constant >= headerHeight {
self.headerHeightConstr.constant -= scrollView.contentOffset.y
if self.headerHeightConstr.constant < headerHeight {
self.headerHeightConstr.constant = headerHeight
}
}
}
}
这是一种幼稚的方法,但是可以,如果需要,将尝试回答您的问题。
检查您的WillLayoutSubViews和DidLayoutSubViews函数。 如果您要在这些函数中进行大量工作,则需要添加以下内容:
var isScrolling: Bool = false
override func viewDidLayoutSubviews() {
if (isScrolling) {
return
} else {
super.viewDidLayoutSubviews()
}
}
当您的tableView正在滚动集isScrolling = true
在scrollViewDidEndDragging
设置isScrolling = false
,这将保证,虽然是的tableView滚动类不会做过多的工作,但保证会做的工作在其他情况下。 可能不是执行此操作的最佳方法,但它确实有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.