代码之家  ›  专栏  ›  技术社区  ›  Eduard

在React Native中手动按TouchableOpacity设置不透明度的音量

  •  21
  • Eduard  · 技术社区  · 9 年前

    根据 documentation 为此 Animated API

    不透明度通过在动画中包裹子对象来控制。视图,该视图添加到视图层次中。请注意,这可能会影响布局。

    我做到了,看起来是这样的:

    <Animated.View style={{ opacity: this.state.opacity._value }}>
        <TouchableOpacity 
            onPress={this.hideKeyboard.bind(this)}
            style={{ opacity: this.state.opacity._value }}
        >
            <Text style={buttonTextStyle}>Cancel</Text>
        </TouchableOpacity>
    </Animated.View>
    

    这个 方法调用 更改不透明度

    changeOpacity() {
        Animated.timing(
            this.state.opacity, 
            {
                toValue: this.state.opacity === 1 ? 0 : 1,
                duration: 500
            }
        ).start();
    }
    

    this.state.opacity 在构造函数中声明:

    constructor(props) {
        super(props);
        this.state = { opacity: new Animated.Value(1) };
    }
    

    1 回复  |  直到 9 年前
        1
  •  56
  •   Ravi Raj    9 年前

    你试过这个吗

    <TouchableOpacity 
      activeOpacity={.7} // default is .2
      ... other props here
    />
    
    推荐文章