在React原生应用程序的标题中,我有一个条件图标和一个Searchbar.

static navigationOptions = ({ navigation }) => {
const { params = {} } = navigation.state;
return {
  headerTitle: (
    <View
      style={{
        flex: 1,
        backgroundColor: Platform.OS === 'ios' ? '#e54b4d' : '',
        alignItems: 'center',
        flexDirection: 'row',
        paddingHorizontal: 10,
        height: StatusBar.currentHeight,
      }}>
      {params.isIconTriggered && <Icon name="chevron-left" size={28} />}
      <SearchBar
        round
        platform={'default'}
        placeholder="Search"
        containerStyle={{
          flex: 1,
          backgroundColor: 'transparent',
        }}
      />
    </View>
  ),
  headerStyle: {
    backgroundColor: '#e54b4d',
  },
};
};

通常搜索栏会占据标题的全宽,这就是我想要的.如果条件isIconTriggered为真,则搜索栏前面会出现一个图标,搜索栏的宽度会缩小到足够大,以便在旁边可以看到该图标.

然而,发生这种情况时没有过渡或动画,感觉也不好.我想给搜索栏添加一个动画,这样当条件被触发并出现图标时,宽度会逐渐平滑地缩小.

这有可能实现吗?我该如何实现?

推荐答案

try 学习react native的动画API.

下面是我如何使用按钮触发器完成的.

here is the preview

import React, {Component} from 'react';
import {StyleSheet, View, TextInput , Button, SafeAreaView, Animated} from 'react-native';
import FA from 'react-native-vector-icons/FontAwesome5'

const AnimatedIcon = Animated.createAnimatedComponent(FA)
// make your icon animatable using createAnimatedComponent method

export default class Application extends Component {

  animVal = new Animated.Value(0);
   // initialize animated value to use for animation, whereas initial value is zero

  interpolateIcon = this.animVal.interpolate({inputRange:[0,1], outputRange:[0,1]})
  interpolateBar = this.animVal.interpolate({inputRange:[0,1],outputRange:['100%','90%']})
  // initialize interpolation to control the output value that will be passed on styles
  // since we will animate both search bar and icon. we need to initialize both 
  // on icon we will animate the scale whereas outputRange starts at 0 end in 1
  // on search bar we will animate width. whereas outputRange starts at 100% end in 90%

  animatedTransition = Animated.spring(this.animVal,{toValue:1})
  // we use spring to make the animation bouncy . and it will animate to Value 1

  clickAnimate = () => {
    this.animatedTransition.start()
  }
  // button trigger for animation

  //Components that will use on Animation must be Animated eg. Animted.View
  render() {
    return (
      <SafeAreaView>
      <View style={styles.container}>
        <View style={styles.search}>
        {/* our icon */}

        <Animated.View style={{width: this.interpolateBar}}>
        <TextInput placeholder='search here' style={styles.input}/>
        </Animated.View>

        <AnimatedIcon name='search' size={28} style={{paddingLeft: 10,paddingRight:10, transform:[{scale: this.interpolateIcon}]}}/>
        </View>

          <Button title='animate icon' onPress={this.clickAnimate}/>
      </View>
      </SafeAreaView>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    backgroundColor:'#F79D42',
    // flex: 1,
    height:'100%',
    paddingTop:20,
    flexDirection: 'column',
    // justifyContent: 'center',
    alignItems:'center'
  },
  input:{
    width: '100%',
    height:40,
    backgroundColor:'gray',
    textAlign:'center'
  },
  search:{
    flexDirection:'row-reverse',
    width:'90%',
    height:40,
    alignItems:'center'
  }
});

使用react native elements搜索栏组件的解决方案.

这样地:

 <Animated.View style={{width: this.interpolateBar}}>
    <SearchBar
    placeholder="Type Here..."
    containerStyle={{width: '100%'}}
  />
    </Animated.View>

enter image description here

React-native相关问答推荐

React Native:无法将具有 resizeMode包含的图像定位在父组件的 flex-end

如何在react-native 中设置视图的自动高度

react native HTML entities

React Native Ios错误 - ENOENT:no such file or directory, uv_cwd (null)

No component found for view with name "ARTShape"

react-native-firebase crashlytics 未显示在 firebase 仪表板上

如何在获取请求中传递 POST 参数?

错误:安装 react-native-elements 后无法识别字体系列 Material Design 图标?

Test suite无法运行 TypeError:Cannot read property ‘default’ of undefined

在 React Native 中无法滚动到 ScrollView 的底部

如果我有 AngularJS 基础知识,怎么学习Thinking in ReactJS呢?

React Native require(image) 返回数字

检测是否通过推送通知打开了 React Native iOS 应用

如何在样式组件之外获取主题?

react-native 中的倒数计时器

React Native Bullet Character? or Unicode?

Xcode 12 - 没有要编译的架构(ONLY_ACTIVE_ARCH=YES,active arch=x86_64,VALID_ARCHS=arm64e armv7s arm64 arm7)

为react-native TextInput 设置边框

加载字体native-base 错误

如何定义react-native应用程序的入口点