ios - UITableView滚动执行动画CPU使用率高

标签 ios swift uiscrollview

我有一个 UITableView ,它在滚动时执行动画,基本上是一个粘性标题,但它调整 UIView 的高度约束。我在滚动时的 CPU 使用率非常高。有没有更好的方法来执行这个动画?

编辑:我没有在 cellForRow at 函数中执行任何可能导致此问题的操作。肯定是scrollViewDidScroll函数。

CPU usage

滚动:

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?

Dashboard

最佳答案

首先,我建议您检查是否在主线程上执行了任何其他繁重的操作,例如数据获取或类似的操作。我创建了一个带有简单表格 View 和标题的简单测试项目。这是调整标题大小的代码。根据 CPU 报告,它在高峰时使用了 8% 的 CPU。

CPU report

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

这是一种有点幼稚的方法,但它有效,如果需要,我们会尝试回答您的问题。

关于ios - UITableView滚动执行动画CPU使用率高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57885018/

相关文章:

iOS:使用滚动功能显示图像和长文本的正确方法

ios - UIScrollviews zoomToRect 仅设置 zoomScale 但不设置 contentOffset

iphone - 可以将 WiFi 设置从 iOS 设备传递到 ExternalAccessory 对象吗?

ios - Swift:尝试更改从另一个 swift 文件调用的函数中的对象属性时意外发现 "nil"

ios - 仅在 UITableView 中将分隔符添加到第一个单元格

swift - 在 UIView 内部设置 UITableView 的约束

objective-c - UIScrollView 的 subview 中的水平 UISwipeGestureRecognizer? (UIScrollView 只需要识别垂直滚动)

objective-c - iOS 中的 RTSP 流

ios - SWIFT 4 中 UITableViewCell 上的内存管理问题谷歌地图

ios - Swift 结果未通过 Storyboard 添加的 SearchBar 进行更新