gpt4 book ai didi

ios - 水平 ScrollView 捕捉 react native

转载 作者:IT王子 更新时间:2023-10-29 08:02:54 25 4
gpt4 key购买 nike

您好,我正在尝试实现 scrollview 居中对齐像下面的 gif 链接

Check This Gif

但是做不到。以下是我实现此目的的 native react 代码。

或者是否有任何方法可以滚动到像 android 这样的 scrollview 元素的特定索引?

如有任何帮助,我们将不胜感激。提前致谢

<ScrollView
style={[styles.imgContainer,{backgroundColor:colorBg,paddingLeft:20}]}
automaticallyAdjustInsets={false}
horizontal={true}
pagingEnabled={true}
scrollEnabled={true}
decelerationRate={0}
snapToAlignment='center'
snapToInterval={DEVICE_WIDTH-100}
scrollEventThrottle={16}
onScroll={(event) => {
var contentOffsetX=event.nativeEvent.contentOffset.x;
var contentOffsetY=event.nativeEvent.contentOffset.y;

var cellWidth = (DEVICE_WIDTH-100).toFixed(2);
var cellHeight=(DEVICE_HEIGHT-200).toFixed(2);

var cellIndex = Math.floor(contentOffsetX/ cellWidth);

// Round to the next cell if the scrolling will stop over halfway to the next cell.
if ((contentOffsetX- (Math.floor(contentOffsetX / cellWidth) * cellWidth)) > cellWidth) {
cellIndex++;
}

// Adjust stopping point to exact beginning of cell.
contentOffsetX = cellIndex * cellWidth;
contentOffsetY= cellIndex * cellHeight;

event.nativeEvent.contentOffsetX=contentOffsetX;
event.nativeEvent.contentOffsetY=contentOffsetY;

// this.setState({contentOffsetX:contentOffsetX,contentOffsetY:contentOffsetY});
console.log('cellIndex:'+cellIndex);

console.log("contentOffsetX:"+contentOffsetX);
// contentOffset={{x:this.state.contentOffsetX,y:0}}
}}
>
{rows}

</ScrollView>

最佳答案

您不需要其他库,您可以使用 ScrollView 做到这一点。您只需要在组件中添加以下 Prop 即可。

    horizontal= {true}
decelerationRate={0}
snapToInterval={200} //your element width
snapToAlignment={"center"}

查看此 snack 以了解有关如何实现它的更多详细信息 https://snack.expo.io/H1CnjIeDb

关于ios - 水平 ScrollView 捕捉 react native ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39849648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com