旋转后如何重绘不可见的 UICollectionViewCell 以备重用时使用? [英] how to redraw non-visible UICollectionViewCell's after rotation ready for when reuse occurs?

查看:12
本文介绍了旋转后如何重绘不可见的 UICollectionViewCell 以备重用时使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何重绘不可见的 UICollectionViewCell 在重用发生时准备好???

How to redraw non-visible UICollectionViewCell's ready for when reuse occurs???

我想到的一种方法是根据 Layout Cell prepareForReuse 函数中的代码,但是虽然它不是最佳的,因为它会导致更多的重新绘制然后需要.

One approach I thought of was per the code in the Layout Cell prepareForReuse function, however whilst it works it non-optimal as it causes more re-drawing then required.

背景:需要在方向改变后触发 drawRect 对于当前不可见的单元格,但弹出以使用且尚未重绘,到目前为止我只能看到 prepareForReuse 是合适的.问题是我正在重新绘制所有重用"单元格,而我真的只想重新绘制那些在设备的前一个方向位置期间创建的最初弹出的单元格.

Background: Need to trigger drawRect for cells after an orientation change that are not current visible, but pop up to be used and haven't been redraw, so so far I can only see that prepareForReuse would be appropriate. Issue is I'm re-drawing all "reuse" cells, whereas I really only want to redraw those that initially pop up that were created during the previous orientation position of the device.

附加信息:所以目前我正在这样做:

ADDITIONAL INFO: So currently I'm doing this:

在视图控制器中:

override func viewWillLayoutSubviews() {
    // Clear cached layout attributes (to ensure new positions are calculated)
    (self.cal.collectionViewLayout as! GCCalendarLayout).resetCache()
    self.cal.collectionViewLayout.invalidateLayout()

    // Trigger cells to redraw themselves (to get new widths etc)
    for cell in self.cal?.visibleCells() as! [GCCalendarCell] {
        cell.setNeedsDisplay()
    }

    // Not sure how to "setNeedsDisplay" on non visible cells here?
}

在布局单元类中:

override func prepareForReuse() {
    super.prepareForReuse()
    // Ensure "drawRect" is called (only way I could see to handle change in orientation
    self.setNeedsDisplay() 
    // ISSUE: It does this also for subsequent "prepareForReuse" after all
    // non-visible cells have been re-used and re-drawn, so really
    // not optimal
}

没有上面 prepareForReuse 中的代码会发生什么的例子.方向改变后拍摄的快照,并在向上滚动一点后:

Example of what happens without the code in prepareForReuse above. Snapshot taken after an orientation change, and just after scrolling up a little bit:

推荐答案

我想我现在在这里:

import UIKit

@IBDesignable class GCCalendarCell: UICollectionViewCell {
    var prevBounds : CGRect?

    override func layoutSubviews() {
        if let prevBounds = prevBounds {
            if !( (prevBounds.width == bounds.width) && (prevBounds.height == bounds.height) ) {
                self.setNeedsDisplay()
            }
        }
    }

    override func drawRect(rect: CGRect) {
        // Do Stuff
        self.prevBounds = self.bounds
    }

}

注意到此检查在prepareForReuse"中不起作用,因为此时单元格尚未应用旋转.然而,似乎在layoutSubviews"中工作.

Noted this check didn't work in "prepareForReuse" as at this time the cell had not had the rotation applied. Seems to work in "layoutSubviews" however.

这篇关于旋转后如何重绘不可见的 UICollectionViewCell 以备重用时使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆