3

I can't click TouchableOpactity in Animated.View. Can you help me?

<Animated.View>
    <TouchableOpacity>
        <Text>Click Me!!</Text>
    </TouchableOpacity>
</Animated.View>
ravibagul91
  • 20,072
  • 5
  • 36
  • 59
An Cam
  • 113
  • 3
  • 13

3 Answers3

5

use TouchableOpacity from 'react-native-gesture-handler' instead of from 'react-native'.

import { TouchableOpacity } from 'react-native';

import { TouchableOpacity } from 'react-native-gesture-handler';
Randunu.KSW
  • 405
  • 4
  • 9
  • Take in to consideration that there is still a style compatibility [issue](https://github.com/software-mansion/react-native-gesture-handler/issues/607) – Cris69 Jan 19 '20 at 09:34
  • I had the same issue with TouchableHighlight and this solved it – Jared Beach Apr 04 '20 at 23:27
  • TouchableOpacity from gesture-handler worked for Android and I got undesirable behaviour on iOS so I used Pressable for the same implementation on iOS. 2022. – Jordan Grant Jan 12 '22 at 03:51
1

I think Animated API not working like that, you have to create separate class like in this example and demo in snack:

import React from 'react';
import { Animated, Text, View } from 'react-native';

class FadeInView extends React.Component {
  state = {
    fadeAnim: new Animated.Value(0),  // Initial value for opacity: 0
  }

  componentDidMount() {
    Animated.timing(                  // Animate over time
      this.state.fadeAnim,            // The animated value to drive
      {
        toValue: 1,                   // Animate to opacity: 1 (opaque)
        duration: 10000,              // Make it take a while
      }
    ).start();                        // Starts the animation
  }

  render() {
    let { fadeAnim } = this.state;

    return (
      <Animated.View                 // Special animatable View
        style={{
          ...this.props.style,
          opacity: fadeAnim,         // Bind opacity to animated value
        }}
      >
        {this.props.children}
      </Animated.View>
    );
  }
}

// You can then use your `FadeInView` in place of a `View` in your components:
export default class App extends React.Component {
  render() {
    return (
      <View style={{flex: 1, alignItems: 'center', justifyContent: 'center'}}>
        <FadeInView style={{width: 250, height: 50, backgroundColor: 'powderblue'}}>
          <Text style={{fontSize: 28, textAlign: 'center', margin: 10}}>Fading in</Text>
        </FadeInView>
      </View>
    )
  }
}
Satrio Adi Prabowo
  • 570
  • 1
  • 4
  • 13
-1

see this question for more info if PanResponder is used. TouchableOpacity with parent PanResponder


PS: in my case, below can make TouchableHighlight unresponding.

import { TouchableHighlight } from 'react-native-gesture-handler';
Chenhua
  • 185
  • 2
  • 10