zoukankan      html  css  js  c++  java
  • rn实现一个view的拖拽

    通过添加手势事件

    // dx 和 dy:从触摸操作开始到现在的累积横向/纵向路程
    //
    // moveX 和 moveY:最近一次移动时的屏幕横/纵坐标
    //
    // numberActiveTouches:当前在屏幕上的有效触摸点的数量
    //
    // stated:和之前一样,用来识别手指的ID
    //
    // vx 和 vy:当前横向/纵向移动的速度
    //
    // x0 和 y0:当触摸操作开始时组件相对于屏幕的横/纵坐标
    
    import React, {PureComponent, Component} from 'react';
    import {
        AppRegistry,
        StyleSheet,
        Text,
        View,
        PanResponder,
    } from 'react-native';
    
    export default class TouchStartAndRelease extends PureComponent {
        constructor(props) {
            super(props);
            this.state = {
                backgroundColor: 'red',
                marginTop: 100,
                marginLeft: 100,
            };
            this.lastX = this.state.marginLeft;
            this.lastY = this.state.marginTop;
        }
    
        componentWillMount(){
            this._panResponder = PanResponder.create({
                onStartShouldSetPanResponder: (evt, gestureState) => {
                    return true;
                },
                onMoveShouldSetPanResponder:  (evt, gestureState) => {
                    return true;
                },
                onPanResponderGrant: (evt, gestureState) => {
                    this._highlight();
                },
                onPanResponderMove: (evt, gestureState) => {
                    console.log(`gestureState.dx : ${gestureState.dx}   gestureState.dy : ${gestureState.dy}`);
                    this.setState({
                        marginLeft: this.lastX + gestureState.dx,
                        marginTop: this.lastY + gestureState.dy,
                    });
                },
                onPanResponderRelease: (evt, gestureState) => {
                    this._unhighlight();
                    this.lastX = this.state.marginLeft;
                    this.lastY = this.state.marginTop;
                },
                onPanResponderTerminate: (evt, gestureState) => {
                },
            });
        }
    
        _unhighlight(){
            this.setState({
                backgroundColor: 'red',
            });
        }
    
        _highlight(){
            this.setState({
                backgroundColor: 'blue',
            });
        }
    
        render() {
            return (
                <View style={styles.container}>
                    <View style={[styles.redView,
                        {
                            backgroundColor: this.state.backgroundColor,
                            marginTop: this.state.marginTop,
                            marginLeft: this.state.marginLeft,
                        }
                    ]}
                          {...this._panResponder.panHandlers}
                    />
                </View>
            );
        }
    }
    
    const styles = StyleSheet.create({
        container: {
            flex: 1,
        },
        redView: {
             100,
            height: 100,
        },
    
    });
  • 相关阅读:
    今天的学习
    sql&nbsp;修改字段
    原来这个分类是powerdesigner
    sql&nbsp;sum&nbsp;&nbsp;&nbsp;&nbsp;空或0
    mac 配置maven报zsh: command not found各种坑点走位
    java-Map集合中存入的数组值转存到ArryList集合中的实现
    Java-集合总结之Collection和Map--Map(3)
    Java-集合总结之Collection和Map--Set(2)
    Java-集合总结之Collection和Map--List(1)
    测试-bug跟踪过程中的相关状态英文释义
  • 原文地址:https://www.cnblogs.com/jingguorui/p/13525306.html
Copyright © 2011-2022 走看看