fra*_*ank 9 animation react-native
我有一个控制子元素渲染的元素.(一个TouchableHighlight,它设置了一些状态onPress.)在子元素的componentDidMount方法中,我构造了一个Animated.spring和start它.这适用于输入,但我需要反向做相同的动画退出(它就像一个抽屉).componentWillUnmount执行得太快,Animated.spring甚至无法开始工作.
我该如何处理孩子出口的动画?
我已经实现了一个FadeInOut组件,可以在其isVisible属性发生变化时为组件设置动画.我之所以这样做是因为我想避免显式处理应该使用动画进入/退出的组件中的可见性状态.
<FadeInOut isVisible={this.state.someBooleanProperty} style={styles.someStyle}>
<Text>Something...</Text>
</FadeInOut>
Run Code Online (Sandbox Code Playgroud)
此实现使用延迟淡入淡出,因为我使用它来显示进度指示器,但您可以将其更改为使用您想要的任何动画,或者将其概括为接受动画参数作为道具:
'use strict';
import React from 'react-native';
const {
View,
Animated,
PropTypes
} = React;
export default React.createClass({
displayName: 'FadeInOut',
propTypes: {
isVisible: PropTypes.bool.isRequired,
children: PropTypes.node.isRequired,
style: View.propTypes.style
},
getInitialState() {
return {
view: this.props.children,
opacity: new Animated.Value(this.props.isVisible ? 1 : 0)
};
},
componentWillReceiveProps(nextProps) {
const isVisible = this.props.isVisible;
const shouldBeVisible = nextProps.isVisible;
if (isVisible && !shouldBeVisible) {
Animated.timing(this.state.opacity, {
toValue: 0,
delay: 500,
duration: 200
}).start(this.removeView);
}
if (!isVisible && shouldBeVisible) {
this.insertView();
Animated.timing(this.state.opacity, {
toValue: 1,
delay: 500,
duration: 200
}).start();
}
},
insertView() {
this.setState({
view: this.props.children
});
},
removeView() {
this.setState({
view: null
});
},
render() {
return (
<Animated.View
pointerEvents={this.props.isVisible ? 'auto' : 'none'}
style={[this.props.style, {opacity: this.state.opacity}]}>
{this.state.view}
</Animated.View>
);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5570 次 |
| 最近记录: |