为什么动画 toValue React Native 不能正常工作?
Why animated toValue react native not working properly?
我尝试使用 react-native
中的 Animated
API。我想制作一个看起来像手风琴的组件(这是为了尝试 Animated
API。我知道我可以使用其他手风琴库)。动画有效,但 toValue
没有扩展为内容的高度。到目前为止,这是我尝试过的:
Child.js
import React, { Component } from 'react';
import {
Animated,
View,
} from 'react-native';
class Child extends Component {
constructor(props) {
super(props);
this.state = {
expanded: false,
animation: null,
maxHeight: '',
minHeight: '',
};
}
toggle = () => {
const {
expanded, maxHeight, minHeight,
} = this.state;
const initialValue = expanded ? maxHeight + minHeight : minHeight;
const finalValue = expanded ? minHeight : maxHeight + minHeight;
this.setState(prevState => ({
expanded: !prevState.expanded,
}));
this.state.animation.setValue(initialValue);
Animated.spring(
this.state.animation,
{
toValue: finalValue,
bounciness: 0,
},
).start();
}
setMinHeight = (e) => {
this.setState({
minHeight: e.nativeEvent.layout.height,
animation: new Animated.Value(e.nativeEvent.layout.height),
});
}
setMaxHeight = (e) => {
this.setState({
maxHeight: e.nativeEvent.layout.height,
});
}
render() {
const { header, content } = this.props;
const { animation } = this.state;
return (
<Animated.View style={{
height: animation,
overflow: 'hidden',
width: '100%',
}}
>
<View onLayout={e => this.setMinHeight(e)}>
{header}
</View>
<View onLayout={e => this.setMaxHeight(e)}>
{content}
</View>
</Animated.View>
);
}
}
export default Child;
App.js
import React, { Component, Fragment } from 'react';
import {
ScrollView,
View,
Text,
StyleSheet,
Button,
} from 'react-native';
import { Row } from 'react-native-easy-grid';
// You can import from local files
import Child from './components/Child';
export default class App extends React.Component {
render() {
const header = (
<Button
title="press"
onPress={() => this.toggle.toggle()}
/>
);
const content = (
<Fragment>
<View
style={{
width: 300,
backgroundColor: 'green',
padding: 20,
}}
>
<View style={{ width: 'auto', padding: 10, backgroundColor: 'blue' }}>
<Text>Content1</Text>
<Text>Content2</Text>
<Text>Content3</Text>
// Please try to add more text here
</View>
</View>
</Fragment>
);
return (
<Fragment>
<ScrollView style={[styles.container]}>
<Row style={{ marginTop: 30 }}>
<View style={[styles.view]}>
<Child
content={content}
header={header}
ref={(ref) => { this.toggle = ref; }}
/>
</View>
</Row>
</ScrollView>
</Fragment>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 15,
backgroundColor: 'purple',
},
view: {
flex: 1,
backgroundColor: 'pink',
},
});
我在这里想念什么?任何帮助都会非常有帮助和感激。谢谢!
更新
当我尝试将 console.log(e.nativeEvent.layout.height)
放入 setMaxHeight
时,它有时会显示不同的值。怎么会这样?
正如@hardworker 在评论中所说,所以我尝试在 maxHeight 和 minHeight 的 setState 之前进行验证。防止 maxHeight 和 minHeight 的状态在初始渲染后发生变化。这是工作代码:
setMinHeight = (e) => {
if(this.state.minHeight === '') {
this.setState({
minHeight: e.nativeEvent.layout.height,
animation: new Animated.Value(e.nativeEvent.layout.height),
});
}
}
setMaxHeight = (e) => {
if (this.state.maxHeight === '') {
this.setState({
maxHeight: e.nativeEvent.layout.height,
});
}
}
我尝试使用 react-native
中的 Animated
API。我想制作一个看起来像手风琴的组件(这是为了尝试 Animated
API。我知道我可以使用其他手风琴库)。动画有效,但 toValue
没有扩展为内容的高度。到目前为止,这是我尝试过的:
Child.js
import React, { Component } from 'react';
import {
Animated,
View,
} from 'react-native';
class Child extends Component {
constructor(props) {
super(props);
this.state = {
expanded: false,
animation: null,
maxHeight: '',
minHeight: '',
};
}
toggle = () => {
const {
expanded, maxHeight, minHeight,
} = this.state;
const initialValue = expanded ? maxHeight + minHeight : minHeight;
const finalValue = expanded ? minHeight : maxHeight + minHeight;
this.setState(prevState => ({
expanded: !prevState.expanded,
}));
this.state.animation.setValue(initialValue);
Animated.spring(
this.state.animation,
{
toValue: finalValue,
bounciness: 0,
},
).start();
}
setMinHeight = (e) => {
this.setState({
minHeight: e.nativeEvent.layout.height,
animation: new Animated.Value(e.nativeEvent.layout.height),
});
}
setMaxHeight = (e) => {
this.setState({
maxHeight: e.nativeEvent.layout.height,
});
}
render() {
const { header, content } = this.props;
const { animation } = this.state;
return (
<Animated.View style={{
height: animation,
overflow: 'hidden',
width: '100%',
}}
>
<View onLayout={e => this.setMinHeight(e)}>
{header}
</View>
<View onLayout={e => this.setMaxHeight(e)}>
{content}
</View>
</Animated.View>
);
}
}
export default Child;
App.js
import React, { Component, Fragment } from 'react';
import {
ScrollView,
View,
Text,
StyleSheet,
Button,
} from 'react-native';
import { Row } from 'react-native-easy-grid';
// You can import from local files
import Child from './components/Child';
export default class App extends React.Component {
render() {
const header = (
<Button
title="press"
onPress={() => this.toggle.toggle()}
/>
);
const content = (
<Fragment>
<View
style={{
width: 300,
backgroundColor: 'green',
padding: 20,
}}
>
<View style={{ width: 'auto', padding: 10, backgroundColor: 'blue' }}>
<Text>Content1</Text>
<Text>Content2</Text>
<Text>Content3</Text>
// Please try to add more text here
</View>
</View>
</Fragment>
);
return (
<Fragment>
<ScrollView style={[styles.container]}>
<Row style={{ marginTop: 30 }}>
<View style={[styles.view]}>
<Child
content={content}
header={header}
ref={(ref) => { this.toggle = ref; }}
/>
</View>
</Row>
</ScrollView>
</Fragment>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 15,
backgroundColor: 'purple',
},
view: {
flex: 1,
backgroundColor: 'pink',
},
});
我在这里想念什么?任何帮助都会非常有帮助和感激。谢谢!
更新
当我尝试将 console.log(e.nativeEvent.layout.height)
放入 setMaxHeight
时,它有时会显示不同的值。怎么会这样?
正如@hardworker 在评论中所说,所以我尝试在 maxHeight 和 minHeight 的 setState 之前进行验证。防止 maxHeight 和 minHeight 的状态在初始渲染后发生变化。这是工作代码:
setMinHeight = (e) => {
if(this.state.minHeight === '') {
this.setState({
minHeight: e.nativeEvent.layout.height,
animation: new Animated.Value(e.nativeEvent.layout.height),
});
}
}
setMaxHeight = (e) => {
if (this.state.maxHeight === '') {
this.setState({
maxHeight: e.nativeEvent.layout.height,
});
}
}