react native 手势绘图

项目新增一个业务需求,需要用户可以通过手势进行签名,然后去研究了下react native的ART,最后搞出来个这个东西。

import React, { Component } from "react"
import {
    View,
    ART,
    PanResponder,
    Dimensions
} from "react-native"

let {
    Surface,
    Shape,
    Path
} = ART;
let {
    height: deviceHeight,
    width: deviceWidth
} = Dimensions.get('window');
let path;

class Draw extends Component {

	state = {
		coordinates: [] // 线路
	}	

	componentWillMount() {
        this._panResponder = PanResponder.create({
            onStartShouldSetPanResponder: this._handleStartShouldSetPanResponder,
            onMoveShouldSetPanResponder: this._handleMoveShouldSetPanResponder,
            onPanResponderGrant: this._handlePanResponderGrant,
            onPanResponderMove: this._onPanResponderMove,
            onPanResponderRelease: this._handlePanResponderEnd,
            onPanResponderTerminate: this._handlePanResponderEnd,
        });
    }

	_handleStartShouldSetPanResponder = (e, gestureState) => {
		// 响应触摸
        return true;
    }

    _handleMoveShouldSetPanResponder = (e, gestureState) => {
    	// 响应移动
        return true;
    }


    _handlePanResponderGrant = (e, gestureState) => {
    	//  响应移动操作
        let coordinates = this.state.coordinates.concat([{
            x: e.nativeEvent.locationX,
            y: e.nativeEvent.locationY,
        }])
        this.setState({ coordinates })
    }

    _onPanResponderMove = (e, gestureState) => {
    	// 最近一次的移动距离
        let coordinates = this.state.coordinates.concat([{
            x: e.nativeEvent.locationX,
            y: e.nativeEvent.locationY,
        }])
        this.setState({ coordinates })
    }

    _handlePanResponderEnd = () => {
    	// 结束触摸
    }
	render() {
       path = Path()
       this.state.coordinates.forEach(item => {
       	   // ios获取的x,y有时候为0,android正常,这里需要处理一下
           if (item.x && item.y) {
               if (item.move) 
                   path.moveTo(item.x, item.y)
               else
                   path.lineTo(item.x, item.y)
           }
       })
		
		return <View style={{ flex: 1 }}>
			<View {...this._panResponder.panHandlers}>
                <Surface width={deviceWidth} height={deviceHeight}>
                    <Shape d={path} stroke="#000000" strokeWidth={3}/>
                </Surface>
            </View>
		</View>
	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值