如何在编辑时为我的集合视图单元设置较小的动画?

时间:2017-01-12 04:05:54

标签: ios iphone swift uicollectionview uicollectionviewcell

所以我有一个集合视图,每行放置2个单元格。当我进入编辑模式时,我希望细胞的大小减小。我尝试为它们添加填充以获得所需的效果,但是我需要它们来制作动画!我怎么能做到这一点。请看下面的代码:

加载视图时的初始设置:

 override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.leftBarButtonItem = editButtonItem
    navigationController?.isToolbarHidden = true

    // Does not allow to move items around innitialy
    installsStandardGestureForInteractiveMovement = false

    // set collectionView item size
    //let width = collectionView!.frame.width/2
    let layout = collectionViewLayout as! UICollectionViewFlowLayout
    let itemsPerRow: CGFloat = 2.0
    let paddingSpace: CGFloat = 2.0 * itemsPerRow
    let availableWidth = view.frame.width - paddingSpace
    let widthPerItem = availableWidth / itemsPerRow
    layout.minimumLineSpacing = 4.0
    layout.itemSize = CGSize(width: widthPerItem, height: widthPerItem)

}

然后这是在点击“编辑”时被调用的内容

func reduceCellSize() {

    if isEditing {
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout

        let itemsPerRow: CGFloat = 2.0
        let paddingSpace: CGFloat = 8.0 * itemsPerRow
        let availableWidth = self.view.frame.width - paddingSpace
        let widthPerItem = availableWidth / itemsPerRow
        layout.minimumLineSpacing = 6.0
        layout.sectionInset.left = 5
        layout.sectionInset.right = 5
        layout.itemSize = CGSize(width: widthPerItem, height: widthPerItem)
    } else {
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        let itemsPerRow: CGFloat = 2.0
        let paddingSpace: CGFloat = 2.0 * itemsPerRow
        let availableWidth = view.frame.width - paddingSpace
        let widthPerItem = availableWidth / itemsPerRow
        layout.minimumLineSpacing = 4.0
        layout.sectionInset.left = 0
        layout.sectionInset.right = 0
        layout.itemSize = CGSize(width: widthPerItem, height: widthPerItem)
    }
}

2 个答案:

答案 0 :(得分:0)

在reduceCellSize方法结束时尝试此操作:

connectString

(将“collectionView”替换为集合视图的实际名称。)

这应该会使集合视图为其子视图的大小调整设置动画。

答案 1 :(得分:0)

您需要的是CGAffineTransform。所以你可以这样设置它们的整体大小:

    private func transformCell() {
            var offset : Float = 0.0

            for cell in numberCollectionView.visibleCells as! [MyCell] {


                offset = 0.07
                cell.transform = CGAffineTransform.identity
                cell.transform = CGAffineTransform(scaleX: CGFloat(offset), y: CGFloat(offset))

            }
     }

它会根据偏移值将可见单元格转换为不同的大小,您可以从scrollViewDidScroll

调用它
func scrollViewDidScroll(_ scrollView: UIScrollView) {


            transformCell()

    }