使用React-Native和React-Native PDF自动滚动PDF代码
我对React原生编程有点陌生,我正在为PDF实现autoscroll功能。例如,在某些情况下,我希望自动将PDF向下滚动x页,然后以所需的速度滚动。我遵循了本教程,它只适用于普通数据,但当我使用react native pdf中的对象时,它似乎不再滚动。我将PDF对象包装在一个ScrollView中,可以确认正在调用滚动代码。有人能提出一个解决方案或解释为什么这不适用于PDF吗?非常感谢 如果有帮助的话,我也在下面附上了我的代码。目前,PDF显示,但根本不是自动滚动使用React-Native和React-Native PDF自动滚动PDF代码,react-native,pdf,scrollview,React Native,Pdf,Scrollview,我对React原生编程有点陌生,我正在为PDF实现autoscroll功能。例如,在某些情况下,我希望自动将PDF向下滚动x页,然后以所需的速度滚动。我遵循了本教程,它只适用于普通数据,但当我使用react native pdf中的对象时,它似乎不再滚动。我将PDF对象包装在一个ScrollView中,可以确认正在调用滚动代码。有人能提出一个解决方案或解释为什么这不适用于PDF吗?非常感谢 如果有帮助的话,我也在下面附上了我的代码。目前,PDF显示,但根本不是自动滚动 import React
import React from 'react';
import {StyleSheet, Dimensions, View, ScrollView} from 'react-native';
import Pdf from 'react-native-pdf';
export default class PDFScroll extends React.Component {
constructor(props) {
super(props);
this.state = {
currentPosition: 0,
};
this.scrolling = this.scrolling.bind(this);
}
componentDidMount(){
this.activeInterval = setInterval(this.scrolling, 100);
}
componentWillUnmount(){
clearInterval(this.activeInterval);
}
scrolling() {
position = this.state.currentPosition + 50;
this.pdf.scrollTo({ x: position, animated: true });
// After position passes this value, snaps back to beginning
let maxOffset = 2000;
// Set animation to repeat at end of scroll
if (this.state.currentPosition > maxOffset) {
this.pdf.scrollTo({ x: 0, animated: false })
this.setState({ currentPosition: 0 });
}
else {
this.setState({ currentPosition: position });
}
}
render() {
const source = {
uri: 'http://samples.leanpub.com/thereactnativebook-sample.pdf',
cache: true,
};
return (
<View style={styles.container}>
<ScrollView
style={styles.scrollview}
horizontal={false}
bounces={true}
ref={(ref) => this.pdf = ref}
>
{
<Pdf
source={source}
onLoadComplete={(numberOfPages, filePath) => {
console.log(`number of pages: ${numberOfPages}`);
}}
onPageChanged={(page, numberOfPages) => {
console.log(`current page: ${page}`);
}}
onError={error => {
console.log(error);
}}
onPressLink={uri => {
console.log(`Link presse: ${uri}`);
}}
style={styles.pdf}
/>
}
</ScrollView>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'flex-start',
alignItems: 'center',
marginTop: 25,
},
pdf: {
flex: 1,
width: Dimensions.get('window').width,
height: Dimensions.get('window').height,
},
});
从“React”导入React;
从“react native”导入{样式表、维度、视图、滚动视图};
从“react native Pdf”导入Pdf;
导出默认类PDFScroll扩展React.Component{
建造师(道具){
超级(道具);
此.state={
当前位置:0,
};
this.scrolling=this.scrolling.bind(this);
}
componentDidMount(){
this.activeInterval=setInterval(this.scrolling,100);
}
组件将卸载(){
clearInterval(this.activeInterval);
}
滚动(){
位置=this.state.currentPosition+50;
this.pdf.scrollTo({x:position,动画:true});
//位置通过此值后,将捕捉回起始位置
设maxOffset=2000;
//将动画设置为在滚动结束时重复
if(this.state.currentPosition>maxOffset){
this.pdf.scrollTo({x:0,动画:false})
this.setState({currentPosition:0});
}
否则{
this.setState({currentPosition:position});
}
}
render(){
常量源={
uri:'http://samples.leanpub.com/thereactnativebook-sample.pdf',
是的,
};
返回(
this.pdf=ref}
>
{
{
log(`number of pages:${numberOfPages}`);
}}
onPageChanged={(第页,页数)=>{
log(`current page:${page}`);
}}
onError={error=>{
console.log(错误);
}}
onPressLink={uri=>{
log(`linkpresse:${uri}`);
}}
style={styles.pdf}
/>
}
);
}
}
const styles=StyleSheet.create({
容器:{
弹性:1,
justifyContent:“flex start”,
对齐项目:“居中”,
玛金托普:25,
},
pdf:{
弹性:1,
宽度:尺寸。获取('窗口')。宽度,
高度:尺寸。获取(“窗口”)。高度,
},
});