当用户使用2个手指缩放图像时,我想放大或缩放图像。如何使用PanResponder
实现它?
或者有人可以建议我如何完成它的良好逻辑?
ps:我不想使用任何外部API。
对于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}/>;
}
}