TouchView.js 1.85 KB
import React from 'react'
import {View, StyleSheet, Animated, PanResponder, Text, TouchableOpacity,Platform,Keyboard} from 'react-native'
import {width} from "../pages/Home";
import {observable} from "mobx";
import {observer} from 'mobx-react/native'
import Interactable from 'react-native-interactable';
const styles = StyleSheet.create({
	container: {
		width: width + 65,
		flexDirection: 'row'
	},
	delView: {
		width: 65,
		alignItems: 'center',
		justifyContent: 'center',
		backgroundColor: "#ea281a",
	},
	delText: {
		color: "white",
		fontSize: 16,
	}
})
@observer
export default class TouchView extends React.Component {
	@observable
	x = 0

	@observable
	show=false
	@observable
	height=20
	xy=new Animated.ValueXY()
	_xy={x:0,y:0}

	moveBack(){
		this.view&&this.view.snapTo({x:0,index:0})
		// this.view&&this.view.changePosition({x:0})

		this.show=false
	}
	onPress = async () => {
		if (this.props.onPress) {
			await this.props.onPress()
		}
	}

	onDrag=v=>{
		const {nativeEvent}=v
		console.log(nativeEvent)
		if(nativeEvent.x<0){
			this.show=true
			this.props.call(this.props.id)
		}else{
			this.show=false
		}
		console.log(this.show)
	}

	render() {
		return (
			<View style={[styles.container]}>
			<Interactable.View
				style={[styles.container]}
				onLayout={v=>{this.height=v.nativeEvent.layout.height}}
				horizontalOnly={true}
				snapPoints={[{x: 0}, {x: -65}]}
				springPoints={[{x: 0, tension: 3000, damping: 0.1, influenceArea: {left: 0}}]}
				ref={v=>this.view=v}
				onDrag={this.onDrag}
			>
				{this.props.children}
				<TouchableOpacity style={styles.delView} onPress={this.onPress}>
					<Text style={styles.delText}>删除</Text>
				</TouchableOpacity>
				{this.show&&<TouchableOpacity style={{width,height:this.height,position:"absolute",left:0}} onPress={()=>this.moveBack()}>
				</TouchableOpacity>}
			</Interactable.View>
			</View>
		)
	}
}