如何用呈现的值构建与指数相对应的数学公式
How to construct a mathematical formula corresponding to the index with the presented values
我有这个功能可以在 Flatlist
(水平)中集中一个项目:
centralizeItem = (index) => {
const WIDTH = scale(95)
let centerSize
if (index === 0) centerSize = (WIDTH*1.5) * -1
if (index === 1) centerSize = (WIDTH/2) * -1
if (index === 2) centerSize = (WIDTH/2)
if (index === 3) centerSize = (WIDTH*(index/2))
if (index === 4) centerSize = (WIDTH*(index/2+0.5)) // 2.5
if (index === 5) centerSize = (WIDTH*(index/2+1)) // 3.5
if (index === 6) centerSize = (WIDTH*(index/2+1.5)) //4.5
return centerSize
}
此公式 return 使用
在 y 值中滚动的偏移值
this.refs.listRef.scrollToOffset({offset: this.centralizeItem(index) })
以上代码在项目被选中时触发
但是,Flatlist
渲染了很多数据,不仅仅是7个索引,这个公式是行不通的。
我需要一个遵循上述函数变化的公式
您可以使用公式计算因数。
centralizeItem = index => scale(95) * (index - 1.5);
我有这个功能可以在 Flatlist
(水平)中集中一个项目:
centralizeItem = (index) => {
const WIDTH = scale(95)
let centerSize
if (index === 0) centerSize = (WIDTH*1.5) * -1
if (index === 1) centerSize = (WIDTH/2) * -1
if (index === 2) centerSize = (WIDTH/2)
if (index === 3) centerSize = (WIDTH*(index/2))
if (index === 4) centerSize = (WIDTH*(index/2+0.5)) // 2.5
if (index === 5) centerSize = (WIDTH*(index/2+1)) // 3.5
if (index === 6) centerSize = (WIDTH*(index/2+1.5)) //4.5
return centerSize
}
此公式 return 使用
在 y 值中滚动的偏移值this.refs.listRef.scrollToOffset({offset: this.centralizeItem(index) })
以上代码在项目被选中时触发
但是,Flatlist
渲染了很多数据,不仅仅是7个索引,这个公式是行不通的。
我需要一个遵循上述函数变化的公式
您可以使用公式计算因数。
centralizeItem = index => scale(95) * (index - 1.5);