如何使用IGListKit在选择上为UICollectionViewCell制作动画

时间:2018-04-30 09:27:18

标签: swift uicollectionview iglistkit

我无法使用didHighlightItem和didUnhighlightItem函数在单击单元格时正确设置动画,因为我无法访问单元格的节号。我想知道我是否错误地解决了问题,或者是否有一种方法可以在集合视图中访问SectionController的section索引。我的代码如下:

class PersonSectionController: ListSectionController {
    var current: Person?

    override init() {}

    override func didUpdate(to object: Any) {
        if let person = object as? Person {
            current = person
        }
    }

    // Number of items displayed per object
    override func numberOfItems() -> Int {
        return 1
    }

    // Dequeue and configure cell
    override func cellForItem(at index: Int) -> UICollectionViewCell {}

    // Returns the required cell size at the given index
    override func sizeForItem(at index: Int) -> CGSize {}

    override func didHighlightItem(at index: Int) {
        if let viewController = self.viewController as? PeopleViewController {
            if let cell = viewController.peopleCollectionView.cellForItem(at: IndexPath(row: index, section: 0)) {
                UIView.animate(withDuration: 0.1) {
                    cell.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
                }
            }
        }
    }

    override func didUnhighlightItem(at index: Int) {
        if let viewController = self.viewController as? PeopleViewController {
            if let cell = viewController.peopleCollectionView.cellForItem(at: IndexPath(row: 0, section: index)) {
                UIView.animate(withDuration: 0.1) {
                    cell.transform = CGAffineTransform.identity
                }
            }
        }
    }
}

提前致谢!

1 个答案:

答案 0 :(得分:0)

我认为您需要从collectionContext访问单元格:

override func didHighlightItem(at index: Int) {
    guard let cell = collectionContext?.cellForItem(at: index) else { return }        
    // Animation stuff
}