「React Native」上滑全屏

有一个需求,是需要上滑大于二分之一屏幕,动画展示全屏并跳转新页面,上滑小于二分之一屏幕时,动画回退到之前的默认高度布局。
于是采用了手势+动画完成,如下的效果:


上拉.gif

实际代码:

/**
 * 上滑全屏demo
 */
import React from "react"
import { Component } from 'react'
import { View, Text, Animated, PanResponder, Dimensions } from "react-native"

export default class SlideUpFullScreenDemo extends Component {
    constructor(props) {
        super(props);
        this.state = {
            height: new Animated.Value(100)
        }
    }

    componentWillMount() {
        this._panResponder = PanResponder.create({
            onStartShouldSetPanResponder: (evt, gestureState) => true,
            onPanResponderMove: this.onPanResponderMove,
            onPanResponderRelease: this._handlePanResponderEnd
        });
    }

    onPanResponderMove = (evt, gestureState) => {
        if (gestureState.dy < 0) {
            console.log('onPanResponderMove zyx dx', gestureState.dx)
            console.log('onPanResponderMove zyx dy', gestureState.dy)
            let height = -gestureState.dy
            Animated.spring(
                this.state.height,
                {
                    toValue: height + 100
                }
            ).start()
        }
    }

    _handlePanResponderEnd = (e, gestureState) => {
        if (gestureState.dy < 0) {
            // 执行向上移动画
            let height = -gestureState.dy
            console.log('_handlePanResponderEnd zyx dx', gestureState.dx)
            console.log('_handlePanResponderEnd zyx dy', gestureState.dy)
            if (height > Dimensions.get('window').height / 2) {
                height = Dimensions.get('window').height
            } else {
                height = 100
            }
            Animated.spring(
                this.state.height,
                {
                    toValue: height
                }
            ).start()
        }
    }

    render() {
        return (
            <View style={{ height: Dimensions.get('window').height, flexDirection: 'column-reverse', flex: 1 }}>
                <Animated.View
                    {...this._panResponder.panHandlers}
                    style={{
                        backgroundColor: 'red',
                        width: Dimensions.get('window').width,
                        height: this.state.height,
                        position: 'absolute',
                        top: (Dimensions.get('window').height - this.state.height)
                    }} >
                    <Text style={{ color: 'white', fontSize: 23 }}>hello wolrd</Text>
                </Animated.View>
            </View>
        )
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • github上关于iOS的各种开源项目集合(转) UI 下拉刷新 EGOTableViewPullRefresh-...
    其实也没有阅读 7,253评论 1 112
  • 11.1 姓名:韩艾辰 第62天 【学习:150分钟】 【冥想:20分钟】每天多次冥想 【找到/服务老师:每天至少...
    韩艾辰阅读 186评论 0 0
  • 夜幕降临 昏黄的灯光透过窗散漫进屋内 洒在遍地倾倒的酒瓶上 透过碧绿色的瓶体折射出微微柔光 这无数折射的柔光 是伤...
    渔浮阅读 269评论 0 1
  • 耕耘收获,天道酬勤! 人生只要努力,就有希望。永远不要说‘不’,要知难而上,知其不可为而为之,这样才能成功。世上没...
    娜娜优客生活阅读 489评论 0 0