在 ScollView 下触摸/交互
我在垂直 ScrollView 下方有一个图像轮播。我希望能够在图像轮播顶部滑动一些内容。您可以看到我当前的实现方式存在问题。我无法再与我的图像轮播交互。
我希望能够在轮播顶部滚动内容,但是当顶部没有内容时,我希望能够像平常一样使用我的轮播。如果可能的话, 我还希望能够使滚动不弹回到开始的位置,而是停留在原来的位置。如果我将内容滚动到屏幕顶部,它需要保留在屏幕顶部。
我附加了一些关于如何渲染滚动视图和轮播的代码,但每一行代码都可以在我的snack示例 此处。它准确地再现了我的问题。
export default function App() {
return (
<View style={{backgroundColor: '#d1cfcf', flex: 1}}>
<View style={{position: 'absolute',}}>
<Carousel/>
</View>
{/* beginning of white box below image*/}
<ScrollView style={{height: scale(500)}}>
<View style={styles.whiteBox}>
<View style={{flexDirection: 'row', top: scale(15), left: scale(20)}}></View>
{/* grey body in white text box*/}
<View style={styles.greyBody}></View>
</View>
{/* end of white box below image*/}
{/* grey body in white text box*/}
{/* beginning of description*/}
<View style={{bottom: scale(340),left: scale(25), position: "absolute", backgroundColor: 'red'}}>
<Text style={{fontSize: 15, color: 'black', fontWeight: 'bold'}}>
Description
</Text>
</View>
{/* beginning of View more text*/}
<View style={{position: 'absolute', top: scale(400), left: scale(25), height: scale(100), width: scale(300),}}>
<Text>
Lorem ipsum dolor sit amet, in quo dolorum ponderum, nam veri molestie constituto eu. Eum enim tantas sadipscing ne, ut omnes malorum nostrum cum. Errem populo qui ne, ea ipsum antiopam definitionem eos. Lorem ipsum dolor sit amet,
</Text>
</View>
</ScrollView>
{/* End of View more text*/}
{/* End of description*/}
</View>
);
}
I have a image carousel underneath a vertical ScrollView. I want to be able to slide some content on top of my image carousel. You can see in the current way I am implementing it there is an issue. I can no longer interact with my image carousel.
I would like to be able to scroll content on top of my carousel, but when there is not content on top, I would like to be able to use my carousel like normal. If possible,
I would also like to be able for the scrolling to not bounce back to where it started, but to stay where it is left. If I scroll my content to the top of the screen, it needs to stay at the top of the screen.
I have attached some code as to how I am rendering the scrollView and the carousel but every line of code can be found on my snack example here. It reproduces my issue exactly.
export default function App() {
return (
<View style={{backgroundColor: '#d1cfcf', flex: 1}}>
<View style={{position: 'absolute',}}>
<Carousel/>
</View>
{/* beginning of white box below image*/}
<ScrollView style={{height: scale(500)}}>
<View style={styles.whiteBox}>
<View style={{flexDirection: 'row', top: scale(15), left: scale(20)}}></View>
{/* grey body in white text box*/}
<View style={styles.greyBody}></View>
</View>
{/* end of white box below image*/}
{/* grey body in white text box*/}
{/* beginning of description*/}
<View style={{bottom: scale(340),left: scale(25), position: "absolute", backgroundColor: 'red'}}>
<Text style={{fontSize: 15, color: 'black', fontWeight: 'bold'}}>
Description
</Text>
</View>
{/* beginning of View more text*/}
<View style={{position: 'absolute', top: scale(400), left: scale(25), height: scale(100), width: scale(300),}}>
<Text>
Lorem ipsum dolor sit amet, in quo dolorum ponderum, nam veri molestie constituto eu. Eum enim tantas sadipscing ne, ut omnes malorum nostrum cum. Errem populo qui ne, ea ipsum antiopam definitionem eos. Lorem ipsum dolor sit amet,
</Text>
</View>
</ScrollView>
{/* End of View more text*/}
{/* End of description*/}
</View>
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您对两个视图都使用绝对定位,即
Carousel
及其下方的ScrollView
的绝对位置。我们可以通过在App
中的ScrollView
添加zIndex: 2
并设置backgroundColor: "red"
来调试该问题> 这使我们能够在这里看到问题:App
中的ScrollView
实际上位于Carousel
的顶部 。这可能是有意为之,因为我们想要在“Carousel”上滚动。然而,它禁用了
触摸
的能力,例如滚动、Carousel
。通过更改以下内容可以轻松修复此问题:
App
中ScrollView
的{height:scale(500)}
,{marginTop: 250, Overflow: "visible"}
到App
中的ScrollView
。上边距删除了实际上位于
Carousel
顶部
的部分,并且overflow: "visible"
仍然允许我们滚动现在,在滚动
可以在Carousel
时,App
中的 >ScrollViewCarousel
上运行。请注意,文本现在已移至底部。我们可以通过
top:scale(400)
更改为top:scale(200)
来解决此问题。我使用您的初始代码制作了一个完全可用的 snack 。请注意,我仅在 iOS 上对此进行了测试。 Android 的行为可能有所不同。
由于我只更改了
App.js
,因此这里是App.js
的更新代码,它修复了该问题。You are using absolute positioning for both of your views, that is an absolute position for
Carousel
and theScrollView
below it. We can debug the issue by adding azIndex: 2
to theScrollView
inApp
and setbackgroundColor: "red"
which enables us to see the issue here:ScrollView
inApp
is in fact on top of theCarousel
.This might be intended since we want to scroll "over" the
Carousel
. However, it disables the ability totouch
, e.g. scroll, theCarousel
.This can be easily fixed by changing the following:
{height: scale(500)}
of theScrollView
inApp
,{marginTop: 250, overflow: "visible"}
to theScrollView
inApp
.The top margin removes the part which is actually
on top
of theCarousel
and theoverflow: "visible"
still allows us to scroll theScrollView
inApp
over theCarousel
while scrolling theCarousel
works now.Notice that the text has shifted to the bottom now. We can fix this by
top: scale(400)
of the view that contains your text totop: scale(200)
.I have made a fully working snack using your initial code. Notice that I have only tested this on iOS. Android might behave differently.
Since I have only changed
App.js
, here is the updated code ofApp.js
which fixes the issue.