React Native-当使用动画和键盘监听器出现键盘时如何向上滚动屏幕

时间:2020-05-19 14:28:23

标签: reactjs react-native

我目前正在开发一个移动应用程序,并且由于我的输入字段将由键盘覆盖,因此该移动应用程序中的表格存在一些问题。我尝试将Keyboard.addListener('keyboardWillShow')与React Native Animated一起使用,以便在屏幕上添加底部填充。底部填充的添加是有效的,并且是动画的,但是问题是内容没有随之移动,可能是因为我在表单中使用ScrollView吗?如果是这样,我该如何克服呢?还是有什么方法可以使我在出现键盘时向下滚动屏幕达键盘的高度(由于底部已经有填充,所以所需的结果已经存在,只是它没有滚动)向下,填充底部仍在屏幕之外)。

下面是我的代码,感谢您的帮助,并期待得到答案和帮助,谢谢!!

DynamicForm.js(在其中呈现表单并且应该在其中进行动画制作)

class DynamicForm extends Component {
    keyboardHeight = new Animated.Value(0);

    componentDidMount() {
        this.keyboardDidShowListener = Keyboard.addListener('keyboardWillShow', this.keyboardShow);
        this.keyboardDidHideListener = Keyboard.addListener('keyboardWillHide', this.keyboardHide);
    }

    componentWillUnmount() {
        this.keyboardDidShowListener.remove();
        this.keyboardDidHideListener.remove();
    }

    keyboardShow = (e) => {
        Animated.timing(this.keyboardHeight, {
            duration: e.duration,
            toValue: e.endCoordinates.height,
         }).start(() => {
            console.log(this.keyboardHeight)
        })
     }

    keyboardHide = (e) => {
        Animated.timing(this.keyboardHeight, {
            duration: e.duration,
            toValue: 0,
         }).start()
    }

    render() {
        return (
            <View style={{ padding: '3%' }} >
                <Animated.View style={{ paddingBottom: this.keyboardHeight }}>
                    {this.renderForm()}
                    {this.props.addon.map(addon => this.renderAddOn(addon))}
                    <Button title='Submit' onPress={() => this.props.onSubmit(this.state.data)} />
                </Animated.View>
            </View>
           )
       }
   }

SalesProjectCreate.js(DynamicForm的父组件)

class SalesProjectCreate extends Component {

    render() {
        return (
            <ScrollView style={{ flex: 1, backgroundColor: '#fff' }}>
                <View>
                    <DynamicForm className="form"
                        model={[
                            { key: "sales_project_name", label: "Project Name" },
                            { key: "sales_project_est_rev", label: "Estimated Revenue" },
                            { key: "timedelta", label: "Time Delta" },
                        ]}

                        addon={[]}

                        addonData={[]}

                        onSubmit={(data) => { this.onSubmit(data) }}
                    />
                </View>

            </ScrollView>
        )
    }
}

0 个答案:

没有答案