[英]UITextView in collectionview cell on mac spikes cpu
我有一個簡單的 UIKit 應用程序,它在UICollectionViewCell
中有一個UITextView
。 該應用程序專為 iOS/iPadOS 設計,在這些平台上運行良好。 但是,當我開始滾動集合視圖時在 Mac(專為 iPad 設計)上運行時,CPU 使用率飆升至 ~85% 並無限期地保持在那里。 降低 cpu 的唯一方法是在應用程序 window 之外單擊,但一旦它再次出現在前台,cpu 使用率就會立即跳回。 我也試過在 Mac 上以 Catalyst 模式運行,但同樣的問題發生在 cpu 使用率略低 (~45%) 的情況下。
此外,調試器不斷吐出[API] cannot add handler to 3 from 3 - dropping
。
有人對此有解釋或解決方案嗎?
我在 macOS Ventura 13.0 (22A380) 上使用 Xcode 版本 14.1 (14B47b)。
class ViewController: UIViewController {
var dataSource: UICollectionViewDiffableDataSource<Section, String>! = nil
var collectionView: UICollectionView! = nil
var items = Array(0...100).map{"Item \($0)"}
enum Section: String {
case main
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "List"
configureCollectionView()
configureDataSource()
applyInitialSnapshot()
}
private func createLayout() -> UICollectionViewLayout {
return UICollectionViewCompositionalLayout { sectionIndex, layoutEnvironment in
let size = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .estimated(100))
let item = NSCollectionLayoutItem(layoutSize: size)
let group = NSCollectionLayoutGroup.horizontal(layoutSize: size, subitems: [item])
return NSCollectionLayoutSection(group: group)
}
}
private func configureCollectionView() {
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: createLayout())
collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
collectionView.backgroundColor = .systemBackground
view.addSubview(collectionView)
}
private func configureDataSource() {
let cellRegistration = UICollectionView.CellRegistration<TestCell, String> { (cell, indexPath, item) in
cell.configure(title: item, row: indexPath.item)
}
dataSource = UICollectionViewDiffableDataSource<Section, String>(collectionView: collectionView) {
(collectionView, indexPath, identifier) -> UICollectionViewCell? in
return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: identifier)
}
}
private func applyInitialSnapshot() {
var snapshot = NSDiffableDataSourceSnapshot<Section, String>()
snapshot.appendSections([.main])
snapshot.appendItems(items)
dataSource.apply(snapshot, animatingDifferences: false)
}
}
class TestCell: UICollectionViewCell {
private let annotationsTextView = UITextView()
override init(frame: CGRect) {
super.init(frame: frame)
addViews()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func configure(title: String, row: Int) {
annotationsTextView.attributedText = .init(string: "Row: \(row) Item: \(title)", attributes: [.font: UIFont.preferredFont(forTextStyle: .title1)])
}
private func addViews() {
annotationsTextView.isScrollEnabled = false
annotationsTextView.isEditable = false
annotationsTextView.translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(annotationsTextView)
NSLayoutConstraint.activate([
annotationsTextView.topAnchor.constraint(equalTo: contentView.topAnchor),
annotationsTextView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
annotationsTextView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
annotationsTextView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
])
}
}
每當視圖層次結構中的任何位置有一個 UITextView 且 isSelectable 或 isEditable 設置為 true 時,我都會在 Ventura 中遇到這種確切的行為,但前提是 UITextView 不是第一響應者。 無論 UITextView 是可見還是隱藏,都會發生這種情況。
可以通過以下任一方式防止 CPU 使用率過高:
我仍在調查中,如果發現更多,我會在這里更新。 我們可能都應該向 Apple 報告這個問題,因為我想他們需要在系統級別解決這個問題。
2022-12-01:更新
從 macOS 13.1 beta 4 (22C5059b) 開始,此問題似乎已得到解決。 哈利路亞!
我仍在使用 13.0.1 並遇到了同樣的問題(大量 [API] 無法將處理程序從 3 添加到 3 - 丟棄)。 能夠將其縮小到調用 UITableView.scrollToNearestSelectedRow 並使用 animated = true。
將 animated 設置為 false 會停止記錄。 我想我得去看看 13.1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.