Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fire event when scrolling past? #711

Open
bobbyziom opened this issue Oct 26, 2024 · 1 comment
Open

Fire event when scrolling past? #711

bobbyziom opened this issue Oct 26, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@bobbyziom
Copy link

Working with the circlular carousel, and I'm missing one feature

Wondering if there is a way to fire an event (e.g. haptic) when an item is passing by the center.

I've tried both onScrollEnd and ProgressChange trying to convert the absolute valute to index and then firing. But when scrolling faster it seems to not be possible.

Basically just the implementation from examples im playing with atm.

<Carousel
                  ref={ref}
                  width={itemSize}
                  height={itemSize}
                  style={{
                    width: UI_CONSTANTS.screenWidth,
                    height: UI_CONSTANTS.screenWidth / 2,
                  }}
                  onProgressChange={onProgressChange}
                  onScrollEnd={onSnapToItem}
                  snapEnabled
                  pagingEnabled={false}
                  overscrollEnabled={false}
                  scrollAnimationDuration={200}
                  data={data || []}
                  renderItem={({ item, index }) => (
                    <Pressable
                      key={item._id}
                      onPress={() => onItemPress(index)}
                      style={{ flex: 1 }}
                    >
                      <View
                        style={{
                          backgroundColor: 'black',
                          flex: 1,
                          borderRadius: 100,
                          justifyContent: 'center',
                          overflow: 'hidden',
                          alignItems: 'center',
                          borderWidth: 2,
                          borderColor: palette.primary[500],
                        }}
                      >
                        <View style={{ width: '100%', height: '100%' }}>
                          <SlideItem index={index} source={{ uri: item.imageLogo }} />
                        </View>
                      </View>
                    </Pressable>
                  )}
                  customAnimation={animationStyle}
                />
@dosubot dosubot bot added the enhancement New feature or request label Oct 26, 2024
@DanielProode
Copy link

I solved it with useAnimatedReaction.

const scrollPosition = useSharedValue(0);
let centeredElement: number | undefined;

const onCenterItemChange = (currentIndex: any) => {
  if (centeredElement != currentIndex) {
    Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Soft)
    centeredElement = currentIndex;
     }};

useAnimatedReaction(
    () => Math.round(scrollPosition.value),
    (currentIndex) => {
        runOnJS(onCenterItemChange)(currentIndex);
    },
    [scrollPosition] );
  
  <Carousel
  ...
  onProgressChange={(_, absoluteProgress) => {
     scrollPosition.value = absoluteProgress;
      }}>
  ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants