仅在swift中滚动时栅格化tableview的单元格

时间:2015-03-18 00:51:20

标签: uitableview swift

我有一个具有漂亮图形的桌面视图(圆角,使用clearcolor()等的透明单元格。)我也使用maskToBounds,因此滚动平滑的唯一方法是设置layer.shouldRasterize = true。 这很好但是当我删除一个单元格或者拖动一个单元格进行移动和重新排列时,我看到“工件”主要是单元格在我的tableview中一瞬间失去透明度,因为单元格正在从栅格化中更新它的内容

我只想在滚动它时光栅化tableviewcells,所以我尝试了各种方法,包括一个非常脏的方法,一旦tableView.contentOffset.y改变然后将其延迟一秒,我就栅格化为true模拟滚动并将其设置为假所需的时间。

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
}

有人能指点我这么干净吗? 谢谢。

1 个答案:

答案 0 :(得分:4)

我会拥有属性

weak var tableView : UITableView!
var isScrolling = false

当UITableViewDelegate协议扩展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
}
相关问题