展开时如何保持UICollectionviewcell的高度?

时间:2018-12-05 23:33:10

标签: swift uicollectionviewcell

我的UIViewController类中有一个集合视图,并使用布尔数组来查看(refer to this answer)扩展了哪些索引。

var isExpanded = [Bool]()

因此,当我单击按钮时,我可以用以下方式重新加载单元格:

@objc func topButtonTouched(indexPath: IndexPath) {
    print(indexPath)
    isExpanded[indexPath.row] = !isExpanded[indexPath.row]
    UIView.animate(withDuration: 0.6, delay: 0.0, usingSpringWithDamping: 0.6, initialSpringVelocity: 1.0, options: UIView.AnimationOptions.transitionCurlUp, animations: {
        self.listCollectionView.reloadItems(at: [indexPath])
    }, completion: { success in
        print("success")
    })
}

到目前为止,一切都很好。但是,重新排序单元格时出现我遇到的问题。我没有使用beginInteractiveMovementForItem(),updateInteractiveMovementTargetPosition()等重新排序。

我开始使用下面的函数(from this repository)来创建缩放动画(单元格快照的动画),认为它也会对单元格重新排序:

@objc func longPressRecognized(_ recognizer: UILongPressGestureRecognizer) {
    let location = recognizer.location(in: collectionView)
    let indexPath = collectionView.indexPathForItem(at: location)

    switch recognizer.state {
    case UIGestureRecognizer.State.began:
        guard let indexPath = indexPath else { return }

        let cell = cellForRow(at: indexPath)
        snapshotView = cell.snapshotView(afterScreenUpdates: true)
        collectionView.addSubview(snapshotView!)
        cell.contentView.alpha = 0.0

        UIView.animate(withDuration: 0.2, animations: {
            self.snapshotView?.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
            self.snapshotView?.alpha = 0.9
        })
        snapshotPanPoint = location
        snapshotIndexPath = indexPath
    case UIGestureRecognizer.State.changed:
        guard let snapshotPanPoint = snapshotPanPoint else { return }

        let translation = CGPoint(x: location.x - snapshotPanPoint.x, y: location.y - snapshotPanPoint.y)
        snapshotView?.center.y += translation.y
        self.snapshotPanPoint = location

        guard let indexPath = indexPath else { return }
        collectionView.moveItem(at: snapshotIndexPath!, to: indexPath)
        snapshotIndexPath = indexPath
    default:
        guard let snapshotIndexPath = snapshotIndexPath else { return }
        let cell = cellForRow(at: snapshotIndexPath)
        UIView.animate(
            withDuration: 0.2,
            animations: {
                self.snapshotView?.center = cell.center
                self.snapshotView?.transform = CGAffineTransform.identity
                self.snapshotView?.alpha = 1.0
        },
            completion: { finished in
                cell.contentView.alpha = 1.0
                self.snapshotView?.removeFromSuperview()
                self.snapshotView = nil
        })
        self.snapshotIndexPath = nil
        self.snapshotPanPoint = nil
    }
}

我尝试在开关的.changed部分更改isExpanded数组,但是没有运气。我也尝试过这个answer

我的问题的答案

Gif Screen Shot

编辑: 在我有一个单一的布尔数组之前,就像这样:

@objc func topButtonTouched(indexPath: IndexPath) {
isExpanded[indexPath.item] = !isExpanded[indexPath.item]

现在使用对象的主要数组,我正在尝试这样做:

@objc func topButtonTouched(indexPath: IndexPath) {

    var names = mainData.map({ $0.isExpanded })
    names[indexPath.row] = !names[indexPath.row]

,然后更新索引路径。

1 个答案:

答案 0 :(得分:0)

通常,在计划更改Windows Server 2003的单个UITableView 中使用多个数据源会带来不幸。项目顺序-只是自杀:)

因此,我建议您将数据源从两个数组-[String][Bool]更改为单个[ListItem]-

(这里我使用的是class,但您可能使用的是struct,请记住,是通过 reference 传递的, 结构通过)传递

class ListItem{
    var title: String
    var isExpended: Bool

    init(title: String, isExpended: Bool){
        self.title = title
        self.isExpended = isExpended
    }
}

然后,当用户完成重新排序时,您需要在列表上进行这些更改-删除并将元素插入到另一个索引中即可完成工作。