wonday / react-native-pdf

A <Pdf /> component for react-native
MIT License
1.58k stars 544 forks source link

Scrolling PDF is laggy on android but smooth on ios #638

Open DebNeUIbm opened 2 years ago

DebNeUIbm commented 2 years ago

What react-native version are you using? 0.65.1

What react-native-pdf version are you using? 6.4.0

What platform does your issue occur on? (android/ios/both) android

Describe your issue as precisely as possible : 1) Steps to reproduce the issue or to explain in which case you get the issue

         <View style={{ flex: 1 }}>
            <ScrollView contentContainerStyle={{ flex: 1 }}>
              <Pdf
                source={{
                  uri,
                  cache: true,
                }}
                style={{ flex: 1 }}
                onError={(error) => { console.error(error); }}
              />
            </ScrollView>
          </View>
   Using the react-native-pdf package like above, tried with and without the surrounding View tag, but in android scrolling 
   behaviour is still choppy.
JeffreyVanelderenACA commented 2 years ago

I've found a workararound, see: https://github.com/wonday/react-native-pdf/issues/448#issuecomment-1117216273

KarlyLamm commented 1 year ago

Adding a scrollView solves the scrolling issue, but then you lose the ability to pinch & zoom. Issue is still a problem

Niltonsf commented 1 year ago

Any fixes for this, the pinch & zoom seems to be a problem. It seems to pinch on steps on android, iphone works correctly

LyDawei commented 1 year ago

Any luck on this?

Niltonsf commented 1 year ago

Unfortunately not

Fagner3g commented 1 year ago

Any luck on this?

rada commented 1 month ago

I was able to get the scroll working on Android using the setNativeProps. Although you sometimes need to touch twice, it's the closest way how I was able to get it working smoothly on Android.

export function useDisableNativePropsScroll<ScrollView>(): {
  enableNativePropsScroll: () => void;
  disableNativePropsScroll: () => void;
  scrollViewRef: React.RefObject<ScrollView>;
} {
  const scrollViewRef = useRef<ScrollView>(null);

  const disableNativePropsScroll = useCallback(() => {
    // @ts-expect-error for SrollView ref as null
    scrollViewRef.current?.setNativeProps({ scrollEnabled: false });
  }, [scrollViewRef]);

  const enableNativePropsScroll = useCallback(() => {
    // @ts-expect-error for SrollView ref as null
    scrollViewRef.current?.setNativeProps({ scrollEnabled: true });
  }, [scrollViewRef]);

  return { scrollViewRef, disableNativePropsScroll, enableNativePropsScroll };
}

And then use with the PDF component:

const { scrollViewRef, disableNativePropsScroll, enableNativePropsScroll } =
    useDisableNativePropsScroll<ScrollView>();

...
return (
  <ScrollView ref={scrollViewRef}>
    <View onTouchStart={disableNativePropsScroll} onTouchEnd={enableNativePropsScroll}>
       <Pdf
          source={{ uri: 'URL_to_some.pdf' }}
          style={{ height: 300 }}
          trustAllCerts={false}
       />
    </View>
  </ScrollView>
}