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
是一个圆,有一个蓝绿色渐变的边界。
此外,我不确定这是否是XCode错误,或者是否与我的问题有关,但在Project Navigator的Instruments窗格中,当我运行应用程序并滚动laggy UITableView时,FPS并不反映滞后,即使我可以清楚地看出它非常滞后。事实上,窗格中的任何组件(CPU、内存、能量影响等)都没有明显差异。
更新:
layoutProfilePics(with:)
函数转换为
CustomAddFriendTableViewCell
prepareForReuse()
函数,我也试着
layoutProfilePics(带:)
layoutSubviews()
功能,但两者都没有改善滚动。