所以我要建立自己的拖放系统。为此,我需要一种方法来创建collectionViews
中的单元格之间的“间隙”,用户将鼠标悬停在其中。
我现在正在尝试一些东西,并使用可移动单元格的自定义流程布局进行了基本演示。
我所做的演示包括一个简单的collectionView
(使用IGListKit
,但这对这个问题无关紧要,我敢肯定)和一个UIPanGestureRecognizer
可以让您在collectionView上平移,以在手指下方创建一个间隙。
我通过每次pan gesture reconizer
更改时使布局无效来实现此目的。它是这样工作的,但是当我在滚动collectionView
的同时滚动时,单元格似乎出现了一些小故障。看起来像这样(看起来单元格的渲染跟不上):
我很确定问题出在包含此调用的makeAGap
函数之内:
collectionView?.performBatchUpdates({
self.invalidateLayout()
self.collectionView?.layoutIfNeeded()
}, completion: nil)
如果我不为无效动画,
self.invalidateLayout()
self.collectionView?.layoutIfNeeded()
完全不出现故障。它与动画有关。你有什么想法?
谢谢
PS:这是代码(还有更多IGListKit
东西,但这并不重要):
class MyCustomLayout: UICollectionViewFlowLayout {
fileprivate var cellPadding: CGFloat = 6
fileprivate var cache = [UICollectionViewLayoutAttributes]()
fileprivate var contentHeight: CGFloat = 300
fileprivate var contentWidth: CGFloat = 0
var gap: IndexPath? = nil
var gapPosition: CGPoint? = nil
override var collectionViewContentSize: CGSize {
return CGSize(width: contentWidth, height: contentHeight)
}
override func prepare() {
// cache contains the cached layout attributes
guard cache.isEmpty, let collectionView = collectionView else {
return
}
// I'm using IGListKit, so every cell is in its own section in my case
for section in 0..<collectionView.numberOfSections {
let indexPath = IndexPath(item: 0, section: section)
// If a gap has been set, just make the current offset (contentWidth) bigger to
// simulate a "missing" item, which creates a gap
if let gapPosition = self.gapPosition {
if gapPosition.x >= (contentWidth - 100) && gapPosition.x < (contentWidth + 100) {
contentWidth += 100
}
}
// contentWidth is used as x origin
let frame = CGRect(x: contentWidth, y: 10, width: 100, height: contentHeight)
contentWidth += frame.width + cellPadding
let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
attributes.frame = frame
cache.append(attributes)
}
}
public func makeAGap(at indexPath: IndexPath, position: CGPoint) {
gap = indexPath
self.cache = []
self.contentWidth = 0
self.gapPosition = position
collectionView?.performBatchUpdates({
self.invalidateLayout()
self.collectionView?.layoutIfNeeded()
}, completion: nil)
//invalidateLayout() // Using this, the glitch does NOT appear
}
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
var visibleLayoutAttributes = [UICollectionViewLayoutAttributes]()
// Loop through the cache and look for items in the rect
for attributes in cache {
if attributes.frame.intersects(rect) {
visibleLayoutAttributes.append(attributes)
}
}
return visibleLayoutAttributes
}
override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
return cache[indexPath.item]
}
}
class ViewController: UIViewController {
/// IGListKit stuff: Data for self.collectionView ("its cells", which represent the rows)
public var data: [String] {
return (0...100).compactMap { "\($0)" }
}
/// This collectionView will consist of cells, that each have their own collectionView.
private lazy var collectionView: UICollectionView = {
let layout = MyCustomLayout()
layout.scrollDirection = .horizontal
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
collectionView.backgroundColor = UIColor(hex: 0xeeeeee)
adapter.collectionView = collectionView
adapter.dataSource = self
view.addSubview(collectionView)
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.frame = CGRect(x: 0, y: 50, width: 1000, height: 300)
return collectionView
}()
/// IGListKit stuff. Data manager for the collectionView
private lazy var adapter: ListAdapter = {
let adapter = ListAdapter(updater: ListAdapterUpdater(), viewController: self, workingRangeSize: 0)
return adapter
}()
override func viewDidLoad() {
super.viewDidLoad()
_ = collectionView
let pan = UIPanGestureRecognizer(target: self, action: #selector(handlePan(pan:)))
pan.maximumNumberOfTouches = 1
pan.delegate = self
view.addGestureRecognizer(pan)
}
@objc private func handlePan(pan: UIPanGestureRecognizer) {
guard let indexPath = collectionView.indexPathForItem(at: pan.location(in: collectionView)) else {
return
}
(collectionView.collectionViewLayout as? MyCustomLayout)?.makeAGap(at: indexPath, position: pan.location(in: collectionView))
}
}
extension ViewController: ListAdapterDataSource, UIGestureRecognizerDelegate {
func objects(for listAdapter: ListAdapter) -> [ListDiffable] {
return data as [ListDiffable]
}
func listAdapter(_ listAdapter: ListAdapter, sectionControllerFor object: Any) -> ListSectionController {
return RowListSection()
}
func emptyView(for listAdapter: ListAdapter) -> UIView? {
return nil
}
func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
return true
}
}
答案 0 :(得分:0)
好的,这一直在发生。我花了几个小时试图解决问题,放弃了在这里问一个问题,几分钟后我找到了解决方案。
因此,我无法完全解释为什么会发生这种故障,但似乎与屏幕绘制有关。
我添加了CADisplayLink
,以使布局无效与屏幕的刷新率同步,现在故障已消失(下面的代码段供感兴趣的用户使用)。
但是,我很想知道那里到底发生了什么,以及为什么同步失效可以解决图形故障。我也要研究它,但是我不那么有经验,所以如果任何人都知道像这样的东西,我非常感谢这个问题的新答案(更详细):)
override func viewDidLoad() {
super.viewDidLoad()
_ = collectionView
let pan = UIPanGestureRecognizer(target: self, action: #selector(handlePan(pan:)))
pan.maximumNumberOfTouches = 1
pan.delegate = self
view.addGestureRecognizer(pan)
let displayLink = CADisplayLink(target: self, selector: #selector(update))
displayLink.add(to: .current, forMode: .common)
}
var indexPath: IndexPath? = nil
var position: CGPoint? = nil
@objc private func update() {
if let indexPath = self.indexPath, let position = self.position {
(collectionView.collectionViewLayout as? MyCustomLayout)?.makeAGap(at: indexPath, position: position)
}
}
@objc private func handlePan(pan: UIPanGestureRecognizer) {
guard let indexPath = collectionView.indexPathForItem(at: pan.location(in: collectionView)) else {
return
}
// buffer the values
self.indexPath = indexPath
position = pan.location(in: collectionView)
}