ReactNative中有两个API可以实现动画:LayoutAnimation和Animated。
Animated经常用来实现比较复杂的动画,LayoutAnimation则用来实现比较简单的动画。
要实现 单击 收缩按钮A 后, 收起列表。由于动画简单,所以我就采用了LayoutAnimation来实现。
import { UIManager } from ‘react-native’;
constructor(props) {
super(props);
UIManager.setLayoutAnimationEnabledExperimental && UIManager.setLayoutAnimationEnabledExperimental(true);
}
//定义了一些动画的config属性,duration为动画时间。
sss = () => {
LayoutAnimation.configureNext({
duration: 288,
update: {
duration: 288,
type: ‘linear’
}
});
};
//定义了一个单击事件,在事件中调用动画配置函数。flag则用来控制列表的高度。
handClick = () => {
const { flag } = this.state;
this.xsss();
this.setState({
flag: flag !== 0 ? 0 : cy(89),
});
};
在列表的外层元素中进行样式设置。
< style={{height:height}}>