解决 – 分页 react-native-reanimated-carousel 和 react-native-animated-dots-carousel

huangapple go评论52阅读模式
英文:

RESOLVE - Pagination react-native-reanimated-carousel and react-native-animated-dots-carousel

问题

Here is the translated content:

我在使用react-native-reanimated-carousel时遇到了一点问题。我尝试使用react-native-animated-dots-carousel来创建分页,但当我滑动轮播时,分页只在轮播停止移动时才更新到新值。问题在于,如果我一次传递三张图像,分页会立刻从1跳到4,没有任何过渡,似乎存在时间延迟。

我该如何解决这个问题?以下是我的轮播代码:

const handleIndex = (index: number) => {
    setIndex(index)
}

<Carousel
 style={{borderRadius: 5}}
 pagingEnabled={true}
 loop={false}
 width={width}
 height={180}
 autoPlay={false}
 data={props.arrayImage}
 panGestureHandlerProps={{
  activeOffsetX: [-10, 10],
 }}
 onSnapToItem={(index) => handleIndex(index)}
 renderItem={({ item, ind }: any) => (
  <View>
   <View>
    <Image
     style={styles.imageFullSize}
     source={{
      uri: item
     }}
    />
   </View>
  </View>
 )}
/>
<View style={{position: 'absolute', bottom: 50, left: 20, width: '100%', height: 25, justifyContent: 'center', alignContent: 'center'}}>
<AnimatedDotsCarousel
 length={props.arrayImage.length}
 currentIndex={index}
 maxIndicators={props.arrayImage.length}
 interpolateOpacityAndColor={false}
 activeIndicatorConfig={{
  color: '#EC3C4C',
  margin: 3,
  opacity: 1,
  size: 8,
 }}
 inactiveIndicatorConfig={{
  color: '#F96B2B',
  margin: 3,
  opacity: 0.5,
  size: 8,
 }}
 decreasingDots={[
  {
   config: { color: '#F96B2B', margin: 3, opacity: 0.5, size: 6 },
   quantity: 1,
  },
  {
   config: { color: '#F96B2B', margin: 3, opacity: 0.5, size: 4 },
   quantity: 1,
  },
 ]}
/>
</View>

I've translated the code part for you.

英文:

I have a little problem with react-native-reanimated-carousel
I try to install a pagination with react-native-animated-dots-carousel but when I swipe my carousel, my pagination takes on my new value only when the carousel stops moving. The problem is that if I pass three images at once, the pagination goes from 1 to 4 without any transition and it seems that it has a time delay.

How can I solve this problem? Here is my carousel code:

const handleIndex = (index: number) =&gt; {
setIndex(index)
}
&lt;Carousel
style={{borderRadius: 5}}
pagingEnabled={true}
loop={false}
width={width}
height={180}
autoPlay={false}
data={props.arrayImage}
panGestureHandlerProps={{
activeOffsetX: [-10, 10],
}}
onSnapToItem={(index) =&gt; handleIndex(index)}
renderItem={({ item, ind }: any) =&gt; (
&lt;View&gt;
&lt;View&gt;
&lt;Image
style={styles.imageFullSize}
source={{
uri: item
}}
/&gt;
&lt;/View&gt;
&lt;/View&gt;
)}
/&gt;
&lt;View style={{position: &#39;absolute&#39;, bottom: 50, left: 20, width: &#39;100%&#39;, height: 25, justifyContent: &#39;center&#39;, alignContent: &#39;center&#39;}}&gt;
&lt;AnimatedDotsCarousel
length={props.arrayImage.length}
currentIndex={index}
maxIndicators={props.arrayImage.length}
interpolateOpacityAndColor={false}
activeIndicatorConfig={{
color: &#39;#EC3C4C&#39;,
margin: 3,
opacity: 1,
size: 8,
}}
inactiveIndicatorConfig={{
color: &#39;#F96B2B&#39;,
margin: 3,
opacity: 0.5,
size: 8,
}}
decreasingDots={[
{
config: { color: &#39;#F96B2B&#39;, margin: 3, opacity: 0.5, size: 6 },
quantity: 1,
},
{
config: { color: &#39;#F96B2B&#39;, margin: 3, opacity: 0.5, size: 4 },
quantity: 1,
},
]}
/&gt;
&lt;/View&gt;```
</details>
# 答案1
**得分**: 2
使用 Math.round 和 onProgressChange 属性代替 onSnapToItem 对我有帮助。在您的情况下,应该如下所示:
```jsx
<Carousel
...
onProgressChange={(_, absoluteProgress) => {
handleIndex(Math.round(absoluteProgress));
}}
/>
英文:

Using Math.round and onProgressChange prop instead of onSnapToItem did the trick for me. In your case it should look like this

&lt;Carousel
...
onProgressChange={(_, absoluteProgress) =&gt; {
handleIndex(Math.round(absoluteProgress));
}}
/&gt;

答案2

得分: -1

Could you provide a gif video of what is happening?

react-native-animated-dots-carousel doesn't have a way of knowing exactly when should execute the transition, detecting that moment in the proper way is up to you. You need to detect the event of the index changing faster, for example, if you implement a Carousel using a FlatList, you can use:

scrollEventThrottle

but since you are using react-native-reanimated-carousel, maybe you can try reducing the scrollAnimationDuration or play around with onProgressChange. Maybe that prop can do what you need.

I also think that it will look smoother if you use

interpolateOpacityAndColor={false}

on react-native-animated-dots-carousel.

I am the owner of react-native-animated-dots-carousel, so I would appreciate any suggestions! Have a good day.

英文:

Could you provide a gif video of what is happening?
react-native-animated-dots-carousel doesn't have a way of know exactly when should execute the transition, detect that moment on the proper way is up to you. You need to detect the event of the index changing faster, for example if you implement a Carousel using a FlatList you can use:

scrollEventThrottle

but since you are using react-native-reanimated-carousel maybe you can try by reducing the scrollAnimationDuration or play around with onProgressChange maybe that prop can do what you need.

I also think that it will look smother if you use

interpolateOpacityAndColor={false}

on react-native-animated-dots=carousel

I am the owner of react-native-animated-dots-carousel so I would appreciate any suggestion! Have a good day

huangapple
  • 本文由 发表于 2023年4月17日 21:14:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定