如何检测用户是放大还是缩小反应本机
How to detect whether user zoomed in or zoomed out in react native
我正在开发一个 react native 应用程序,我想检测用户是放大了还是缩小了。
我只想在用户放大时打印用户放大,在用户缩小时打印缩小
类似于下面的伪代码
const detectZoom = (event)=>{
if(event.gesture === "zoomed in"){
console.log("Zoomed in");
}
if(event.gesture === "zoomed out"){
console.log("Zoomed out");
}
}
//This is how I want to call it
<TouchableHighlight onPress={this.detectZoom}>
</TouchableHighlight>
请告诉我更好的方法。
您应该使用 react-native-gesture-handler 并在 View/Container.
中专门使用捏合手势
yarn add react-native-gesture-handler
https://docs.swmansion.com/react-native-gesture-handler/docs/api/gesture-handlers/pinch-gh
<PinchGestureHandler
onGestureEvent={this._onPinchGestureEvent}
onHandlerStateChange={this._onPinchHandlerStateChange}>
<View style={styles.container} collapsable={false}>
</View>
</PinchGestureHandler>
您的处理人:
let previousScale = 1;
_onPinchHandlerStateChange = (event) => {
if (event.nativeEvent.oldState === State.ACTIVE) {
//Handle Zoom here based on the values..
let newScale = previousScale * event.nativeEvent.scale;
if(newScale > previousZoom){
console.log("zoomed in");
}else{
console.log("zoomed out");
}
}
};
我正在开发一个 react native 应用程序,我想检测用户是放大了还是缩小了。
我只想在用户放大时打印用户放大,在用户缩小时打印缩小
类似于下面的伪代码
const detectZoom = (event)=>{
if(event.gesture === "zoomed in"){
console.log("Zoomed in");
}
if(event.gesture === "zoomed out"){
console.log("Zoomed out");
}
}
//This is how I want to call it
<TouchableHighlight onPress={this.detectZoom}>
</TouchableHighlight>
请告诉我更好的方法。
您应该使用 react-native-gesture-handler 并在 View/Container.
中专门使用捏合手势yarn add react-native-gesture-handler
https://docs.swmansion.com/react-native-gesture-handler/docs/api/gesture-handlers/pinch-gh
<PinchGestureHandler
onGestureEvent={this._onPinchGestureEvent}
onHandlerStateChange={this._onPinchHandlerStateChange}>
<View style={styles.container} collapsable={false}>
</View>
</PinchGestureHandler>
您的处理人:
let previousScale = 1;
_onPinchHandlerStateChange = (event) => {
if (event.nativeEvent.oldState === State.ACTIVE) {
//Handle Zoom here based on the values..
let newScale = previousScale * event.nativeEvent.scale;
if(newScale > previousZoom){
console.log("zoomed in");
}else{
console.log("zoomed out");
}
}
};