如何在Scroll上对UICollectionViewCell的大小进行动画处理,使中间的一个最大? [英] How Can I Animate the Size of A UICollectionViewCell on Scroll so the middle one is largest?

查看:161
本文介绍了如何在Scroll上对UICollectionViewCell的大小进行动画处理,使中间的一个最大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UICollectionView,它显示一个带有全角列的几行(看起来像一个UITableView)

I have a UICollectionView that shows several rows with one, full-width column (looks like a UITableView)

我想要实现的类似于

...其中中间单元格的高度要大得多。当用户上下滚动时,中间单元格之前和之后的单元格动画返回给定单元格的默认高度。

... where the middle cell has a much greater height. As the user scrolls up and down, the cells before and after the middle cell animate back to the default height for the given cell.

有人可以概述我应该如何处理此问题

Can somebody outline how I should approach this problem?

推荐答案

我在水平 Swift 3 代码中使用了 UICollectionView

I use this Swift 3 code in my horizontal UICollectionView.

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let centerX = scrollView.contentOffset.x + scrollView.frame.size.width/2
    for cell in mainCollectionView.visibleCells {

        var offsetX = centerX - cell.center.x
        if offsetX < 0 {
            offsetX *= -1
        }

        cell.transform = CGAffineTransform(scaleX: 1, y: 1)
        if offsetX > 50 {

            let offsetPercentage = (offsetX - 50) / view.bounds.width
            var scaleX = 1-offsetPercentage

            if scaleX < 0.8 {
                scaleX = 0.8
            }
            cell.transform = CGAffineTransform(scaleX: scaleX, y: scaleX)
        }
    }
}

这篇关于如何在Scroll上对UICollectionViewCell的大小进行动画处理,使中间的一个最大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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