React Native 动画之View 从底部滑出,然后滑下去

本文介绍如何在React Native项目中创建一个View从底部滑出并滑动到一定高度的效果。通过在componentDidMount()中设置动画,并在点击事件中实现滑回,详细步骤包括动画实现及录屏展示。

项目中有个需求是进入到页面,有个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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值