反应本机PanResponder

时间:2018-06-24 20:43:32

标签: reactjs react-native

我正在尝试在React Native中使用PanResponder。

非常简单-我有2个元素:

  1. 视图,即屏幕的宽度和高度100。
  2. 一个Animated.View,宽50,高50,边框半径25,所以它是一个圆。圆圈位于主视图内。

我希望能够有效地在屏幕上拖动圆圈。有人可以帮我吗?

到目前为止,这是我的代码:

import React from 'react';
import { Animated, Dimensions, PanResponder, StyleSheet, View } from 'react-native';



export class MySlider extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            pan: new Animated.ValueXY()
        };
    }

    _panResponder = PanResponder.create({
            // Ask to be the responder:
            onStartShouldSetPanResponder: (evt, gestureState) => true,
            onStartShouldSetPanResponderCapture: (evt, gestureState) => true,
            onMoveShouldSetPanResponder: (evt, gestureState) => true,
            onMoveShouldSetPanResponderCapture: (evt, gestureState) => true,

            onPanResponderGrant: (evt, gestureState) => {
                this.state.pan.setValue({x: 0, y: 0});
                return true
            },
            onPanResponderMove: (evt, gestureState) => {
                Animated.event([null, {dx: this.state.pan.x, dy: this.state.pan.y}])
                return true
            },
            onPanResponderTerminationRequest: (evt, gestureState) => true,
            onPanResponderRelease: (evt, gestureState) => {
                return true
            },
            onPanResponderTerminate: (evt, gestureState) => {
                return true
            },
            onShouldBlockNativeResponder: (evt, gestureState) => {
                return true;
            },
        });

    render() {
        let { pan } = this.state;
        let [translateX, translateY] = [pan.x, pan.y];
        const styles = StyleSheet.create({
            mover: {
                transform: [{translateX:translateX}, {translateY:5}],
                height: 50,
                width: 50,
                borderRadius: 25,
                backgroundColor: 'blue'
            },
            holder: {
                height: 100,
                width: Dimensions.get('window').width,
                backgroundColor: 'yellow'
            }
        });
        return (
            <View style={styles.holder}>
                <Animated.View style={styles.mover} {...this._panResponder.panHandlers}/>   
            </View>
            )
    }
}

2 个答案:

答案 0 :(得分:0)

我稍微更改了代码 所以试试这个

import React,{Component} from 'react'
import {
  PanResponder,
  View,
  Animated,
  StyleSheet,
  Dimensions
} from 'react-native'

export default class MySlider extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            pan: new Animated.ValueXY()
        };
    

    this._panResponder = PanResponder.create({
     onStartShouldSetPanResponder : () => true,
      onPanResponderGrant: (e, gestureState) => {
        this.setState({isAddNewSession:true})
      },
      onPanResponderMove : Animated.event([null,{ //Step 3

          dx : this.state.pan.x,
          dy : this.state.pan.y
      }]),
      onPanResponderRelease           : (e, gesture) => {  this.setState({isAddNewSessionModal:true},
        
      )
        Animated.spring(            
            this.state.pan,         
            {toValue:{x:0,y:0}}     //To default position you can delete this animated.spring() 
        ).start();
        this.setState({isAddNewSession:false})
    },
        });
}
    render() {
        let { pan } = this.state;
        let [translateX, translateY] = [pan.x, pan.y];
        const styles = StyleSheet.create({
            mover: {
                transform: [{translateX:translateX}, {translateY:translateY}],
                height: 50,
                width: 50,
                position:'absolute',
                top:0,
                borderRadius: 25,
                zIndex:20,
                backgroundColor: 'blue'
            },
            holder: {
                height: 100,
                width: Dimensions.get('window').width,
                backgroundColor: 'yellow'
            }
        });
        return (
            <View style={styles.holder}>
                <Animated.View style={styles.mover} {...this._panResponder.panHandlers}/>   
            </View>
            )
    }
}

让我知道它是否起作用...

答案 1 :(得分:0)

是的。我复制并粘贴了您的班级以代替我的班级,现在可以使用了。我可以在二维上拖动它,放开时可以回到开始的地方。谢谢。

this.setState({isAddNewSession:true})this.setState({isAddNewSessionModal:true})this.setState({isAddNewSession:false})是什么?

此外,当我删除Animated.Spring时,可以拖放圆-但是,当我第二次拖动圆时,圆从原始点开始。关于如何在我离开的地方接机的任何建议?我尝试用moveX和moveY替换dx和dy,但是没有用。我还尝试将旧值存储在变量中,但是它很复杂,因为它是一个Animated值和一个javascript对象。