项目中有个需求是进入到页面,有个view 是需要从底部滑出,滑倒一定高度,当点击button 是再滑回原来的地方。研究了半天,React Native 的动画,写了一个简单的Demo:
(1)在componentDidMount()方法里从底部滑出view;
(2)点击页面的空白处,view 滑下去。
import React, { Component } from 'react';
import {
AppRegistry,
View,
Animated,
Dimensions,
TouchableOpacity
} from 'react-native';
import FadeInView from './FadeInView';
const { width, height } = Dimensions.get("window");
export default class App extends Component {
constructor(props) {
super(props)
this.state = {
securetyTipViewY: new Animated.Value(height),
}
}
componentDidMount() {
this._showTipView()
}
//展示View
_showTipView = () => {
Animated.timing(
this.state.securetyTipViewY,
{
toValue: height - 254 - 64,
duration: 300, //动画时长300毫秒
}
).start();
}
//隐藏view
_hiddenTipView = () => {
Animated.timing(
this.state.securetyTipViewY,
{
toValue: height,
duration: 300, //动画时长300毫秒
}).start();
}
render() {
return (
<TouchableOpacity style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }} onPress={this._hiddenTipView}>
<Animated.View style={{ position: "absolute", top: this.state.securetyTipViewY, backgroundColor: 'red', height: 50, width: 300 }}>
<View style={{ backgroundColor: 'yellow', width: 300, height: 50 }}></View>
</Animated.View>
</TouchableOpacity>
)
}
}

React Native 动画,这篇写得很好https://www.jianshu.com/p/7fd37d8ed138
初次尝试了电脑录屏方法:https://jingyan.baidu.com/article/6525d4b19cb9b6ac7d2e94ab.html
本文介绍如何在React Native项目中创建一个View从底部滑出并滑动到一定高度的效果。通过在componentDidMount()中设置动画,并在点击事件中实现滑回,详细步骤包括动画实现及录屏展示。
2437

被折叠的 条评论
为什么被折叠?



