我有一个具有漂亮图形的表格 View (圆角、使用clearcolor()等的透明单元格)我还使用maskToBounds,因此使滚动平滑的唯一方法是设置layer.shouldRasterize = true。 这工作正常,但是当我删除一个单元格或拖动一个单元格来移动和重新排列时,我看到“伪像”,大多数单元格在我的表格 View 中瞬间失去透明度,因为单元格正在更新其来自光栅化的内容
我只想在滚动时栅格化 tableviewcells,所以我尝试了各种方法,包括一个非常脏的方法,其中一旦 tableView.contentOffset.y 发生变化,我就栅格化为 true,然后将其延迟一秒钟模拟滚动所需的时间并将其设置回 false。
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> TableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as TableViewCell
cell.textLabel?.text = cellitemcontent[indexPath.row]
//here I have all the custom cell design...
var currentOffset = tableView.contentOffset.y
cell.layer.shouldRasterize = true
if currentOffset > 0
{ cell.layer.shouldRasterize = true}
else{
delay(1.5){
cell.layer.shouldRasterize = false
}
}
return cell
}
有人能给我指出一种更干净的方法吗? 谢谢。
最佳答案
我会拥有这些属性
weak var tableView : UITableView!
var isScrolling = false
由于 UITableViewDelegate 协议(protocol)扩展了 UIScrollViewDelegate,我将实现
func visibleCellsShouldRasterize(aBool:Bool){
for cell in tableView.visibleCells() as [UITableViewCell]{
cell.layer.shouldRasterize = aBool;
}
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
isScrolling = false
self.visibleCellsShouldRasterize(isScrolling)
}
func scrollViewWillEndDragging(scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
if velocity == CGPointZero{
isScrolling = false
self.visibleCellsShouldRasterize(isScrolling)
}
}
func scrollViewWillBeginDragging(scrollView: UIScrollView) {
isScrolling = true
self.visibleCellsShouldRasterize(isScrolling)
}
要切换可见单元格和进入屏幕的单元格的shouldRasterize,我会实现
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
cell.layer.shouldRasterize = isScrolling
}
关于uitableview - 仅在快速滚动时栅格化表格 View 的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29112364/