• 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,
        },
    
    });
  • 相关阅读:
    react脚手架搭建及配置
    mac使用技巧
    nginx配置
    vue常见前端UI库
    自定义指令
    代码缩进修改
    webpack学习入门
    webpack使用extract-text-webpack-plugin打包时提示错误
    webpack未成功全局安装
    基于jQuery的AJAX实现三级联动菜单
  • 原文地址:https://www.cnblogs.com/jingguorui/p/13525306.html
Copyright © 2020-2023  润新知