Kipnoedels
Kipnoedels

Reputation: 1365

How to match size to children using react-native-gesture-handler

I am trying to create a draggable, scalable and rotatable component using react-native-gesture-handler. This is my first time trying out react-native-gesture-handler so I'm not sure if this is expected behaviour due to the hitboxes needed for the gesture triggers.

So I nested some GestureHandlers to get achieve all three behaviours, but my final <Animated.View> takes up the full screen width. I want the red area to only cover the size of children. This is a problem because the rotation and scale is based from the center of the red area, so the anchor point is at the wrong place.

Draggable:

render() {
        return (
            <PanGestureHandler
                ref={this.posRef}
                onGestureEvent={this._onGestureEvent}
                onHandlerStateChange={this._onPosHandlerStateChange}
                minPointers={1}
                maxPointers={1}
            >
                <Animated.View>
                    <RotationGestureHandler
                        ref={this.rotationRef}
                        simultaneousHandlers={[this.pinchRef, this.posRef]}
                        onGestureEvent={this._onRotateGestureEvent}
                        onHandlerStateChange={this._onRotateHandlerStateChange}
                        minPointers={2}
                        maxPointers={2}
                    >
                        <Animated.View>
                            <PinchGestureHandler
                                ref={this.pinchRef}
                                simultaneousHandlers={[this.rotationRef, this.posRef]}
                                onGestureEvent={this._onPinchGestureEvent}
                                onHandlerStateChange={this._onPinchHandlerStateChange}
                                minPointers={2}
                                maxPointers={2}
                            >
                                <Animated.View collapsable={false} style={{
                                    transform: [
                                        { translateX: this._translateX },
                                        { translateY: this._translateY },
                                        { scale: this._scale },
                                        { rotate: this._rotateStr },
                                    ],
                                    backgroundColor: 'red', // <-- HERE
                                }}>
                                    {this.props.children}
                                </Animated.View>
                            </PinchGestureHandler>
                        </Animated.View>
                    </RotationGestureHandler>
                </Animated.View>
            </PanGestureHandler>
        );
    }

Usage:

<Draggable>
   <View style={{ width: 100, height: 100, backgroundColor: 'blue' }}/>
</Draggable>

Results in:

Upvotes: 1

Views: 994

Answers (1)

Kipnoedels
Kipnoedels

Reputation: 1365

I found out that using alignSelf: 'center' makes the red are the same size as the child. But why does alignment determine the parent size?

Upvotes: 1

Related Questions