From c0e5ae3a5709b3452f17220524e8067d2a5c07e7 Mon Sep 17 00:00:00 2001 From: Michael Lefkowitz Date: Fri, 24 May 2019 10:26:36 -0500 Subject: [PATCH] x and y axis toggleable --- .gitignore | 4 +- README.md | 2 + package.json | 2 +- src/MeasureX.js | 129 ++++++++++++++++++++++++++++-------------------- src/MeasureY.js | 93 +++++++++++++++++++++------------- src/index.js | 34 +++++++++++-- 6 files changed, 171 insertions(+), 93 deletions(-) diff --git a/.gitignore b/.gitignore index 4b710d5..2383a87 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ node_modules/ coverage/ .vscode/ -.npm/ \ No newline at end of file +.npm/ + +demo/ \ No newline at end of file diff --git a/README.md b/README.md index f0dcf72..fc66b62 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ import { RNRuler } from 'react-native-ruler'; ; ``` +- Tap the bottom right corner of the ruler to swap between axis + ## License react-native-ruler is [MIT licensed](https://github.com/lfkwtz/react-native-ruler/tree/master/LICENSE) diff --git a/package.json b/package.json index e0e5185..0eeaaba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-ruler", - "version": "0.0.7", + "version": "0.1.0", "description": "A devtool for measuring pixel dimensions on your React Native screens", "license": "MIT", "author": "Michael Lefkowitz ", diff --git a/src/MeasureX.js b/src/MeasureX.js index 254c1c2..68aa6ba 100644 --- a/src/MeasureX.js +++ b/src/MeasureX.js @@ -9,7 +9,8 @@ export class MeasureX extends PureComponent { this.state = { pan: new Animated.ValueXY(), - xLine: 0, + opacity: new Animated.Value(1), + xLine: null, }; this._val = { x: 0, y: 0 }; @@ -19,7 +20,10 @@ export class MeasureX extends PureComponent { }); this.panResponder = PanResponder.create({ - onStartShouldSetPanResponder: () => { + onStartShouldSetPanResponder: ({ nativeEvent }, { numberActiveTouches }) => { + if (nativeEvent.pageY > height * 0.95 || numberActiveTouches > 1) { + this.props.swapLines(); + } return true; }, onPanResponderMove: Animated.event([ @@ -28,79 +32,107 @@ export class MeasureX extends PureComponent { dx: this.state.pan.x, }, ]), - onPanResponderRelease: (e, gesture) => { - const xLine = width + gesture.dx - 30; + onPanResponderRelease: (e, { dx }) => { + const xLine = width + dx - 30; console.log('x location', xLine); - this.setState({ xLine }); - Animated.spring(this.state.pan, { toValue: { x: 0, y: 0 } }).start(); + this.setState({ xLine, opacity: new Animated.Value(1) }, () => { + Animated.spring(this.state.pan, { toValue: { x: 0, y: 0 } }).start(); + Animated.timing(this.state.opacity, { + toValue: 0, + duration: 1500, + }).start(); + }); }, }); } - modifyVerticalAlignment({ tick }) { - if (tick < 10) { - return { alignItems: 'flex-end' }; - } else if (tick > height - 10) { - return { alignItems: 'flex-start' }; + setAlignment({ mark }) { + if (mark === 0) { + return 2; + } else if (mark === height) { + return -12; } + return 2; } - renderVerticalRulerTicks() { - const ticks = [0, height * 0.25, height * 0.5, height * 0.75, height]; - return ticks.map((tick) => { + renderHatchMarks() { + const marks = [0, height * 0.25, height * 0.5, height * 0.75, height]; + return marks.map((mark, idx) => { return ( - + - {tick} + + {mark} + ); }); } render() { - const { xLine, pan } = this.state; + const { xLine, pan, opacity } = this.state; const panStyle = { transform: pan.getTranslateTransform(), }; return ( - - + - {xLine.toFixed(2)} - + {xLine === null ? '' : xLine.toFixed(2)} + + {/* + {xLine.toFixed(2)} + */} - - {xLine.toFixed(2)} - + {/* + {xLine.toFixed(2)} + */} - {/* {this.renderVerticalRulerTicks()} */} + {this.renderHatchMarks()} ); } } - -const defaultStyles = StyleSheet.create({ - lineText: { - position: 'absolute', - textAlign: 'center', - width: '100%', - fontWeight: 'bold', - }, -}); diff --git a/src/MeasureY.js b/src/MeasureY.js index 75ae045..50ac5f4 100644 --- a/src/MeasureY.js +++ b/src/MeasureY.js @@ -9,7 +9,8 @@ export class MeasureY extends PureComponent { this.state = { pan: new Animated.ValueXY(), - yLine: 0, + opacity: new Animated.Value(1), + yLine: null, }; this._val = { x: 0, y: 0 }; @@ -19,7 +20,10 @@ export class MeasureY extends PureComponent { }); this.panResponder = PanResponder.create({ - onStartShouldSetPanResponder: () => { + onStartShouldSetPanResponder: ({ nativeEvent }, { numberActiveTouches }) => { + if (nativeEvent.pageX > width * 0.95 || numberActiveTouches > 1) { + this.props.swapLines(); + } return true; }, onPanResponderMove: Animated.event([ @@ -28,16 +32,22 @@ export class MeasureY extends PureComponent { dy: this.state.pan.y, }, ]), - onPanResponderRelease: (e, gesture) => { - const yLine = height + gesture.dy - 30; + onPanResponderRelease: (e, { dy }) => { + const yLine = height + dy - 30; console.log('y location', yLine); - this.setState({ yLine }); - Animated.spring(this.state.pan, { toValue: { x: 0, y: 0 } }).start(); + + this.setState({ yLine, opacity: new Animated.Value(1) }, () => { + Animated.spring(this.state.pan, { toValue: { x: 0, y: 0 } }).start(); + Animated.timing(this.state.opacity, { + toValue: 0, + duration: 1500, + }).start(); + }); }, }); } - modifyAlignment({ mark }) { + setAlignment({ mark }) { if (mark === 0) { return { alignItems: 'flex-start' }; } else if (mark === width) { @@ -50,52 +60,70 @@ export class MeasureY extends PureComponent { const marks = [0, width * 0.25, width * 0.5, width * 0.75, width]; return marks.map((mark) => { return ( - + - {mark} + {mark} ); }); } render() { - const { yLine, pan } = this.state; + const { yLine, pan, opacity } = this.state; const panStyle = { transform: pan.getTranslateTransform(), }; return ( - - + - {yLine.toFixed(2)} - + {yLine === null ? '' : yLine.toFixed(2)} + + {/* + {yLine.toFixed(2)} + */} - - {yLine.toFixed(2)} - + {/* + {yLine.toFixed(2)} + */} - {/* */} - + + {showY ? ( + + ) : ( + + )} ); }