React Native动画组件Animated-----案例
程序员文章站
2024-01-14 16:44:04
...
import React, { Component } from 'react'
import { View, StyleSheet, Animated, TouchableOpacity } from 'react-native'
class App extends Component {
//为什么要使用UNSAFE_componentWillMount,因为componentWillMount要废弃
UNSAFE_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 App
const styles = StyleSheet.create({
container: {
justifyContent: 'center',
alignItems: 'center'
},
box: {
backgroundColor: 'blue',
width: 50,
height: 100
}
})
上一篇: 模板-倍增法求LCA(邻接表)
下一篇: 判断素数【C语言】
推荐阅读