当用户用两根手指捏住图像时,我想放大或缩小图像。如何使用PanResponder
来实现它?
或者有人可以建议我如何做一个好的逻辑吗?
PS:我不想使用任何外部API。
答案 0 :(得分:0)
对于ios,它可以将图像包装在ScrollView中,因为它具有对以下属性的内置支持
maximumZoomScale
minimumZoomScale
pinchGestureEnabled
尝试类似
<ScrollView
maximumZoomScale={SOME_VALUE i.e. 2.5}
minimumZoomScale={MIN_ZOOM_VALUE i.e 1.0}
pinchGestureEnabled={true}
>
<Image source={YOUR_IMAGE} style={YOUR_IMAGE_STYLEs}/>
</Scrollview>
在Android上,您可能需要使用某些库或通过PanResponder和Animated对其进行管理,PanResponder示例代码可能是您的一个很好的起点。
class ExampleComponent extends Component {
constructor(props) {
super(props);
this._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) => {
// The gesture has started. Show visual feedback so the user knows
// what is happening!
// gestureState.d{x,y} will be set to zero now
},
onPanResponderMove: (evt, gestureState) => {
// The most recent move distance is gestureState.move{X,Y}
// The accumulated gesture distance since becoming responder is
// gestureState.d{x,y}
},
onPanResponderTerminationRequest: (evt, gestureState) => true,
onPanResponderRelease: (evt, gestureState) => {
// The user has released all touches while this view is the
// responder. This typically means a gesture has succeeded
},
onPanResponderTerminate: (evt, gestureState) => {
// Another component has become the responder, so this gesture
// should be cancelled
},
onShouldBlockNativeResponder: (evt, gestureState) => {
// Returns whether this component should block native components from becoming the JS
// responder. Returns true by default. Is currently only supported on android.
return true;
},
});
}
render() {
const imageStyles = {transform: [
{
scale: ANIMATION_VALUE_CALCULATED_ON_MOVEMENT //
}
]}
return <Animated.Image {...this._panResponder.panHandlers} style ={imageStyles} source={IMAGE_SOURCE}/>;
}
}