TouchableOpacity is not working when Transform Animation is applied

869 Views Asked by At

Below code is to render a Touchable button with Transform Animation.

    const { scrollY, headerScrollDistance } = this.state;
    const profileImageTranslateX = scrollY.interpolate({
        inputRange: [0, headerScrollDistance],
        outputRange: [0, -(ScreenWidth  /2) + 32],
        extrapolate: 'clamp',
    });

    const profileImageTranslateY = scrollY.interpolate({
        inputRange: [0, headerScrollDistance],
        outputRange: [0, -11],
        extrapolate: 'clamp',
    });

    const profileImageScale = scrollY.interpolate({
        inputRange: [0, headerScrollDistance / 2, headerScrollDistance],
        outputRange: [1, 0.8, 0.6],
        extrapolate: 'clamp',
    });

    return (
        <Animated.View
            style={[
                Styles.animatedView.profileStyle,
                {
                    transform: [
                        { translateX: profileImageTranslateX },
                        { translateY: profileImageTranslateY },
                        { scale: profileImageScale }
                    ]
                }
            ]}
        >
            <TouchableOpacity activeOpacity={0.5} onPress={() => this.props.history.push('./profilePhotoChanger')}>
                <ImageComp profileImageUrl={profileimageurl} imageStyle={Styles.homePageImageStyle} />
            </TouchableOpacity>
        </Animated.View>
    );

As page scrolls, Animation applies to Touchable button. Button is working as expected when transform animation is not applied. But not working when animation is applied. If page comes back to it's normal state(i.e scrolling back) then button works as expected.

Is it normal behaviour in react-native that TouchableOpacity's onPress wont't work when animation applied? or is something wrong with my code?

1

There are 1 best solutions below

0
On

You can try one of the following option if it works for you

1- import { TouchableOpacity } from 'react-native-gesture-handler';

2- Change height of Animated.View which contain TouchableOpacity (to fit size of TouchableOpacity)

3- By moving <Animated.View> inside TouchableOpacity

Look like there is open discussion on Touchableopacity not working inside Animated.View