由于 CAShapeLayer (Swift 3),导致 UITableView 滚动延迟
Laggy UITableView scrolling because of CAShapeLayer (Swift 3)
我在 UIView 中有一个 UITableView,它由以下 class 定义的自定义单元格组成:
class CustomAddFriendTableViewCell: UITableViewCell {
@IBOutlet weak var UsernameLbl: UILabel!
@IBOutlet weak var ProfileImg: UIImageView!
@IBOutlet weak var AddFriendBtn: UIButton!
}
在 ViewController 的 tableview(_:cellForRowAt:)
方法中,我调用了以下函数来布局单元格的 ProfileImg:
private func layoutProfilePics(with cell:CustomAddFriendTableViewCell) {
//create gradient
let gradient = CAGradientLayer()
gradient.frame = CGRect(origin: CGPoint.zero, size: cell.ProfileImg.frame.size)
gradient.colors = [Colors.blueGreen.cgColor, Colors.yellow.cgColor]
//create gradient mask
let shape = CAShapeLayer()
shape.lineWidth = 3
shape.path = UIBezierPath(ovalIn: cell.ProfileImg.bounds).cgPath // commenting this out makes lag go away
shape.strokeColor = UIColor.black.cgColor // commenting this out makes lag go away
shape.fillColor = UIColor.clear.cgColor
gradient.mask = shape
cell.ProfileImg.layoutIfNeeded()
cell.ProfileImg.clipsToBounds = true
cell.ProfileImg.layer.masksToBounds = true
cell.ProfileImg.layer.cornerRadius = cell.ProfileImg.bounds.size.width/2.0
cell.ProfileImg.layer.addSublayer(gradient)
}
此代码使 ProfileImg
成为一个圆圈,并具有带有 blue-green 渐变的边框。
旁边带有注释的两行使滚动非常平滑(这意味着渐变不是导致滞后的原因),所以我假设渲染 CAShapeLayer(特别是笔划)是导致问题的原因(因此问题标题)。我可以做些什么来提高 tableview 的滚动性能?
此外,我不确定这是 XCode 错误还是与我的问题有关,但是在 Project Navigator 的 Instruments 窗格中,当我 运行 应用程序和滚动滞后的 UITableView,FPS 不会反映滞后,尽管我可以清楚地看出它非常滞后。事实上,窗格中的任何组件(CPU、内存、能源影响等)都没有明显差异。
更新:
我尝试将 layoutProfilePics(with:)
函数移动到 CustomAddFriendTableViewCell
的 prepareForReuse()
函数中,我还尝试将 layoutProfilePics(with:)
放入其 layoutSubviews()
函数中,但都没有其中改进了滚动。
该代码应为每个单元格调用一次,因此不应在 tableView(_:cellForRowAt:)
中调用。尝试将该代码移至 awakeFromNib()
,如果您为自定义单元格使用笔尖,请在 viewDidLoad()
中使用以下代码加载插座:
let addFriendCellNib = UINib(nibName: "CustomAddFriendTableViewCell", bundle: nil)
tableView.register(addFriendCellNib, forCellReuseIdentifier: "addFriendCell")
希望对你有用。
我在 UIView 中有一个 UITableView,它由以下 class 定义的自定义单元格组成:
class CustomAddFriendTableViewCell: UITableViewCell {
@IBOutlet weak var UsernameLbl: UILabel!
@IBOutlet weak var ProfileImg: UIImageView!
@IBOutlet weak var AddFriendBtn: UIButton!
}
在 ViewController 的 tableview(_:cellForRowAt:)
方法中,我调用了以下函数来布局单元格的 ProfileImg:
private func layoutProfilePics(with cell:CustomAddFriendTableViewCell) {
//create gradient
let gradient = CAGradientLayer()
gradient.frame = CGRect(origin: CGPoint.zero, size: cell.ProfileImg.frame.size)
gradient.colors = [Colors.blueGreen.cgColor, Colors.yellow.cgColor]
//create gradient mask
let shape = CAShapeLayer()
shape.lineWidth = 3
shape.path = UIBezierPath(ovalIn: cell.ProfileImg.bounds).cgPath // commenting this out makes lag go away
shape.strokeColor = UIColor.black.cgColor // commenting this out makes lag go away
shape.fillColor = UIColor.clear.cgColor
gradient.mask = shape
cell.ProfileImg.layoutIfNeeded()
cell.ProfileImg.clipsToBounds = true
cell.ProfileImg.layer.masksToBounds = true
cell.ProfileImg.layer.cornerRadius = cell.ProfileImg.bounds.size.width/2.0
cell.ProfileImg.layer.addSublayer(gradient)
}
此代码使 ProfileImg
成为一个圆圈,并具有带有 blue-green 渐变的边框。
旁边带有注释的两行使滚动非常平滑(这意味着渐变不是导致滞后的原因),所以我假设渲染 CAShapeLayer(特别是笔划)是导致问题的原因(因此问题标题)。我可以做些什么来提高 tableview 的滚动性能?
此外,我不确定这是 XCode 错误还是与我的问题有关,但是在 Project Navigator 的 Instruments 窗格中,当我 运行 应用程序和滚动滞后的 UITableView,FPS 不会反映滞后,尽管我可以清楚地看出它非常滞后。事实上,窗格中的任何组件(CPU、内存、能源影响等)都没有明显差异。
更新:
我尝试将 layoutProfilePics(with:)
函数移动到 CustomAddFriendTableViewCell
的 prepareForReuse()
函数中,我还尝试将 layoutProfilePics(with:)
放入其 layoutSubviews()
函数中,但都没有其中改进了滚动。
该代码应为每个单元格调用一次,因此不应在 tableView(_:cellForRowAt:)
中调用。尝试将该代码移至 awakeFromNib()
,如果您为自定义单元格使用笔尖,请在 viewDidLoad()
中使用以下代码加载插座:
let addFriendCellNib = UINib(nibName: "CustomAddFriendTableViewCell", bundle: nil)
tableView.register(addFriendCellNib, forCellReuseIdentifier: "addFriendCell")
希望对你有用。