React Native - 动画
在本章中,我们将向您展示如何在 React Native 中使用 LayoutAnimation。
动画组件
我们将设置 myStyle 作为状态的属性。此属性用于设置 PresentationalAnimationComponent 内元素的样式。
我们还将创建两个函数 − expandElement 和 collapseElement。这些函数将更新状态的值。第一个将使用 spring 预设动画,而第二个将使用 linear 预设。我们也将把它们作为 props 传递。 Expand 和 Collapse 按钮调用 expandElement() 和 collapseElement() 函数。
在此示例中,我们将动态更改框的宽度和高度。由于 Home 组件相同,因此我们仅更改 Animations 组件。
App.js
import React, { Component } from 'react'
import { View, StyleSheet, Animated, TouchableOpacity } from 'react-native'
class Animations extends Component {
componentWillMount = () => {
this.animatedWidth = new Animated.Value(50)
this.animatedHeight = new Animated.Value(100)
}
animatedBox = () => {
Animated.timing(this.animatedWidth, {
toValue: 200,
duration: 1000
}).start()
Animated.timing(this.animatedHeight, {
toValue: 500,
duration: 500
}).start()
}
render() {
const animatedStyle = { width: this.animatedWidth, height: this.animatedHeight }
return (
<TouchableOpacity style = {styles.container} onPress = {this.animatedBox}>
<Animated.View style = {[styles.box, animatedStyle]}/>
</TouchableOpacity>
)
}
}
export default Animations
const styles = StyleSheet.create({
container: {
justifyContent: 'center',
alignItems: 'center'
},
box: {
backgroundColor: 'blue',
width: 50,
height: 100
}
})

