【问题标题】:react native navbar animation input text反应原生导航栏动画输入文本
【发布时间】:2016-06-15 12:21:30
【问题描述】:

我想在焦点 TextInput 动画上显示一个取消 (iptal) 按钮?

我做了以下代码,但没有成功:

constructor(props) {
    super(props);
    this.state = { cancelBtn:this.cancelBtn()};
}

cancelBtn(){
    return (<TouchableHighlight style={styles.searchTouch} onPress={this.press()}>
    <Text style={styles.searchBarText}>İptal</Text>
    </TouchableHighlight>);
}

render(){
    <View style={styles.searchBar}>
        <View style={styles.searchBarBox}>
            <TextInput
                ref="searchBarInput"
                style = {styles.searchBarInput}
                placeholder = 'Mekan Ara...'
            />
        </View>
    {this.state.cancelBtn}
    </View>
}

我如何以动画方式做到这一点?

图片: s.s.1 => http://i.stack.imgur.com/m7wxm.png

s.s.2 => http://i.stack.imgur.com/hYa3z.png

【问题讨论】:

  • 您是要为其制作动画还是仅根据焦点显示/隐藏?
  • 焦点输入文本输入按钮要变短并出现取消按钮。

标签: animation reactjs react-native textinput


【解决方案1】:

使用TextInputonFocus 方法触发动画。用onBlur 反转它。然后,根据输入是否被选中(状态)显示输入。

此示例未从样式角度进行测试,但应该可以让您有所了解。 Make sure to read up on the Animation docs 也是。

constructor() {
  this.state = {
    inputLength: new Animated.Value(100), // initial value 
    isFocused: false
  }
}

onFocus() {
  Animated.timing(this.state.inputLenght, {
    toValue: 90, // or whatever value
    duration: 1000
  }).start(() => this.setState({isFocused: true}))
}

onBlur() {
  Animated.timing(this.state.inputLenght, {
    toValue: 100, // or whatever value
    duration: 1000
  }).start(() => this.setState({isFocused: false}))
}



<Animated.View style={{width: this.state.inputLength}}>
  <TextInput
    ref="input"
    onFocus={this.onFocus}
    onBlur={this.onBlur}
    ...
  />
</Animated.View>
{this.state.isFocused && (
  <TouchableOpacity onPress={() => this.refs.input.blur()}><Text>Submit</Text></TouchableOpacity>
)}

【讨论】:

  • 非常感谢。代码有效..如何在文本输入焦点后移除焦点。取消按钮被按下?
  • 您可以在 TextInput 中放置一个 ref,然后通过调用 this.refs.input.blur() 来调用它。
猜你喜欢
  • 2018-07-11
  • 2018-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-23
  • 2018-04-17
  • 2021-03-23
相关资源
最近更新 更多